周末就要到了,今天項目中遇到這樣一個Tab,選中tab的背景是個圓角矩形,方向指向器沒有了,這樣普通的TabLayout不能滿足我的要求,可能會想到動態(tài)的去設置選中Tab的背景不就可以了,但是那樣的話太生硬了,沒有動畫效果,其實想想也還比較簡單,今天就簡單的說一說這個YzzTab。效果如下圖:
十多年的甘泉網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。網(wǎng)絡營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整甘泉建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“甘泉網(wǎng)站設計”,“甘泉網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
這里是四個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中顯示的最大數(shù)-1時,會向左右滑動,指示器也會 //跟這滑動,相對靜止,否則指示器滑動,Tab布局不移動 if (position>=mMaxLineNum-1) { scrollContent += leftCop - leftForTabLayout; scrollTo(scrollContent, 0); //這里要重新layout update(); } leftForTabLayout = leftCop; invalidate(); } }
首先,在ViewPage的監(jiān)聽中,positionOffset有時候可能大于1,這點需要注意的,當兩次left的坐標相等 時,我們就不進行繪制了,接下來就是
如何確定left的值了,對于這點我也想了很久,最后終于得出結(jié)論:
int leftCop = (int) (positionOffset*(getMeasuredWidth()/mMaxLineNum)+position*getMeasuredWidth()/mMaxLineNum);
因為當positionOffset的值在向右滑動80%左右的時候getCurrentItem()的值會發(fā)生變化,這點可以試驗一下,所以getCurrentItem()方法不能用了,只能用參數(shù)position.Layout滑動的實際代碼注釋很詳細了,我就不再闡述了,可以試試。在布局滑動了以后,必須要layout,不然View的屬性不會變,點擊沒法應,但是也可以不更新,動態(tài)的告訴用戶點擊的真是Tab,這樣也可以。
private void update() { for (int i = 0; i <mChildCount ; i++) { View v = getChildAt(i); v.setLeft(v.getLeft()+scrollContent); } //必須調(diào)用,不然不會重新layout requestLayout(); }
接下來就是繪制了,ViewGroup是默認不調(diào)用onDraw(Canvas canvas)方法的,原因很簡單,ViewGroup是個容器,主要作用是起承載作用,繪畫就交給子View了,但是還是有辦法讓其調(diào)用該方法的,如下:
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建立關(guān)聯(lián)的方法
/** * 于ViewPager建立聯(lián)系,這里必須先要給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進去,也可以弄個方法,通過用戶動態(tài)添加自己想要的View,都可以實現(xiàn)的。至于監(jiān)聽的話就很簡單了.上面已經(jīng)寫到了。
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/> * 實現(xiàn)背景隨著ViewPager的滑動跟著移動 */ public class YzzTab extends LinearLayout implements ViewPager.OnPageChangeListener { private ViewPager mViewPager; private Paint mPaint; //tab的數(shù)量 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建立聯(lián)系,這里必須先要給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中顯示的最大數(shù)-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); } }
好啦,就介紹這么多,還有待完善,繼續(xù)封裝,完成更強大的功能。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當前名稱:Android自定義TabLayout效果
本文路徑:http://jinyejixie.com/article20/ggijjo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設、響應式網(wǎng)站、用戶體驗、動態(tài)網(wǎng)站、服務器托管、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)