Androidbby
来源:互联网 发布:access sql 编辑:程序博客网 时间:2024/06/05 17:57
private static final int QUANTITY = 4; private FrameLayout flMain; private List<Fragment> fragments; private RadioGroup rgNavigation; private int currentIndex = 0; //导航切换 private void radioGroupManage() { fragments = new ArrayList<>(); final RadioButton[] buttons=new RadioButton[QUANTITY]; assert rgNavigation!=null; for(int i=0;i<rgNavigation.getChildCount();i++){ //获取第i个位置上的子控件 buttons[i] = (RadioButton) rgNavigation.getChildAt(i); //往集合中添加Fragment对象 fragments.add(FragmentFactory.createFragment(i)); } //默认加载第一个Fragment对象 getSupportFragmentManager().beginTransaction().add(R.id.fl_main,fragments.get(0)).commit(); //处理单机事件 rgNavigation.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { for(int i=0;i<rgNavigation.getChildCount();i++){ if(buttons[i].getId()==checkedId){ switchFragment(i); } } } }); } //切换Fragment的方法 private void switchFragment(int targetIndex) { //目标的Fragment Fragment targetFragment = fragments.get(targetIndex); //当前的Fragment Fragment currentFragment = fragments.get(currentIndex); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); if(targetFragment.isAdded()){ //目标Fragment已经被添加了...则隐藏当前正在展示的Fragment,显示目标Fragment fragmentTransaction.hide(currentFragment).show(targetFragment); }else { //没有被添加...则隐藏当前的Fragment,添加目标Fragment fragmentTransaction.hide(currentFragment).add(R.id.fl_main,targetFragment); } fragmentTransaction.commit(); //改变当前索引值 currentIndex=targetIndex; }
public class FragmentFactory { private static final int FRAGMENT_INDEX = 0; private static final int FRAGMENT_CATEGORY = 1; private static final int FRAGMENT_FIND = 2; private static final int FRAGMENT_MINE = 3; private static SparseArray<Fragment> fragments = new SparseArray<>(); public static Fragment createFragment(int index){ Fragment fragment = fragments.get(index); if(fragment == null){ switch (index){ case FRAGMENT_INDEX: fragment = new IndexFragment(); break; case FRAGMENT_CATEGORY: fragment = new CategoryFragment(); break; case FRAGMENT_FIND: fragment = new FindFragment(); break; case FRAGMENT_MINE: fragment = new MineFragment(); break; } fragments.put(index,fragment); } return fragment; }}
<style name="rb_style"> <item name="android:gravity">center</item> <item name="android:layout_width">0dp</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_weight">1</item> <item name="android:button">@null</item></style>
<RelativeLayout 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" tools:context="bby.com.projecttemplate.ui.MainActivity"> <FrameLayout android:id="@+id/fl_main" android:layout_above="@+id/rg_navigation_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <!--<TextView--> <!--android:layout_width="match_parent"--> <!--android:layout_height="1dp"--> <!--android:background="#c4c3c3"/>--> <RadioGroup android:id="@+id/rg_navigation_main" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="2dp" android:background="#fff"> <RadioButton android:id="@+id/rb_index_navigation_main" style="@style/rb_style" android:text="首页" android:checked="true" android:drawableTop="@drawable/selector_navigation_index"/> <RadioButton android:id="@+id/rb_category_navigation_main" style="@style/rb_style" android:text="分类" android:drawableTop="@drawable/selector_navigation_category"/> <RadioButton android:id="@+id/rb_find_navigation_main" style="@style/rb_style" android:text="发现" android:drawableTop="@drawable/selector_navigation_find"/> <RadioButton android:id="@+id/rb_mine_navigation_main" style="@style/rb_style" android:text="我" android:drawableTop="@drawable/selector_navigation_mine"/> </RadioGroup></RelativeLayout>
阅读全文
0 0
- Androidbby
- Androidbby
- HDU 5952 搜索
- STL之Iterator
- ffmpeg一些东西设置
- C++中的异常处理开销(13)---《More Effective C++》
- 洛谷P2024 食物链 (POJ 1182)
- Androidbby
- java抽象类练习题
- HDU-3746 Cyclic Nacklace (活用KMP中next数组)
- 兔子路径规划
- 获取指定时间段内的日期 和 获取指定日期是星期几
- 求最长不下降序列并输出序列(动态规划)
- python3 json 与 对象之间的相互转换
- php调用python文件换脸 dlip+opencv+windows64系统
- win10、win7下关掉某个端口的进程