自定义tablayout,好用

这个类中只是2个tablayout,可以根据布局不断的拓展,可以设置背景,文字颜色,等等自定义,其中还能设置tablayout下的进度条:

package com.nick.albummanagement.hidden;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nick.albummanagement.R;

/**
 *
 *
 * 作者:jiuqunZhang on 2018/8/13 19:45
 * 邮箱:[email protected]
 */

public class TabLayoutViewIndicator implements ViewPager.OnPageChangeListener, View.OnClickListener{
    private Context context;
    private ViewPager viewPager;
    private LinearLayout dotLayout;
    private TextView mTvBlackList;
    private TextView mTvHistory;
    private LinearLayout mLlBgIndicator;
    private int mFlag;

    @SuppressLint("ResourceAsColor")
    public TabLayoutViewIndicator(Context context, ViewPager viewPager, LinearLayout dotLayout, int flag) {//flag留作以后拓展用 =0表示是callblocker
        this.context = context;
        this.viewPager = viewPager;
        this.dotLayout = dotLayout;
        mFlag = flag;

        View view = LayoutInflater.from(context).inflate(R.layout.tablayout_viewindicaton, null, false);
        mTvBlackList = (TextView) view.findViewById(R.id.tv_all);
        mLlBgIndicator = (LinearLayout) view.findViewById(R.id.ll_bg_indicator);
        mTvHistory = (TextView) view.findViewById(R.id.tv_missed);
        mTvBlackList.setOnClickListener(this);
        mTvHistory.setOnClickListener(this);

        mTvBlackList.setBackgroundResource(R.mipmap.cb_blacklist_selected);
        mTvBlackList.setTextColor(R.color.text_color_blue);

        mTvHistory.setBackgroundResource(R.mipmap.cb_history_normal);
        mTvHistory.setTextColor(R.color.white);

        if (mFlag == 0) {
            mTvBlackList.setText(R.string.tab_photos);
            mTvHistory.setText(R.string.tab_folder);
        }
        //为LinearLayout添加ImageView
        if (dotLayout != null) {
            dotLayout.addView(view);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            mTvBlackList.setBackgroundResource(R.mipmap.cb_blacklist_selected);
            mTvBlackList.setTextColor(R.color.text_color_blue);
            mTvHistory.setBackgroundResource(R.mipmap.cb_history_normal);
            mTvHistory.setTextColor(R.color.white);

//            mTvBlackList.setTextColor();
//            mTvHistory.setTextColor(Color.parseColor("#000000"));
        } else {
            mTvBlackList.setBackgroundResource(R.mipmap.cb_blacklist_normal);
            mTvBlackList.setTextColor(R.color.white);

            mTvHistory.setBackgroundResource(R.mipmap.cb_history_selected);
            mTvBlackList.setTextColor(R.color.text_color_blue);

//            mTvBlackList.setTextColor(Color.parseColor("#000000"));
//            mTvHistory.setTextColor(Color.parseColor("#000000"));
        }
    }



    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_all) {
            viewPager.setCurrentItem(0);
        } else {
            viewPager.setCurrentItem(1);
        }
    }
}

layout是:

tablayout_viewindicaton.xml  :



    

        

        

            

        

    



 

调用:

private TabLayoutViewIndicator mViewIndicator;
mViewIndicator = new TabLayoutViewIndicator(this, mVpMain, mLlCallblockerIndicator, 0);
mVpMain.addOnPageChangeListener(mViewIndicator);

 

你可能感兴趣的:(自定义tablayout,好用)