Tablayout+fragment

来源:互联网 发布:计算机英语翻译软件 编辑:程序博客网 时间:2024/06/05 05:15

首先加依赖:

compile 'com.android.support:design:26.0.0-alpha1'

布局

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/drawerlayout"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.bwei.com.toutiao.SecondActivity"><LinearLayout    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <RelativeLayout        android:background="#f00"        android:layout_width="match_parent"        android:layout_height="43dp">        <ImageView          android:id="@+id/left"            android:src="@drawable/a2"            android:layout_width="40dp"            android:layout_height="40dp" />        <TextView            android:text="今日头条"            android:textSize="35sp"            android:gravity="center"            android:textColor="#fff"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:layout_alignParentEnd="true"            android:layout_alignParentBottom="true"            android:layout_alignParentLeft="true"            android:layout_alignParentStart="true" />        <ImageView            android:id="@+id/right"            android:layout_alignParentRight="true"            android:src="@drawable/a3"            android:layout_width="40dp"            android:layout_height="40dp" />    </RelativeLayout>    <HorizontalScrollView        android:layout_width="match_parent"        android:layout_height="wrap_content">    <android.support.design.widget.TabLayout        android:id="@+id/tablyout"        android:layout_width="wrap_content"        android:layout_height="35dp"></android.support.design.widget.TabLayout>    </HorizontalScrollView>    <android.support.v4.view.ViewPager        android:id="@+id/pager"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </android.support.v4.view.ViewPager></LinearLayout><!侧拉布局!> <FrameLayout        android:id="@+id/fraglyout"        android:background="#FFF"        android:layout_gravity="start"        android:layout_width="200dp"        android:layout_height="match_parent"></FrameLayout></android.support.v4.widget.DrawerLayout>Main方法
public class SecondActivity extends AppCompatActivity {           ImageView left;            ImageView right;            TabLayout tabLayout;            ViewPager vp;    DrawerLayout drawerLayout;    List<String>list;    List<Fragment>flist;    String[] array = {"推荐","热点","本地","视频","社会","娱乐","科技","汽车","体育","财经"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);        getSupportFragmentManager().beginTransaction().replace(R.id.fraglyout,new LeftFragment()).commit();        //初始化控件       tabLayout = (TabLayout) findViewById(R.id.tablyout);        vp = (ViewPager) findViewById(pager);        left = (ImageView) findViewById(R.id.left);        right = (ImageView) findViewById(R.id.right);        drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);        left.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                drawerLayout.openDrawer(Gravity.LEFT);            }        });        initData();                }                tabLayout.setupWithViewPager(vp);        MyViewPager adapter = new MyViewPager(getSupportFragmentManager());        vp.setAdapter(adapter);    }    private void initData() {        flist = new ArrayList<>();        flist.add(new Fragmeng1());        flist.add(new Fragmeng2());        flist.add(new Fragmeng3());        flist.add(new Fragmeng4());        flist.add(new Fragmeng5());;        flist.add(new Fragmeng6());        flist.add(new Fragmeng7());        flist.add(new Fragmeng8());        flist.add(new Fragmeng9());        flist.add(new Fragmeng10());    }        public class MyViewPager extends FragmentPagerAdapter{            public MyViewPager(FragmentManager fm) {                super(fm);            }            @Override            public Fragment getItem(int position) {                return flist.get(position);            }            @Override            public int getCount() {                return flist.size();            }            @Override            public CharSequence getPageTitle(int position) {                return array[position];            }        }}










 

原创粉丝点击