XML:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.example.mmaster.shuju_____myapplication.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="54dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="首页" android:textSize="30sp" /> <Button android:id="@+id/but_jia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:text="+" /> </RelativeLayout> <!--FrameLayout包裹按钮--> <FrameLayout android:id="@+id/frament" android:layout_width="match_parent" android:layout_height="match_parent"> <RadioGroup android:id="@+id/group" android:layout_width="match_parent" android:layout_height="54dp" android:layout_gravity="bottom" android:orientation="horizontal"> <RadioButton android:id="@+id/lie_biao" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="列表" /> <RadioButton android:id="@+id/xiang_qing" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="详情" /> </RadioGroup> </FrameLayout><!--FrameLayout包裹按钮--> </LinearLayout>
Main页面 不用写适配器
package com.example.mmaster.shuju_____myapplication; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import android.widget.RadioButton; import com.example.mmaster.shuju_____myapplication.fragment.Shou_ye_Fragment; import com.example.mmaster.shuju_____myapplication.fragment.Xiang_qing_Fragment; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Shou_ye_Fragment shou_ye_fragment; private Xiang_qing_Fragment xiang_qing_fragment; private RadioButton lie_biao; private RadioButton xiang_qing; private Button but_jia; //管理者调全局 private FragmentManager supportFragmentManager; //执行者调全局 private FragmentTransaction fragmentTransaction; //调全局FrameLayout控件 private FrameLayout frament; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { //初始化A页面(调全局) shou_ye_fragment = new Shou_ye_Fragment(); //初始化B页面(调全局) xiang_qing_fragment = new Xiang_qing_Fragment(); supportFragmentManager = getSupportFragmentManager(); fragmentTransaction = supportFragmentManager.beginTransaction(); fragmentTransaction.add(R.id.frament, shou_ye_fragment); fragmentTransaction.commit(); } private void initView() { lie_biao = (RadioButton) findViewById(R.id.lie_biao); xiang_qing = (RadioButton) findViewById(R.id.xiang_qing); but_jia = (Button) findViewById(R.id.but_jia); but_jia.setOnClickListener(this); lie_biao.setOnClickListener(this); xiang_qing.setOnClickListener(this); frament = (FrameLayout) findViewById(R.id.frament); frament.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.lie_biao: //管理者 执行者 //替换 //提交 getSupportFragmentManager().beginTransaction().replace(R.id.frament, shou_ye_fragment).commit(); break; case R.id.xiang_qing: //管理者 执行者 //替换 //提交 getSupportFragmentManager().beginTransaction().replace(R.id.frament, xiang_qing_fragment).commit(); break; case R.id.but_jia: break; } } }