当前位置: 代码迷 >> 综合 >> ViewPager,RadioGroup,FragmentManager 详解 四 Fragment,ViewPager 和TabLayout
  详细解决方案

ViewPager,RadioGroup,FragmentManager 详解 四 Fragment,ViewPager 和TabLayout

热度:90   发布时间:2023-12-16 15:44:08.0

这个TabLayout和PagerTabStrip类似,区别在于:

0:布局不同,这个TabLayout 是在ViewPager 的外面,而且是上面.
1:注意这个TabLayout 是design 包里面的.
2:最关键的一个代码就是,而且这个要在设置完了adapter 之后再执行.
 tabLayout.setupWithViewPager(mComicViewPager);


0:创建Fragment

1:创建布局,注意布局里面TabLayout 不是在ViewPager 里面,而是在ViewPager的上面,还有TabLayout 是design 包里面的,所以要引入design的包

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  android:orientation="vertical"  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><android.support.design.widget.TabLayout  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:id="@+id/tablayout"  ></android.support.design.widget.TabLayout><android.support.v4.view.ViewPager  android:layout_width="match_parent"  android:layout_height="0dp"  android:layout_weight="1"  android:id="@+id/viewPager"></android.support.v4.view.ViewPager></LinearLayout>

2:然后设置adapter,

这个adapter 和使用PagerTabStrip 的时候是一样的。都是在getPagerTitle 里面进行Tab的标题的设置。

package tech.androidstudio.tablayoutviewpagerdemo.adapter;import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;import java.util.List;/**  * Created by Kodulf on 2016/3/14.  */ public class TabLayoutFragmentPagerAdapter extends FragmentPagerAdapter {List<Fragment> list;public TabLayoutFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {super(fm);this.list = list;}@Override
    public Fragment getItem(int position) {return list.get(position);}@Override
    public int getCount() {return list.size();}@Override
    public CharSequence getPageTitle(int position) {
// return super.getPageTitle(position);  String ret;switch (position){case 0:ret="财经";break;case 1:ret="体育";break;case 2:ret="娱乐";break;default:ret="未命名";break;}return ret;}
}


3:MainActivity

最关键的一个代码就是,而且这个要在设置完了adapter 之后再执行.
 tabLayout.setupWithViewPager(mComicViewPager);

package tech.androidstudio.tablayoutviewpagerdemo;import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;import java.util.ArrayList;
import java.util.List;import tech.androidstudio.tablayoutviewpagerdemo.adapter.TabLayoutFragmentPagerAdapter;public class MainActivity extends AppCompatActivity {private TabLayout mTabLayout;private ViewPager mViewPager;@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTabLayout = (TabLayout)findViewById(R.id.tablayout);mViewPager = (ViewPager)findViewById(R.id.viewPager);List<Fragment> mList = new ArrayList<Fragment>();mList.add(new FirstFragment());mList.add(new SecondFragment());mList.add(new ThirdFragment());TabLayoutFragmentPagerAdapter adapter = new TabLayoutFragmentPagerAdapter(getSupportFragmentManager(),mList);//一定要先设置adapter ,然后在使用命令mTabLayout.setupWithViewPager(mViewPager);  mViewPager.setAdapter(adapter);//这一步是最关键,将tablayout viewpager 连接起来了  mTabLayout.setupWithViewPager(mViewPager);}
}


  相关解决方案