![](/img/trans.png)
[英]How do I switch which is already in a fragment to move from one fragment to another (Java) in Android?
[英]Not able to switch one fragment to another in android
錯誤顯示無法將Highlight轉換為Fragment,也無法將Overview轉換為Fragment,並且錯誤僅在selectFrag()方法中出現。 請任何人幫我解決此問題。 當我單擊textview項目時,我想將一個片段切換到另一個片段。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFrag(View view){
Fragment fr;
if (view == findViewById(R.id.txt_highlite)) {
fr = (Fragment) new Highlight();
}
else{
fr = (Fragment) new Overview();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.place_holder, fr);
fragmentTransaction.commit();
}
}
//突出顯示類
public class Highlight extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.highlight, container, false);
}
}
//概述類
public class Overview extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.overview, container, false);
}
}
//activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp" >
<LinearLayout
android:id="@+id/lin_projectoverview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp" >
<TextView
android:id="@+id/text_Projectoverview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Project Overview"
android:textSize="26sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/project_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="65dp"
android:gravity="center" >
<TextView
android:id="@+id/txt_overview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginLeft="12dp"
android:text="Overview" />
<TextView
android:id="@+id/txt_highlite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginLeft="12dp"
android:text="Highlight" />
</LinearLayout>
</RelativeLayout>
<fragment
android:name="com.example.fragment_move.Overview"
android:id="@+id/place_holder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
檢查您的導入聲明。 根據您的getFragmentManager()
,擴展Fragment
每個類都應僅具有此片段導入:
import android.app.Fragment;
Highlight和Overview已經從Fragment繼承了,那么為什么要再次將它們轉換為片段? 我建議您這樣做。
public void selectFrag(View view){
Fragment fr;
if (view.getId() == R.id.txt_highlite) {
fr = new Highlight();
}
else{
fr = new Overview();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.place_holder, fr);
fragmentTransaction.commit();
}
並確保您的片段導入始終來自支持庫或其他方式
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.