在片段视图之间切换

在xml布局文件中声明片段的标准方法是

<LinearLayout ...> <fragment class="com.example.SomeFragment" </LinearLayout> 

SomeFragment是一个类定义的java类

 class SomeFragment extends Fragment { ... } 

可以说,我有3个片段; 片段1,片段2和片段3。 当用户启动应用程序时,我向他们展示了片段1,当他们点击button时,我用片段2replace了片段1等。

在单个布局xml文件中定义3个片段的最佳方法是什么?

Solutions Collecting From Web of "在片段视图之间切换"

你应该使用一个FrameLayout,这样你就不必在XML中指定片段类,这样就不限于一个类。

 <FrameLayout android:id="@+id/contentFragment" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> 

而且你可以像这样在代码中设置片段

 Fragment fragment = new YourFragment(); FragmentManager fm = getSupportFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); transaction.replace(R.id.contentFragment, fragment); transaction.commit(); 

我举一个例子来在一个片段中的两个布局之间切换:

首先声明一个包含两个片段的布局(这取决于你在布局中需要多less片段)

fragment_layout_example.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <FrameLayout android:id="@+id/fragment_container" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/Fragment2" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" class="com.example.SecondFragment" > <!-- Preview: layout=@layout/details --> </fragment> </LinearLayout> 

以上布局将显示两个片段Fragment1和Fragment2。 对于Fragment1我已经声明容器作为容器的内容将在运行时更改。 所以在这里没有声明Fragment类。 更多关于这个检查

http://developer.android.com/training/basics/fragments/fragment-ui.html

然后创build一个扩展Activity的类FragmentExampleActivity。 如果您在向后兼容模式下使用Fragment,则扩展FragmentActivity

  public class FragmentExampleActivity extends FragmentActivity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_layout_example); // Check that the activity is using the layout version with // the fragment_container FrameLayout if (findViewById(R.id.fragment_container) != null) { // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. if (savedInstanceState != null) { return; } // Create an instance of Fragment1 Fragment1 firstFragment = new Fragment1(); // In case this activity was started with special instructions from an Intent, // pass the Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, firstFragment).commit(); } } } 

要创build两个片段的布局,请创build两个扩展Fragment

 public class Fagment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //set the layout you want to display in First Fragment View view = inflater.inflate(R.layout.fragment1, container, false); return view; } } 

同样的方式创build第二个片段的片段类,并设置布局

现在,如果你想在Fragment1切换到另一个布局,点击一个button,然后创build另一个类,如Fragment3.java,并设置你想切换的布局,并将下面的代码写入Fragment1.java

 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Button showFragment3=(Button)getView().findViewById(R.id.Button1); showFragment3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); Fragment3 fragment3 = new Fragment3(); fragmentTransaction.replace(R.id.Fragment1, fragment3); //provide the fragment ID of your first fragment which you have given in //fragment_layout_example.xml file in place of first argument fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }); } 

现在再次回到第一个片段,你可以点击后退button。 但是如果你想回到点击button,然后在Fragment3.java中写下面的代码

 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Button showFragment1 = (Button) getView().findViewById( R.id.Button2); showFragment1 .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { getFragmentManager().popBackStack(); } }); } 

谢谢! 希望它能帮助你…