TabLayout+ViewPager+Fragment实现自定义title栏切换页面

 前言

   网上很多TAbLayout+ViewPager实现tile也动态滑动的功能。但是呢,都是基本标题+下滑游标实现的。title栏是固定格式,扩展性不高。我们这里使用自己的tite栏。

 效果图

    

可以看到 我们跟常规的标题跟下划线的区别。title栏完全是自己定义界面。

导包

implementation 'com.android.support:design:28.0.0'

布局文件

activity_main.xml。这里Tablayout有两个属性需要设置

  

 app:tabMode="scrollable"   //当title的内容过多时,我们采用滚动模式
 app:tabIndicatorHeight="0dp" //设置0dp 不显示下滑游标
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/tabLayout"
        app:tabMode="scrollable"
        app:tabIndicatorHeight="0dp"
        >
    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/viewpager"
        >
    </android.support.v4.view.ViewPager>
</LinearLayout>

布局方面跟常规没多大区别

主Activity

package tv.phone.tablayout.demo;

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 android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private ArrayList<String> titles;
    private ArrayList<Fragment> fragmentList;

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

    private void initData() {

        titles = new ArrayList<>();
        fragmentList = new ArrayList<>();
        //模拟数据
        for(int x=0;x<10;x++){
            titles.add((x+2000)+"");
            fragmentList.add(new MyFragment((x+2000)+""));
            TabLayout.Tab tab = tabLayout.newTab();
            tab.setCustomView(getTabView(x));

            //默认第一个选中状态
            if(x==0){
                tabLayout.addTab(tab,true);
                TextView tv_tab = tab.getCustomView().findViewById(R.id.tv_title_epg_tab);
                tv_tab.setTextColor(getResources().getColor(R.color.color_white));
                tv_tab.setBackgroundResource(R.drawable.shape_yuanjiao_bg_shenlan);
            }else{
                tabLayout.addTab(tab,false);
            }
        }

        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragmentList, titles);
        viewPager.setAdapter(myPagerAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //选中的相关操作
                TextView tv_tab = tab.getCustomView().findViewById(R.id.tv_title_epg_tab);
                tv_tab.setTextColor(getResources().getColor(R.color.color_white));
                tv_tab.setBackgroundResource(R.drawable.shape_yuanjiao_bg_shenlan);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //未选中的相关操作
                TextView tv_tab = tab.getCustomView().findViewById(R.id.tv_title_epg_tab);
                tv_tab.setTextColor(getResources().getColor(R.color.color_shenhui));
                tv_tab.setBackgroundResource(R.drawable.shape_yuanjiao);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    public View getTabView(int position) {
        View v = LayoutInflater.from(this).inflate(R.layout.layout_epgtab, null);
        TextView tv = (TextView) v.findViewById(R.id.tv_title_epg_tab);
        tv.setText(titles.get(position));
        return v;
    }
    private void initView() {
        viewPager = findViewById(R.id.viewpager);
        tabLayout = findViewById(R.id.tabLayout);
    }
}

  layout_epgtab.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textColor="#6a6a6a"
    android:paddingTop="3dp"
    android:paddingBottom="3dp"
    android:background="@drawable/shape_yuanjiao"
    android:id="@+id/tv_title_epg_tab"
    android:textSize="14sp"
    >
</TextView>

 Frgment的代码

package tv.phone.tablayout.demo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
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;

import java.util.ArrayList;

/**
 * author:Maxence
 * date: On 2019/4/10
 */

@SuppressLint("ValidFragment")
public class MyFragment extends Fragment {

    private String titles="";
    @SuppressLint("ValidFragment")
    public MyFragment(String titles){
            this.titles=titles;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        TextView tv=new TextView(getContext());
        tv.setText(titles);
        return tv;
    }
}

  MyViewPagerAdapter

   

package tv.phone.tablayout.demo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * author:Maxence
 * date: On 2019/4/10
 */

public class MyPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> mList;   //fragment列表
    private List<String> titles;       //tab名的列表

    public MyPagerAdapter(FragmentManager fm, List<Fragment> mList, List<String> titles) {
        super(fm);
        this.mList=mList;
        this.titles=titles;
    }


    @Override
    public Fragment getItem(int i) {

        if(mList==null){
            return null;
        }
        return mList.get(i);
    }

    @Override
    public int getCount() {
        if(mList==null){
            return 0;
        }
        return mList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }

}

 一些背景图

shape_yuanjiao.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
 <!-- 一个圆角背景框  用于 回看 的表框 -->
    <solid
        android:color="@android:color/transparent" />
    <!-- 边缘线条颜色 -->
    <stroke
        android:width="1dp"
        android:color="#6a6a6a"
        />
    <!-- 圆角的幅度 -->
    <corners
        android:topLeftRadius="12dip"
        android:topRightRadius="12dip"
        android:bottomLeftRadius="12dip"
        android:bottomRightRadius="12dip" />

</shape>

shape_yuanjiao_bg_shenlan.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
 <!-- 一个圆角背景框  用于 回看 的表框 -->   
    
    
    <solid
        android:color="@color/color_shenlan" />
    <!-- 边缘线条颜色 -->
    <stroke
        android:width="0.1dp"
        android:color="@color/color_shenlan"
        />
    <!-- 圆角的幅度 -->
    <corners
        android:topLeftRadius="12dip"
        android:topRightRadius="12dip"
        android:bottomLeftRadius="12dip"
        android:bottomRightRadius="12dip" />

</shape>

  OK。这样就搞定了


版权声明:本文为u012560369原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。