Android--TabLayout的用法

来源:互联网 发布:大数据时代监狱管理 编辑:程序博客网 时间:2024/05/16 10:41

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)传值。


0 0