主界面的Fragment切换之使用RadioGroup配合切换Fragment
2016-12-20 16:15
337 查看
注:效果和使用FragmentTabHost实现相同,在这就只贴出代码
布局文件
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { @BindView(R.id.fl_container) FrameLayout flContainer; @BindView(R.id.rb_all) RadioButton rbAll; @BindView(R.id.rb_tweet) RadioButton rbTweet; @BindView(R.id.rb_add) TextView rbAdd; @BindView(R.id.rb_explore) RadioButton rbExplore; @BindView(R.id.rb_me) RadioButton rbMe; @BindView(R.id.rg_group) RadioGroup rgGroup; private ArrayList<Fragment> fragments; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //创建fragment实例并把他们加入集合 fragments = new ArrayList<>(); addFragment(); //设置默认被选中的RadioButton rgGroup.check(R.id.rb_all); switchFragment(0); //radiogroup中的radiobutton的点击事件 rgGroup.setOnCheckedChangeListener(this); //radiogroup中加号的点击事件 rbAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "hahaha", Toast.LENGTH_SHORT).show(); } }); /** * radiogroup中的radiobutton的点击事件 * * @param group * @param checkedId */ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.rb_all: switchFragment(0); break; case R.id.rb_tweet: switchFragment(1); break; case R.id.rb_explore: switchFragment(2); break; case R.id.rb_me: switchFragment(3); break; } } /** * 创建fragment实例并把他们加入集合 */ public void addFragment() { fragments.add(new AllFrament()); fragments.add(new TweetFrament()); fragments.add(new ExploreFrament()); fragments.add(new MeFrament()); } /** * 点击切换fragment * * @param position */ public void switchFragment(int position) { //开启事务 FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); //遍历集合 for (int i = 0; i <fragments.size() ; i++) { Fragment fragment = fragments.get(i); if (i==position){ //显示fragment if (fragment.isAdded()){ //如果这个fragment已经被事务添加,显示 fragmentTransaction.show(fragment); }else{ //如果这个fragment没有被事务添加过,添加 fragmentTransaction.add(R.id.fl_container,fragment); } }else{ //隐藏fragment if (fragment.isAdded()){ //如果这个fragment已经被事务添加,隐藏 fragmentTransaction.hide(fragment); } } } //提交事务 fragmentTransaction.commit(); } }
布局文件
<?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"> <FrameLayout android:id="@+id/fl_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </FrameLayout> <RadioGroup android:id="@+id/rg_group" android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginBottom="0dp" android:gravity="center_vertical" android:orientation="horizontal"> <RadioButton style="@style/tab_style" android:id="@+id/rb_all" android:text="综合" android:drawableTop="@drawable/tab_icon_new"/> <RadioButton style="@style/tab_style" android:id="@+id/rb_tweet" android:text="动弹" android:drawableTop="@drawable/tab_icon_tweet"/> <TextView style="@style/tab_style" android:id="@+id/rb_add" android:paddingTop="10dp" android:drawableTop="@drawable/btn_quickoption_selector"/> <RadioButton style="@style/tab_style" android:id="@+id/rb_explore" android:text="发现" android:drawableTop="@drawable/tab_icon_explore"/> <RadioButton style="@style/tab_style" android:id="@+id/rb_me" android:text="我" android:drawableTop="@drawable/tab_icon_me"/> </RadioGroup> </LinearLayout>
相关文章推荐
- Android 使用ViewPager和RadioGroup配合Fragment实现标签页,可点击按钮切换、滑动切换。
- [Android学习笔记]fragment配合RadioGroup实现点击切换内容
- Fragment配合RadioGroup实现点击切换布局
- RadioGroup结合RadioButton使用切换Fragment片段
- fragment、viewpager、radiogroup的配合使用
- 主界面的Fragment切换之使用FragmentTabHost配合切换Fragment
- Android中使用RadioGroup实现Fragment的切换
- RadioGroup+Fragment实现界面切换
- 巧妙实现缺角radiogroup控制多个fragment切换和滑动
- RadioGroup+Fragment实现页面之间的切换
- 安卓APP常用界面切换RadioButton+fragment
- RadioGroup+Fragment实现页面之间的切换
- 使用Fragment和RadioGroup实现类似ios的tabbar的效果
- RadioGroup+Fragment实现页面之间的切换
- RadioGroup+Fragment实现页面之间的切换
- RadioGroup+Fragment实现页面之间的切换
- 使用RadioGroup及Fragment来实现底部Tab效果 (AS版本)
- 使用RadioGroup+ViewPager+Fragment实现带滑动的页卡效果TabHost时遇到的问题
- RadioGroup+Fragment实现页面之间的切换
- Fragment的简单使用(切换界面)-FenGKun