示例一:
效果如图所示
每一个Fragment显示一个字符串,比如。
在所有的例子里面,都有四个简单Fragment,分别为FragmentA,FragmentB,FragmentC,FragmentD。。
类的内容
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_a, container, false); }
布局文件就是FrameLayout中有一个TextView。
MainActivity的布局文件内容可以看出下面四个导航按钮是RadioButton
<?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"> <FrameLayout android:id="@+id/content" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> </FrameLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/devider_line" > </View> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" > <RadioButton android:id="@+id/btn_stimulation" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/stimulation" android:gravity="center_horizontal" android:text="@string/tab_stimulation" android:textColor="@color/yellow" /> <RadioButton android:id="@+id/btn_prescription" ............. /> <RadioButton android:id="@+id/btn_network" .............. /> <RadioButton android:id="@+id/btn_setting" ........ /> </RadioGroup> </LinearLayout>
使用ButterKnife获取控件对象
@BindView(R.id.content)
FrameLayout content;
@BindView(R.id.btn_stimulation)
RadioButton btnStimulation;
............
@BindView(R.id.radioGroup)
RadioGroup radioGroup;
在初始化时添加radioGroup的点击响应事件
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { switch (checkedId) { case R.id.btn_stimulation: selectNavigation(0); break; case R.id.btn_prescription: selectNavigation(1); ........ } } });
按钮选择函数selectNavigation就是先将四个变灰色,再把选择的变成彩色,设置选中,同时调用Fragment选择函数。
private void selectNavigation(int page) { //四个导航按钮绘制成灰色 for (int i = 0; i < radioGroup.getChildCount(); i++) { Drawable grey_image = getResources().getDrawable(greyed[i]); grey_image.setBounds(0, 0, grey_image.getMinimumWidth(), grey_image.getMinimumHeight()); RadioButton child = (RadioButton) radioGroup.getChildAt(i); child.setCompoundDrawables(null, grey_image, null, null); child.setTextColor(getResources().getColor( R.color.dark_gray)); } //将指定按钮绘制成彩色,并设置选中 Drawable yellow = getResources().getDrawable(colored[page]); yellow.setBounds(0, 0, yellow.getMinimumWidth(), yellow.getMinimumHeight()); RadioButton select = (RadioButton) radioGroup.getChildAt(page); select.setCompoundDrawables(null, yellow, null, null); select.setTextColor(getResources().getColor( R.color.yellow)); select.setChecked(true); // 选择导航的时候,同时选择Fragment showFragment(page); }
其中greyed和colored就是底部四个灰色和彩色资源文件id数组。
Fragment的声明,创建和显示
FragmentA fragmentA; FragmentB fragmentB; FragmentC fragmentC; FragmentD fragmentD; List<Fragment> fragments = new ArrayList<Fragment>();
在onCreate的时候创建Fragment,调用InitFragments
private void InitFragments() { fragments.clear(); if (fragmentA == null) { fragmentA = new FragmentA(); } if (fragmentB == null) { fragmentB = new FragmentB(); } if (fragmentC == null) { fragmentC = new FragmentC(); } if (fragmentD == null) { fragmentD = new FragmentD(); } fragments.add(fragmentA); fragments.add(fragmentB); fragments.add(fragmentC); fragments.add(fragmentD); for (Fragment frag : fragments) { if (!frag.isAdded()) { getSupportFragmentManager().beginTransaction().add(R.id.content, frag).commit(); } } }
在切换导航的时候同时调用切换Fragment函数
private void showFragment(int frag) { //首先隐藏所有Fragments for (Fragment f : fragments) { getSupportFragmentManager().beginTransaction().hide(f).commit(); } //获取当前 序号的fragment Fragment current_frag = fragments.get(frag); if (current_frag != null) { getSupportFragmentManager().beginTransaction().show(current_frag).commit(); } }