侧边栏以及Fragment切换

布局文件
<android.support.v4.widget.DrawerLayout 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”
android:id="@+id/drawer"
>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="9"
    ></android.support.v4.view.ViewPager>
<RadioGroup
    android:id="@+id/radiogroup"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    >
    <RadioButton
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:button="@null"
        android:text="首页"
        android:gravity="center"
        android:layout_weight="1"
        />
    <RadioButton
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:button="@null"
        android:text="小视频"
        android:gravity="center"
        android:layout_weight="1"
        />
    <RadioButton
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:button="@null"
        android:text="我的"
        android:gravity="center"
        android:layout_weight="1"
        />
</RadioGroup>
</LinearLayout>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#f00"
    >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</FrameLayout></android.support.v4.widget.DrawerLayout>

Activity界面

private ViewPager viewpager;
private RadioGroup radiogroup;
private DrawerLayout drawer;
private ListView listview;
private ArrayList<String> strings;
private ArrayList<Fragment> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //获取控件
    viewpager = findViewById(R.id.viewpager);
    radiogroup = findViewById(R.id.radiogroup);
    drawer = findViewById(R.id.drawer);
    listview = findViewById(R.id.listview);
    //给侧拉栏创建集合
    strings = new ArrayList<>();
    strings.add("首页");
    strings.add("视频");
    strings.add("讲讲");
    strings.add("我的");
    //设置适配器
    listview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strings));
    //设置条目点击事件根据下标进行跳转
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            viewpager.setCurrentItem(position);
            drawer.closeDrawers();
        }
    });
    list = new ArrayList<>();
    list.add(new FragmentA());
    list.add(new FragmentB());
    list.add(new FragmentC());
    list.add(new FragmentD());

    //设置适配器
    viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int i) {
            return list.get(i);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    });
    radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.btn1:
                    viewpager.setCurrentItem(0);
                case R.id.btn2:
                    viewpager.setCurrentItem(1);
                case R.id.btn3:
                    viewpager.setCurrentItem(2);
                case R.id.btn4:
                    viewpager.setCurrentItem(3);
                    break;
            }
        }
    });
}

}

猜你喜欢

转载自blog.csdn.net/ytw737006109/article/details/84931614