Android-FrameLayout實現多頁面(QQ頁面)
阿新 • • 發佈:2018-12-13
第一步 在activity_main.xml中佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> //FrameLayout <FrameLayout android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </FrameLayout> //底部按鈕 <RadioGroup android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:textSize="25dp" android:background="@drawable/selector" android:text="首頁"/> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textSize="25dp" android:button="@null" android:background="@drawable/selector" android:text="推薦"/> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:textSize="25dp" android:background="@drawable/selector" android:text="我的"/> </RadioGroup> </LinearLayout>
第二步 在MainActivity.java中寫程式碼
public class MainActivity extends AppCompatActivity {
private RadioGroup radiogroup; private FragmentManager manager; private Fragment1 fragment1; private Fragment2 fragment2; private Fragment3 fragment3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); //查詢控制元件 radiogroup = (RadioGroup)findViewById( R.id.radiogroup ); //得到管理這類 manager = getSupportFragmentManager(); //開啟事物 FragmentTransaction transaction = manager.beginTransaction(); //新增事物 fragment1 = new Fragment1(); fragment2 = new Fragment2(); fragment3 = new Fragment3(); transaction.add( R.id.pager, fragment1 ); transaction.add( R.id.pager, fragment2 ); transaction.add( R.id.pager, fragment3 ); //只顯示第一個fragment transaction.show( fragment1 ).hide( fragment2 ).hide( fragment3 ); //預設選中第一個按鈕 radiogroup.check( radiogroup.getChildAt( 0 ).getId() ); //提交事物 transaction.commit(); //當我點選按鈕時 頁面切換 radiogroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { FragmentTransaction transaction1 = manager.beginTransaction(); switch (checkedId){ case R.id.radio1: //顯示對應的fragment transaction1.show( fragment1 ).hide( fragment2 ).hide( fragment3 ); break; case R.id.radio2: //顯示對應的fragment transaction1.show( fragment2 ).hide( fragment1 ).hide( fragment3 ); break; case R.id.radio3: //顯示對應的fragment transaction1.show( fragment3 ).hide( fragment2 ).hide( fragment1 ); break; } //提交事務 transaction1.commit(); } } );//當我點選按鈕時 頁面切換 }
}