Android 选项卡页面实现

来源:互联网 发布:数据管家 编辑:程序博客网 时间:2024/06/14 21:50

首先要添加项目的库依赖:

compile 'com.android.support:design:26.0.0-alpha1'

一、MainActivity页面代码:

package com.tabfragment;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;public class MainActivity extends AppCompatActivity {    private TabLayout tabLayout;    private ViewPager vpContent;    private List<Fragment> fragmentList = new ArrayList<>();    private List<String> titleList = new ArrayList<>();    private VpContentAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initWidget();    }    private void initWidget() {        tabLayout = (TabLayout) findViewById(R.id.tl_title);        vpContent = (ViewPager) findViewById(R.id.vp_content);        //传递参数        Bundle bundle1 = new Bundle();        bundle1.putString(ContentFragment.TAG, "111111111");        fragmentList.add(ContentFragment.getInstance(bundle1));        titleList.add("第一页");        //传递参数        Bundle bundle2 = new Bundle();        bundle2.putString(ContentFragment.TAG, "222222222");        fragmentList.add(ContentFragment.getInstance(bundle2));        titleList.add("第二页");        //传递参数        Bundle bundle3 = new Bundle();        bundle3.putString(ContentFragment.TAG, "33333333");        fragmentList.add(ContentFragment.getInstance(bundle3));        titleList.add("第三页");        //传递参数        Bundle bundle4 = new Bundle();        bundle4.putString(ContentFragment.TAG, "44444444");        fragmentList.add(ContentFragment.getInstance(bundle4));        titleList.add("第四页");        //传递参数        Bundle bundle5 = new Bundle();        bundle5.putString(ContentFragment.TAG, "5555555");        fragmentList.add(ContentFragment.getInstance(bundle5));        titleList.add("第五页");        //传递参数        Bundle bundle6 = new Bundle();        bundle6.putString(ContentFragment.TAG, "666666");        fragmentList.add(ContentFragment.getInstance(bundle6));        titleList.add("第六页");        tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorAccent));        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);        tabLayout.setupWithViewPager(vpContent);        adapter = new VpContentAdapter(getSupportFragmentManager(), fragmentList, titleList);        vpContent.setAdapter(adapter);        vpContent.setOffscreenPageLimit(fragmentList.size() - 1);        vpContent.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));    }}
二、activity_mian布局文件代码是:

<RelativeLayout 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"    tools:context="com.tabfragment.MainActivity">        <android.support.design.widget.TabLayout        android:id="@+id/tl_title"        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:tabSelectedTextColor="@color/colorAccent"        app:tabTextColor="@color/colorPrimary" />    <android.support.v4.view.ViewPager        android:id="@+id/vp_content"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@+id/tl_title" /></RelativeLayout>
三、VpContentAdapter适配器的代码是:

package com.tabfragment;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import java.util.ArrayList;import java.util.List;public class VpContentAdapter extends FragmentPagerAdapter {    private List<Fragment> fragmentList = new ArrayList<>();    private List<String> titleList = new ArrayList<>();    public VpContentAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList) {        super(fm);        this.fragmentList = fragmentList;        this.titleList = titleList;    }    @Override    public Fragment getItem(int position) {        return fragmentList.get(position);    }    @Override    public int getCount() {        return titleList.size();    }    @Override    public CharSequence getPageTitle(int position) {        return titleList.get(position);    }}
四、ContentFragment页面的代码:

package com.tabfragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class ContentFragment extends Fragment {    public static final String TAG = "tag";    private String mTextId = "000000";    private TextView tvContent;    public static ContentFragment getInstance(Bundle bundle) {        ContentFragment contentFragment = new ContentFragment();        contentFragment.setArguments(bundle);        return contentFragment;    }    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_content, null, false);        //获取传递过来的参数        mTextId = getArguments().getString(TAG);        tvContent = (TextView) view.findViewById(R.id.content);        tvContent.setText(mTextId);        return view;    }}