主界面的Fragment切换之使用RadioGroup配合切换Fragment

注:效果和使用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>

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页