1、市场上很多app里面的主界面布局是底部是自定义控件或者是radiogroup,然后跟fragment结合使用,然后我的想法是以前的MainAcivity里面放着两种东西。一种是splashFragment,然后里面是viewpager,当滑动到最后一个的时候再从右往左滑动的话就可以直接进入使用界面,但是我的使用界面其实一个fragment。然后fragment里面使用的跟普通的radiogroup和radioButton结合着fragment使用来实现主界面的切换,值得注意的是主界面点击下面按钮的时候到底是replace其他的fragment还是隐藏和显示其他的fragment就要看需求。因为如果直接replace的话生命周期走的是不一样的,比如第一个界面看到的是差不多到底部然后内容没有看完,贰切换到其他界面之后再回国来就又要从上往下滑动,这样用户体验很差,所以要根据实际情况来选择哪一种替换fragment的方法。当然避免每次重复的加载数据浪费资源,所以记得看打log来观察生命周期的变化来避免资源浪费。
2、MainActivity的代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); getFragmentManager().beginTransaction().replace(R.id.replace,new BigFragment()).commit(); }
MainActivity的xml:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/replace" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout>
最外面那层Fragment,我称之为bigFragment,代码:
public class BigFragment extends Fragment { List<Fragment> mFragments = new ArrayList<>(); private RadioGroup radioGroup; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_big, null); radioGroup = (RadioGroup) view.findViewById(R.id.select_button); mFragments.add(new FirstFragment()); mFragments.add(new SecondFragment()); mFragments.add(new ThirdFragment()); mFragments.add(new FouthFragment()); mFragments.add(new FifthFragment()); radioGroup.setOnCheckedChangeListener(mCheckChangeListener); /** * 初始化默认选择哪一个radioButton,有三种方法,然后推荐用第三种 */ //1、 mCheckChangeListener.onCheckedChanged(radioGroup, 0); //2 、getFragmentManager() .beginTransaction() .replace(R.id.replace, fragment) .commit(); //3、因为逼格更高 ((RadioButton) radioGroup.getChildAt(0)).setChecked(true); return view; } private RadioGroup.OnCheckedChangeListener mCheckChangeListener = new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { View child = radioGroup.findViewById(i);//这个i是选中的那个radioButton的id int index = radioGroup.indexOfChild(child);//根据radioButton去比较radioGroup拿到位置 Fragment fragment = mFragments.get(index); addHideShowFragment(fragment); } }; //看公司需求,这个是替换,就跟刷新一样 private void replaceFragment(Fragment fragment) { getFragmentManager() .beginTransaction() .replace(R.id.replace_2, fragment) .commit(); } // replace 操作和 add remove 没什么用 private void addRemoveFragment(Fragment fragment) { FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.replace_2, fragment); if (mCurrentFragment != null) { fragmentTransaction.remove(mCurrentFragment); } fragmentTransaction.commit(); mCurrentFragment = fragment; } // hide 和 show 操作,不会对生命周期方法产生影响,就是你返回之后,还是在相同的地方 Fragment mCurrentFragment; List<Fragment> mHasAddFragments = new ArrayList<Fragment>(); private void addHideShowFragment(Fragment fragment) { FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); // 如果fragment没有添加过,就添加, // 如果fragment添加过,就显示 // 如果原来的fragment存在,就隐藏 if (!mHasAddFragments.contains(fragment)) { fragmentTransaction.add(R.id.replace_2, fragment); mHasAddFragments.add(fragment); } else { fragmentTransaction.show(fragment); } if (mCurrentFragment != null) { fragmentTransaction.hide(mCurrentFragment); } mCurrentFragment = fragment; fragmentTransaction.commit(); } }
bigFragment的xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/replace_2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/select_button" android:background="#98dfac" /> <RadioGroup android:id="@+id/select_button" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <RadioButton android:id="@+id/rd_1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/bg1_selector" android:button="@null" android:gravity="center" android:text="第一个" android:textColor="@color/text_bg_selector" /> <RadioButton android:id="@+id/rd_2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/bg2_selector" android:button="@null" android:gravity="center" android:text="第二个" android:textColor="@color/text_bg_selector" /> <RadioButton android:id="@+id/rd_3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/bg3_selector" android:button="@null" android:gravity="center" android:text="第三个" android:textColor="@color/text_bg_selector" /> <RadioButton android:id="@+id/rd_4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/bg4_selector" android:button="@null" android:gravity="center" android:text="第四个" android:textColor="@color/text_bg_selector" /> <RadioButton android:id="@+id/rd_5" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/bg5_selector" android:button="@null" android:gravity="center" android:text="第五个" android:textColor="@color/text_bg_selector" /> </RadioGroup> </RelativeLayout>
bigFragment里面的字体selector,在res/color目录下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#fff" android:state_pressed="true" /> <item android:color="#000" android:state_checked="false"/> <item android:color="#fff" android:state_checked="true" /> </selector>
bigFragment的里面的背景的变化selector在res/drawable目录下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/fifthSelect" android:state_pressed="true" /> <item android:drawable="@color/fifthSelect" android:state_checked="true" /> <item android:drawable="@color/fifthNormal" android:state_checked="false" /> </selector>
然后我的五个测试的fragment都是一样的,我只展示其中一个就ok了:
public class FirstFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_first, null); return view; } }
fragment的xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="第一个fragment" android:textSize="30dp" /> </LinearLayout>