TabLayout 加ViewPager

======加依赖包
compile 'com.android.support:design:24.2.1'
======权限
<uses-permission android:name="android.permission.INTERNET" />
 
======主代码加载布局
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.administrator.jufanzhibo.R;
import java.util.ArrayList;
import java.util.List;

public class BangdanActivity extends AppCompatActivity implements View.OnClickListener {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private LayoutInflater mInflater;
    private List<String> mTitleList = new ArrayList<>();//页卡标题集合
    private View view1, view2;//页卡视图
    private List<View> mViewList = new ArrayList<>();//页卡视图集合
    private ImageView fhb_gb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bangdan);
        mTabLayout = (TabLayout)findViewById(R.id.tablayout);
        mViewPager = (ViewPager)findViewById(R.id.viewpager);
        mInflater = LayoutInflater.from(BangdanActivity.this);
        view1 = mInflater.inflate(R.layout.activity_main, null);
        view2 = mInflater.inflate(R.layout.activity_main, null);

        //添加页卡视图
        mViewList.add(view1);
        mViewList.add(view2);

        //添加页卡标题
        mTitleList.add("周榜");
        mTitleList.add("月榜");


        mTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置tab模式,当前为系统默认模式
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(0)));//添加tab选项卡
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(1)));


        MyPagerAdapter mAdapter = new MyPagerAdapter(mViewList);
        mViewPager.setAdapter(mAdapter);//给ViewPager设置适配器
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
        mTabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器

    }

   
    //ViewPager适配器
    class MyPagerAdapter extends PagerAdapter {
        private List<View> mViewList;

        public MyPagerAdapter(List<View> mViewList) {
            this.mViewList = mViewList;
        }

        @Override
        public int getCount() {
            return mViewList.size();//页卡数
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;//官方推荐写法
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));//添加页卡
            return mViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));//删除页卡
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);//页卡标题
        }

    }

}
 
======布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_height="35dp"
        android:layout_width="match_parent"
        app:tabIndicatorColor="@color/fense"
        app:tabSelectedTextColor="@color/fense"
        app:tabTextColor="@color/heise"/>
<android.support.v4.view.ViewPager
        android:layout_marginTop="2dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewpager"></android.support.v4.view.ViewPager>
</LinearLayout>
 
 
 
 
======主代码加载布局Fragment
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.widget.ImageView;
import com.example.administrator.jufanzhibo.R;
import com.example.administrator.jufanzhibo.fragment.Yuebang;
import com.example.administrator.jufanzhibo.fragment.Zhoubang;
import java.util.ArrayList;

public class BangdanActivity extends FragmentActivity{

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private ImageView fhb_gb;
    private ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bangdan);
        initView();//TabLayoutViewPager的关联


    }
    //TabLayoutViewPager的关联
    private void initView() {
        mTabLayout = (TabLayout) findViewById(R.id.tablayout);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        fragments = new ArrayList<>();
        fragments.add(new Zhoubang());
        fragments.add(new Yuebang());
        MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(adapter);//ViewPager设置适配器
        mTabLayout.setupWithViewPager(mViewPager);//TabLayoutViewPager关联起来。
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
    }
    
    //TabLayoutViewPager的关联adapter
    public class MyAdapter extends FragmentPagerAdapter {
        private String[] mTitles = new String[]{"周榜", "总榜"};
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }
        @Override
        public int getCount() {
            return mTitles.length;
        }
        //ViewPagerTabLayout绑定后,这里获取到PageTitle就是TabText
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值