Tablayout的使用

来源:互联网 发布:网络剧投资 编辑:程序博客网 时间:2024/06/08 07:40

Fragment+ViewPager+FragmentPagerAdapter的方式


activity的布局:

<?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"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.zdl.testapp.TabActivity">    <android.support.design.widget.TabLayout        android:id="@+id/tb"        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:tabMode="scrollable" />    <android.support.v4.view.ViewPager        android:id="@+id/vp"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" /></LinearLayout>

使用之前一定要先引入design库


初始化操作

  viewPager = (ViewPager) findViewById(R.id.vp);        tabLayout = (TabLayout) findViewById(R.id.tb);

Fragment布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/tv"        android:layout_centerInParent="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout>



创建Fragment

public class PageFragment extends Fragment {    public static final String ARGS_PAGE = "args_page";    private int mPage;    /**     * 单例模式防止重复创建     * @param page     * @return     */    public static PageFragment newInstance(int page) {        Bundle args = new Bundle();        args.putInt(ARGS_PAGE, page);        PageFragment pageFragment = new PageFragment();        pageFragment.setArguments(args);        return pageFragment;    }    @Override    public void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mPage =  getArguments().getInt(ARGS_PAGE);    }    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragmnt_layout, null, false);        TextView textView = (TextView) view.findViewById(R.id.tv);        textView.setText("第" + mPage + "页");        return view;    }}

adapter:

public class FragmentAdapter extends FragmentPagerAdapter {    private String[] titles = new String[]{"Tab1", "Tab2", "Tab3", "Tab4", "Tab5"};    private Context context;    public FragmentAdapter(FragmentManager fm, Context context) {        super(fm);        this.context = context;    }    /**     * 获取fragment     *     * @param position     * @return     */    @Override    public Fragment getItem(int position) {        return PageFragment.newInstance(position + 1);    }    /**     * 标题长度     *     * @return     */    @Override    public int getCount() {        return titles.length;    }    @Override    public CharSequence getPageTitle(int position) {        return titles[position];    }}

tablayout+viewpager一起使用

 FragmentPagerAdapter fragmentPagerAdapter = new FragmentAdapter(getSupportFragmentManager(), this);        viewPager.setAdapter(fragmentPagerAdapter);        tabLayout.setupWithViewPager(viewPager);


tab没有占满的情况

1.代码

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);    tabLayout.setTabMode(TabLayout.MODE_FIXED);
2布局

<android.support.design.widget.TabLayout        android:id="@+id/tabLayout"        app:tabGravity="fill"        app:tabMode="fixed"        android:layout_width="match_parent"        android:layout_height="wrap_content"        />


原创粉丝点击