======加依赖包
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();//TabLayout与ViewPager的关联 } //TabLayout与ViewPager的关联 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);//将TabLayout和ViewPager关联起来。 mTabLayout.setTabMode(TabLayout.MODE_FIXED); } //TabLayout与ViewPager的关联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; } //ViewPager与TabLayout绑定后,这里获取到PageTitle就是Tab的Text @Override public CharSequence getPageTitle(int position) { return mTitles[position]; } } }
TabLayout 加ViewPager
最新推荐文章于 2020-09-11 11:02:47 发布