Android--TabLayout的用法

MD设计风格中,可以用TabLayout + ViewPager + Fragment + TabsFragmentAdapter来实现选项卡界面,如下图:


MainActivity代码:

public class TabsActivity extends AppCompatActivity {

    private ViewPager mViewPager = null;
    private TabsFragmentAdapter mAdapter = null;
    private TabLayout mTabLayout = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabs);

        initView();

    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mAdapter = new TabsFragmentAdapter(getSupportFragmentManager(), this);

        mViewPager.setAdapter(mAdapter);

        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);

        mTabLayout.setupWithViewPager(mViewPager);
    }
}

MainActivity.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ffffff"
        />
</LinearLayout>

Fragment代码:

public class TabsFragment extends Fragment {

    private static final String ARG_PARAM1 = "mPages";

    private int mPages;

    public TabsFragment() {
    }

    public static TabsFragment newInstance(int mPages) {
        TabsFragment fragment = new TabsFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, mPages);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPages = getArguments().getInt(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout, container, false);
        TextView tv = (TextView) view.findViewById(R.id.fragment_text_view);
        tv.setText("This is" + mPages + "page");
        return view;
    }


}

Fragment.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >
    <TextView
        android:id="@+id/fragment_text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@color/colorAccent"
        />

</LinearLayout>

FragmentAdapter代码:

public class TabsFragmentAdapter extends FragmentPagerAdapter{

    public final int COUNT = 5;
    private String[] titles = new String[]{"Tab1", "Tab2", "Tab3", "Tab4", "Tab5"};
    private Context context;

    public TabsFragmentAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        return TabsFragment.newInstance(position+1);
    }

    @Override
    public int getCount() {
        return COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

注意:

1.Fragment+FragmentAdapter+ViewPager的用法。

2.将ViewPager嵌入到TabLayout:

mTabLayout.setupWithViewPager(mViewPager);
3.注意Fragment中newInstance方法和args(Bundle)传值。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值