viewpager 加上侧滑初始化

package com.bw.com.lianxix;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.bw.com.lianxix.frag.Frag01;
import com.bw.com.lianxix.frag.Frag02;
import com.bw.com.lianxix.frag.Frag03;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    private ViewPager pager;
    private RadioGroup radiogroup;
    private DrawerLayout drawer;
    private ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawer = findViewById(R.id.drawer);
        listview = findViewById(R.id.left_drawer);
        pager = findViewById(R.id.pager);
        radiogroup = findViewById(R.id.radiogroup);
        final ArrayList list=new ArrayList();
        Frag01 frag01=new Frag01();
        Frag02 frag02=new Frag02();
        Frag03 frag03=new Frag03();
        list.add(frag01);
        list.add(frag02);
        list.add(frag03);
       pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
           @Override
           public Fragment getItem(int i) {
               return list.get(i);
           }

           @Override
           public int getCount() {
               return list.size();
           }

       });
       radiogroup.check(radiogroup.getChildAt(0).getId());
       radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(RadioGroup group, int checkedId) {
               switch (checkedId){
                   case  R.id.button1:
                        pager.setCurrentItem(0);
                       break;
                       case  R.id.button2:
                           pager.setCurrentItem(1);
                       break;
                       case  R.id.button3:
                           pager.setCurrentItem(2);

                       break;
                       default:  drawer.closeDrawer(checkedId);
               }
           }
       });
        List list_text =new ArrayList<>();
        list_text.add("首页");
        list_text.add("视频");
        list_text.add("我的");
        listview.setAdapter(new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,list_text));

    }
}

 

你可能感兴趣的:(viewpager 加上侧滑初始化)