DrawerLayout + tablayout

来源:互联网 发布:s7 200编程电缆驱动 编辑:程序博客网 时间:2024/06/05 17:30

布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/drawer"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.drawerlayout.MainActivity">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <android.support.design.widget.TabLayout            android:id="@+id/tab"            android:layout_width="match_parent"            android:layout_height="40dp"            app:tabGravity="center"            app:tabIndicatorColor="@color/colorAccent"            app:tabMode="scrollable"            app:tabSelectedTextColor="@color/colorPrimaryDark"            app:tabTextColor="@color/colorPrimary"/>        <android.support.v4.view.ViewPager            android:id="@+id/vp"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_below="@id/tab"></android.support.v4.view.ViewPager>    </RelativeLayout>    <RelativeLayout        android:id="@+id/menu"        android:layout_width="320dp"        android:layout_height="match_parent"        android:layout_gravity="start"        android:background="#fff">        <ImageView            android:id="@+id/img"            android:src="@mipmap/ic_launcher"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <ListView            android:layout_marginTop="50dp"            android:id="@+id/lv"            android:layout_below="@id/img"            android:layout_width="match_parent"            android:layout_height="match_parent"></ListView>    </RelativeLayout></android.support.v4.widget.DrawerLayout>
代码
public class MainActivity extends AppCompatActivity {    private DrawerLayout drawerLayout;    private RelativeLayout mune;    private ListView lv;    private List<String> data;    private TabLayout tab;    private ViewPager viewpager;    private List<String> myTabs=new ArrayList<>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);        mune = (RelativeLayout) findViewById(R.id.menu);        lv = (ListView) findViewById(R.id.lv);        tab = (TabLayout) findViewById(R.id.tab);        viewpager = (ViewPager) findViewById(R.id.vp);        myTabs.add("推荐");        myTabs.add("北京");        myTabs.add("社会");        myTabs.add("娱乐");        myTabs.add("科技");        myTabs.add("民生");        myTabs.add("视频");        viewpager.setAdapter(new MyVpAdapter(getSupportFragmentManager()));        tab.setupWithViewPager(viewpager);        //给侧拉框配置数据        data = new ArrayList<String>();        for (int i=0; i<20; i++){            data.add("item"+i);        }        //给侧拉框创建适配器        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);        lv.setAdapter(adapter);        //listview点击条目事件        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                //关闭侧拉框                drawerLayout.closeDrawer(mune);            }        });    }    class MyVpAdapter extends FragmentPagerAdapter {        public MyVpAdapter(FragmentManager fm) {            super(fm);        }        @Override        public CharSequence getPageTitle(int position) {            return myTabs.get(position);        }        @Override        public Fragment getItem(int position) {            ContentFragment contentFragment=new ContentFragment();            //传递参数            Bundle bundle=new Bundle();//key:string vavle:object            bundle.putString("title",myTabs.get(position));            contentFragment.setArguments(bundle);            return contentFragment;        }        //返回viewPager的加载的页面的数量        @Override        public int getCount() {            return myTabs.size();        }    }

阅读全文
0 0
原创粉丝点击