Android--------TableLayout和Viewpager联动

1.首先导入TableLayout依赖:

compile 'com.android.support:design:25.1.0'
然后是定义TableLayout和ViewPager
<android.support.design.widget.TabLayout
    android:id="@+id/tab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabIndicatorColor="@color/colorAccent"
    app:tabIndicatorHeight="1dp"
    app:tabTextColor="#d39900ff"
    app:tabSelectedTextColor="#000"
    app:tabMode="scrollable"
    >

android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/vp"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

android.support.v4.view.ViewPager>


3.定义一个Pager的适配器类 继承PagerAdapter
public class PagerAdapter extends android.support.v4.view.PagerAdapter {
    private ArrayList mlist;

    public PagerAdapter(ArrayList mlist) {
        this.mlist = mlist;
    }

    @Override
    public int getCount() {
        return mlist==null?0:mlist.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = mlist.get(position);
        container.addView(view);
        return view;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mlist.get(position));
    }


}

4.主界面
public class MainActivity extends AppCompatActivity {
    private TabLayout tb;
    private ViewPager mvp;
    private PagerAdapter pd;
    private ArrayList mlist = new ArrayList<>();
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tb = (TabLayout) findViewById(R.id.tab);
        mvp = (ViewPager) findViewById(R.id.vp);

       tb.addTab(tb.newTab().setText("周一"));
        tb.addTab(tb.newTab().setText("周二"));
        tb.addTab(tb.newTab().setText("周三"));
        tb.addTab(tb.newTab().setText("周四"));
        tb.addTab(tb.newTab().setText("周五"));
        tb.addTab(tb.newTab().setText("周六"));
        tb.addTab(tb.newTab().setText("周日"));

        //得到各个ViewPager的布局
        LayoutInflater lf = LayoutInflater.from(this);
        View v1 = lf.inflate(R.layout.item1, null);
        View v2 = lf.inflate(R.layout.item2, null);
        View v3 = lf.inflate(R.layout.item3, null);
        View v4 = lf.inflate(R.layout.item4, null);
        View v5 = lf.inflate(R.layout.item5, null);
        View v6 = lf.inflate(R.layout.item6, null);
        View v7 = lf.inflate(R.layout.item7, null);
        mlist.add(v1);
        mlist.add(v2);
        mlist.add(v3);mlist.add(v4);mlist.add(v5);
        mlist.add(v6);mlist.add(v7);
        pd = new PagerAdapter(mlist);
        mvp.setAdapter(pd);
      
        //Viewpager的监听事件:
        mvp.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tb));

        //TableLayout的监听事件
        tb.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                int position = tab.getPosition();
                mvp.setCurrentItem(position);

         }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }
    }
Android--------TableLayout和Viewpager联动_第1张图片 Android--------TableLayout和Viewpager联动_第2张图片




你可能感兴趣的:(Android--------TableLayout和Viewpager联动)