您好,登錄后才能下訂單哦!
周末就要到了,今天項目中遇到這樣一個Tab,選中tab的背景是個圓角矩形,方向指向器沒有了,這樣普通的TabLayout不能滿足我的要求,可能會想到動態的去設置選中Tab的背景不就可以了,但是那樣的話太生硬了,沒有動畫效果,其實想想也還比較簡單,今天就簡單的說一說這個YzzTab。效果如下圖:
這里是四個Tab,一版只顯示3個,這里假設有num個Tab,當滑動到第3個時,這里就需要考慮如何讓TabLayout和指示器一起移動呢?
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (positionOffset>1){ return; } int leftCop = (int) (positionOffset*(getMeasuredWidth()/mMaxLineNum)+position*getMeasuredWidth()/mMaxLineNum); if (leftCop!=leftForTabLayout){ //這里要做判斷是否滑動,當選擇的位置大于TabLayout中顯示的最大數-1時,會向左右滑動,指示器也會 //跟這滑動,相對靜止,否則指示器滑動,Tab布局不移動 if (position>=mMaxLineNum-1) { scrollContent += leftCop - leftForTabLayout; scrollTo(scrollContent, 0); //這里要重新layout update(); } leftForTabLayout = leftCop; invalidate(); } }
首先,在ViewPage的監聽中,positionOffset有時候可能大于1,這點需要注意的,當兩次left的坐標相等 時,我們就不進行繪制了,接下來就是
如何確定left的值了,對于這點我也想了很久,最后終于得出結論:
int leftCop = (int) (positionOffset*(getMeasuredWidth()/mMaxLineNum)+position*getMeasuredWidth()/mMaxLineNum);
因為當positionOffset的值在向右滑動80%左右的時候getCurrentItem()的值會發生變化,這點可以試驗一下,所以getCurrentItem()方法不能用了,只能用參數position.Layout滑動的實際代碼注釋很詳細了,我就不再闡述了,可以試試。在布局滑動了以后,必須要layout,不然View的屬性不會變,點擊沒法應,但是也可以不更新,動態的告訴用戶點擊的真是Tab,這樣也可以。
private void update() { for (int i = 0; i <mChildCount ; i++) { View v = getChildAt(i); v.setLeft(v.getLeft()+scrollContent); } //必須調用,不然不會重新layout requestLayout(); }
接下來就是繪制了,ViewGroup是默認不調用onDraw(Canvas canvas)方法的,原因很簡單,ViewGroup是個容器,主要作用是起承載作用,繪畫就交給子View了,但是還是有辦法讓其調用該方法的,如下:
setWillNotDraw(false);
這就告訴該容器,需要繪制;
接下來就是繪制指向器和選中背景了,一個圓角矩形和一條線,比較簡單,我就不再詳細說明了。
@Override protected void onDraw(Canvas canvas) { //left = getMeasuredWidth() / mChildCount * mSelectPosition; super.onDraw(canvas); mPaint.setColor(Color.GREEN); int top = getMeasuredHeight() / 4; int right = leftForTabLayout + getMeasuredWidth() / mMaxLineNum; int bottom = getMeasuredHeight() - getMeasuredHeight() / 4; RectF rectF = new RectF(leftForTabLayout, top, right, bottom); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); canvas.drawRoundRect(rectF, 10, 10, mPaint); mPaint.setColor(Color.RED); mPaint.setStrokeWidth(5); canvas.drawLine(leftForTabLayout,getMeasuredHeight()-5,right,getMeasuredHeight()-5,mPaint); }
接下來介紹建與ViewPager建立關聯的方法
/** * 于ViewPager建立聯系,這里必須先要給ViewPager設置Adapter * * @param viewPager */ public void setUpWithViewPager(ViewPager viewPager) { mViewPager = viewPager; mChildCount = viewPager.getAdapter().getCount(); mSelectPosition = viewPager.getCurrentItem(); viewPager.setOnPageChangeListener(this); }
初始化的方法
/** * 為Tab添加View */ private void init() { setWillNotDraw(false); mPaint = new Paint(); for (int i = 0; i < mChildCount; i++) { final TextView tv = new TextView(getContext()); int w = getMeasuredWidth()/mMaxLineNum; LinearLayout.LayoutParams lp = new LayoutParams(w, ViewGroup.LayoutParams.MATCH_PARENT); tv.setText("tab" + i); tv.setGravity(Gravity.CENTER); tv.setLayoutParams(lp); final int finalI = i; tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (monTabSelecterListener != null){ monTabSelecterListener.selecter(finalI,tv); } } }); addView(tv); } }
這里只是很簡單的加了幾個TextView進去,也可以弄個方法,通過用戶動態添加自己想要的View,都可以實現的。至于監聽的話就很簡單了.上面已經寫到了。
YzzTab的代碼
package a6he.android.yzz.com.mytablayout; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.Switch; import android.widget.TextView; /** * Created by yzz on 2017/2/24 0024. * <p/> * 實現背景隨著ViewPager的滑動跟著移動 */ public class YzzTab extends LinearLayout implements ViewPager.OnPageChangeListener { private ViewPager mViewPager; private Paint mPaint; //tab的數量 private int mChildCount; //tab選中的位置 private int mSelectPosition; //繪制指向器的左頂點 private int leftForTabLayout = 0; private int leftForInvidator = 0; private int mMaxLineNum = 3; private int scrollContent = 0; private onTabSelecterListener monTabSelecterListener; public YzzTab(Context context) { super(context); } public YzzTab(Context context, AttributeSet attrs) { super(context, attrs); } public YzzTab(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onFinishInflate() { super.onFinishInflate(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); init(); } /** * 于ViewPager建立聯系,這里必須先要給ViewPager設置Adapter * * @param viewPager */ public void setUpWithViewPager(ViewPager viewPager) { mViewPager = viewPager; mChildCount = viewPager.getAdapter().getCount(); mSelectPosition = viewPager.getCurrentItem(); viewPager.setOnPageChangeListener(this); } /** * 為Tab添加View */ private void init() { setWillNotDraw(false); mPaint = new Paint(); for (int i = 0; i < mChildCount; i++) { final TextView tv = new TextView(getContext()); int w = getMeasuredWidth()/mMaxLineNum; LinearLayout.LayoutParams lp = new LayoutParams(w, ViewGroup.LayoutParams.MATCH_PARENT); tv.setText("tab" + i); tv.setGravity(Gravity.CENTER); tv.setLayoutParams(lp); final int finalI = i; tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (monTabSelecterListener != null){ monTabSelecterListener.selecter(finalI,tv); } } }); addView(tv); } } @Override protected void onDraw(Canvas canvas) { //left = getMeasuredWidth() / mChildCount * mSelectPosition; super.onDraw(canvas); mPaint.setColor(Color.GREEN); int top = getMeasuredHeight() / 4; int right = leftForTabLayout + getMeasuredWidth() / mMaxLineNum; int bottom = getMeasuredHeight() - getMeasuredHeight() / 4; RectF rectF = new RectF(leftForTabLayout, top, right, bottom); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); canvas.drawRoundRect(rectF, 10, 10, mPaint); mPaint.setColor(Color.RED); mPaint.setStrokeWidth(5); canvas.drawLine(leftForTabLayout,getMeasuredHeight()-5,right,getMeasuredHeight()-5,mPaint); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (positionOffset>1){ return; } int leftCop = (int) (positionOffset*(getMeasuredWidth()/mMaxLineNum)+position*getMeasuredWidth()/mMaxLineNum); if (leftCop!=leftForTabLayout){ //這里要做判斷是否滑動,當選擇的位置大于TabLayout中顯示的最大數-1時,會向左右滑動,指示器也會 //跟這滑動,相對靜止,否則指示器滑動,Tab布局不移動 if (position>=mMaxLineNum-1) { scrollContent += leftCop - leftForTabLayout; scrollTo(scrollContent, 0); //這里要重新layout update(); } leftForTabLayout = leftCop; invalidate(); } } private void update() { for (int i = 0; i <mChildCount ; i++) { View v = getChildAt(i); v.setLeft(v.getLeft()+scrollContent); } requestLayout(); } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { switch (state){ } } public void setmMaxLineNum(int mMaxLineNum) { this.mMaxLineNum = mMaxLineNum; } public void setonTabSelecterListener(onTabSelecterListener monTabSelecterListener) { this.monTabSelecterListener = monTabSelecterListener; } interface onTabSelecterListener{ void selecter(int position,View view); } }
好啦,就介紹這么多,還有待完善,繼續封裝,完成更強大的功能。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。