com.flyco.tablayout.SlidingTabLayout用法

1.添加依赖
在模块build.gradle里面添加compile ‘com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar’
2.创建布局文件:left_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"
    android:orientation="vertical"
    android:background="@color/qmui_config_color_white"
   >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:textSize="20dp"
        android:text="症状"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:textSize="15dp"
        android:text="\t\t\t我的发士大夫士大夫沙发上方式发射点发射点发射点发生发射点发顺丰顺丰顺方式方法打发士大夫"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#f5f5f5"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:textSize="20dp"
        android:text="预防"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:textSize="15dp"
        android:text="\t\t\t我的发士大夫士大夫沙发上方式发射点发射点发射点发生发射点发顺丰顺丰顺方式方法打发士大夫"/>


</LinearLayout>

3.然后新建一个类LeftFragment,这个类是继承自Fragment的:

public class LeftFragement extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_fragment, container, false);
        return view;
    }
}

5.新建activity_main.xml作为主Activity的布局文件,并使用com.flyco.tablayout.SlidingTabLayout和android.support.v4.view.ViewPager组件,并设计页面。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tl="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:background="#ffffff"
    android:orientation="vertical"
    tools:context=".view.MainActivity">

    <com.flyco.tablayout.SlidingTabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="?attr/colorPrimary"
        tl:tl_divider_color="#1AFFFFFF"
        tl:tl_divider_padding="13dp"
        tl:tl_divider_width="1dp"
        tl:tl_indicator_color="#FFFFFF"
        tl:tl_indicator_height="1.5dp"
        tl:tl_indicator_width_equal_title="true"
        tl:tl_tab_padding="22dp"
        tl:tl_tab_space_equal="true"
        tl:tl_textSelectColor="#FFFFFF"
        tl:tl_textUnselectColor="#66FFFFFF"
        tl:tl_underline_color="#1AFFFFFF"
        tl:tl_underline_height="1dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

6.新建MainActivity作为程序的主Activity

public class MainActivity extends AppCompatActivity {

//获取各个组件
    @BindView(R.id.tablayout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    //设置适配器
    private MyPagerAdapter mAdapter;
    //Fragment集合
    private ArrayList<Fragment> mFagments = new ArrayList<>();
    //标题
    private String[] mTitles = {"疾病介绍", "病因", "症状", "预防", "检查", "治疗"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //绑定控件
        ButterKnife.bind(this);

        //初始化tab
        initTab();
    }

    private void initTab() {
        //添加Fragment
        mFagments.add(new LeftFragement());
        mFagments.add(new LeftFragement());
        mFagments.add(new LeftFragement());
        mFagments.add(new LeftFragement());
        mFagments.add(new LeftFragement());
        mFagments.add(new LeftFragement());

        //new一个适配器
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        //设置ViewPager与适配器关联
        viewPager.setAdapter(mAdapter);
        //设置Tab与ViewPager关联
        tabLayout.setViewPager(viewPager);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFagments.size();
        }

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

        @Override
        public Fragment getItem(int position) {
            return mFagments.get(position);
        }
    }
}

7.效果图
这里写图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值