Android_TabLyout的使用

来源:互联网 发布:mac socket 调试工具 编辑:程序博客网 时间:2024/06/16 19:14

//根据版本添加

//添加导包  com.android.support:design:26.0.0-alpha1

//Mainactivity内容

import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {    private List<String> datas = new ArrayList<String>();    private TabLayout tabLayout;    private ViewPager viewPager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);//获取控件        tabLayout = (TabLayout) findViewById(R.id.tas);        viewPager = (ViewPager) findViewById(R.id.vp);//横向滑动的集合        datas.add("推荐");        datas.add("要闻");        datas.add("娱乐");        datas.add("科技");        datas.add("汽车");        datas.add("体育");        datas.add("图片");        datas.add("动漫");        datas.add("社会");        datas.add("游戏");//适配器        vpsp vpsp = new vpsp(getSupportFragmentManager());        viewPager.setAdapter(vpsp);//进行关联        tabLayout.setupWithViewPager(viewPager);//        tabLayout.setupWithViewPager(viewPager);//        tabLayout.setTabsFromPagerAdapter(vpsp);        //虽然过时了但是不能去掉,去掉后        //如果是滑动操作的话没事,但是使用标签点击的时候就不行了。//        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);        //tab的模式如果标签多的话用MODE_SCROLLABLE  少的话用MODE_FIXED    }    class vpsp extends FragmentPagerAdapter{//有参数的构造        public vpsp(FragmentManager fm) {            super(fm);        }//返回选项卡的文本 ,,,添加选项卡        @Override        public CharSequence getPageTitle(int position) {            return datas.get(position);        }//创建fragment对象并返回        @Override        public Fragment getItem(int position) {            content content = new content();            Bundle bundle = new Bundle();            bundle.putString("name",datas.get(position));            content.setArguments(bundle);            return content;        }//返回数量        @Override        public int getCount() {            return datas.size();        }    }}


//Main的布局
<?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"    android:orientation="vertical"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" tools:context="com.example.chenxu20171014.MainActivity">    <android.support.design.widget.TabLayout        android:layout_width="match_parent"        android:layout_height="50dp"        android:id="@+id/tas"        app:tabGravity="center"        app:tabIndicatorColor="@color/colorAccent"        app:tabMode="scrollable"        app:tabSelectedTextColor="@color/colorPrimaryDark"        app:tabTextColor="@color/colorPrimary"        >    </android.support.design.widget.TabLayout>    <android.support.v4.view.ViewPager        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/vp"        android:layout_below="@+id/tas"        >    </android.support.v4.view.ViewPager></LinearLayout>


//content的内容
public class content extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View inflate = View.inflate(getActivity(), R.layout.contentlayout, null);        TextView cte = inflate.findViewById(R.id.cte);        Bundle arguments = getArguments();        String name = arguments.getString("name");        Log.e("chen", "onCreateView: ------"+name );        cte.setText(name);        return inflate;    }}

//contentlayout的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center">    <TextView        android:id="@+id/cte"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="dfxcvnxcv"        android:textColor="@color/colorAccent"/></LinearLayout>





原创粉丝点击