话不多说直接上代码
:
MainActivitypublic class MainActivity extends FragmentActivity implements View.OnClickListener{ PRivate Button button1,button2,button3,button4; private FrameLayout framelayout; private FragmentManager fm; private OneFragment onef; private BlankFragment twof; private FourFragment threef; protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createid(); fm=getSupportFragmentManager(); } private void createid() { button1= (Button) findViewById(R.id.button1); button2= (Button) findViewById(R.id.button2); button3= (Button) findViewById(R.id.button3); button4= (Button) findViewById(R.id.button4); framelayout= (FrameLayout) findViewById(R.id.Fragmentlayout);// 点击事件 button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); button4.setOnClickListener(this); } @Override public void onClick(View view) {// 开启事务 FragmentTransaction fmt=fm.beginTransaction();// 首先隐藏fragment hidefragment(fmt); if(view.getId()==R.id.button1){ if (onef==null){ onef=new OneFragment(); fmt.add(R.id.Fragmentlayout,onef);} else{ fmt.show(onef); } } if(view.getId()==R.id.button2){ if (twof==null){ twof=new BlankFragment(); fmt.add(R.id.Fragmentlayout,twof);} else{ fmt.show(twof); } } if(view.getId()==R.id.button3){ if (threef==null){ threef=new FourFragment(); fmt.add(R.id.Fragmentlayout,threef);} else{ fmt.show(threef); } } if (view.getId()==R.id.button4){ Log.e("button4","你还需要努力"); } fmt.commit(); } private void hidefragment(FragmentTransaction fmt) { if (onef!=null){ fmt.hide(onef); } if (twof!=null){ fmt.hide(twof); } if (threef!=null){ fmt.hide(threef); } }}主界面的xml:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/Fragmentlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="5" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Button1"/> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Button2"/> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Button3"/> <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Button4"/> </LinearLayout></LinearLayout>OneFragment :public class OneFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_one, container, false); }其他的Fragment和一样。fragment的XML:<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="这是第一个" />
新闻热点
疑难解答