Framelayout帧布局和ViewPager和Tablayout的连用

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"


    >
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="9"
    android:id="@+id/frame_layout"
    ></FrameLayout>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:id="@+id/radio_group"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/ra1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="我的"
            android:gravity="center"
            android:drawableTop="@drawable/sel"
            android:checked="true"
            />
        <RadioButton
            android:id="@+id/ra2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="你的"
            android:gravity="center"
            android:drawableTop="@drawable/sel"
            />
        <RadioButton
            android:id="@+id/ra3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="他的"
            android:gravity="center"
            android:drawableTop="@drawable/sel"
            />
    </RadioGroup>


</LinearLayout>

实现底部切换

public class MainActivity extends BaseActivity {


    private FrameLayout layout;
    private RadioGroup group;
    private com.example.framelayout_tablayout.fragment.frag1 frag1;
    private com.example.framelayout_tablayout.fragment.frag2 frag2;
    private com.example.framelayout_tablayout.fragment.frag3 frag3;
    private FragmentManager supportFragmentManager;

    @Override
    public int bindlayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        layout = findViewById(R.id.frame_layout);
        group = findViewById(R.id.radio_group);
    }

    @Override
    protected void intData() {
       //模拟数据
        frag1 = new frag1();
        frag2 = new frag2();
        frag3 = new frag3();
       //获取Fragmen管理器
        supportFragmentManager = getSupportFragmentManager();
        //开启
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        //替换Fragment
        transaction.
                add(R.id.frame_layout, frag1)
                .add(R.id.frame_layout,frag2)
                .add(R.id.frame_layout,frag3)
                .show(frag1)
                .hide(frag2)
                .hide(frag3)
                .commit();



    }

    @Override
    protected void Event() {
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                switch (checkedId){
                    case R.id.ra1:
                        transaction.show(frag1)
                                    .hide(frag2)
                                    .hide(frag3);
                        break;
                    case R.id.ra2:
                        transaction.show(frag2)
                                .hide(frag3)
                                .hide(frag1);
                        break;
                    case R.id.ra3:
                        transaction.show(frag3)
                                .hide(frag2)
                                .hide(frag1);
                        break;
                }
                transaction.commit();
            }
        });

    }
}

//上面的布局

LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f"
    android:orientation="vertical"
    >
    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tab_layout"
        ></android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/view_pager"
        ></android.support.v4.view.ViewPager>
</LinearLayout>
public class frag1 extends BaseFragment {

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private ArrayList<Fragment> list;

    @Override
    public int bindlayout() {
        return R.layout.frag01;
    }

    @Override
    protected void initView() {
        viewPager = getView().findViewById(R.id.view_pager);
        tabLayout = getView().findViewById(R.id.tab_layout);
    }

    @Override
    protected void intData() {
        //创建导航
        //导航窗口
        String [] str={"首页","爱好","喜欢","收藏","添加","关注"};
        list = new ArrayList<>();
        list.add(new tab1());
        list.add(new tab2());
        list.add(new tab3());
        list.add(new tab4());
        list.add(new tab5());
        list.add(new tab6());

        //设置适配器
        viewPager.setAdapter(new VAdapter(getActivity().getSupportFragmentManager(),list,str));

        //绑定Tab
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    protected void Event() {

    }

猜你喜欢

转载自blog.csdn.net/zhe_guan/article/details/88053383