标签控件,下方带有滑动块

    xiaoxiao2021-12-01  24

    /** * 标签控件,下方带有滑动块 */ public class TabsView extends HorizontalScrollView { private LinearLayout.LayoutParams defaultTabLayoutParams; private LinearLayout.LayoutParams expandedTabLayoutParams; // @formatter:off private static final int[] ATTRS = new int[]{ android.R.attr.textSize, android.R.attr.textColor }; // @formatter:on private LinearLayout tabsContainer; private Paint rectPaint; private Paint dividerPaint; private String[] tabs; private int tabCount; private boolean shouldExpand = true; private int scrollOffset = 52; private int indicatorHeight = 2; private int underlineHeight = 2; private int dividerPadding = 12; private int tabPadding = 24; private int dividerWidth = 1; private int currentPosition = 0; private float currentPositionOffset = 0f; private int lastScrollX = 0; private OnRefreshListener listener ; private int selectedTabTextColor = 0xFF212120; private int tabBackgroundResId = R.drawable.background_tab; private Typeface tabTypeface = null; private int tabTypefaceStyle = Typeface.NORMAL; private int tabTextSize = 15; private int tabTextColor = 0xFF999999; private int indicatorColor = 0xFF781378; private int underlineColor = 0x1A000000; private int dividerColor = 0x00000000; private int intervalWidth = 30; public TabsView(Context context) { this(context, null); } public TabsView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public TabsView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setFillViewport(true); setWillNotDraw(false); tabsContainer = new LinearLayout(context); tabsContainer.setOrientation(LinearLayout.HORIZONTAL); tabsContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); addView(tabsContainer); DisplayMetrics dm = getResources().getDisplayMetrics(); scrollOffset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, scrollOffset, dm); indicatorHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, indicatorHeight, dm); underlineHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, underlineHeight, dm); dividerPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerPadding, dm); tabPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, tabPadding, dm); dividerWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerWidth, dm); tabTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, tabTextSize, dm); intervalWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, intervalWidth, dm); // get system attrs (android:textSize and android:textColor) TypedArray a = context.obtainStyledAttributes(attrs, ATTRS); tabTextSize = a.getDimensionPixelSize(0, tabTextSize); tabTextColor = a.getColor(1, tabTextColor); a.recycle(); // get custom attrs a = context.obtainStyledAttributes(attrs, R.styleable.PagerSlidingTabStrip); indicatorColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsIndicatorColor, indicatorColor); underlineColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsUnderlineColor, underlineColor); dividerColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsDividerColor, dividerColor); indicatorHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsIndicatorHeight, indicatorHeight); underlineHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsUnderlineHeight, underlineHeight); dividerPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsDividerPadding, dividerPadding); tabPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsTabPaddingLeftRight, tabPadding); tabBackgroundResId = a.getResourceId(R.styleable.PagerSlidingTabStrip_pstsTabBackground, tabBackgroundResId); shouldExpand = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsShouldExpand, shouldExpand); scrollOffset = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsScrollOffset, scrollOffset); selectedTabTextColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsSelectedTabTextColor, selectedTabTextColor); intervalWidth = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_intervalWidth, intervalWidth); a.recycle(); rectPaint = new Paint(); rectPaint.setAntiAlias(true); rectPaint.setStyle(Paint.Style.FILL); dividerPaint = new Paint(); dividerPaint.setAntiAlias(true); dividerPaint.setStrokeWidth(dividerWidth); defaultTabLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); expandedTabLayoutParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f); } public void setTabs(String[] tabs) { this.tabs = tabs; notifyDataSetChanged(); } private void notifyDataSetChanged() { tabsContainer.removeAllViews(); tabCount = tabs.length; for (int i = 0; i < tabCount; i++) { addTextTab(i, tabs[i]); } updateTabStyles(); getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @SuppressLint("NewApi") @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { getViewTreeObserver().removeOnGlobalLayoutListener(this); } currentPosition = getCurrentItem(); scrollToChild(currentPosition, 0); } }); } private void scrollToChild(int position, int offset) { if (tabCount == 0) { return; } int newScrollX = tabsContainer.getChildAt(position).getLeft() + offset; if (position > 0 || offset > 0) { newScrollX -= scrollOffset; } if (newScrollX != lastScrollX) { lastScrollX = newScrollX; scrollTo(newScrollX, 0); } } private int getCurrentItem() { return 0; } private void updateTabStyles() { for (int i = 0; i < tabCount; i++) { View v = tabsContainer.getChildAt(i); v.setBackgroundResource(tabBackgroundResId); if (v instanceof TextView) { TextView tab = (TextView) v; tab.setTypeface(tabTypeface, tabTypefaceStyle); tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize); if (i == currentPosition) { tab.setTextColor(selectedTabTextColor); } else { tab.setTextColor(tabTextColor); } } } } private void addTextTab(final int position, String title) { TextView tab = new TextView(getContext()); tab.setText(title); tab.setGravity(Gravity.CENTER); tab.setSingleLine(); addTab(position, tab); } private void addTab(final int position, View tab) { tab.setFocusable(true); tab.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setCurrentItem(position); updateTabStyles(); invalidate(); if(listener != null){ listener.onRefresh(position); } } }); tab.setPadding(tabPadding, 0, tabPadding, 0); tabsContainer.addView(tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isInEditMode() || tabCount == 0) { return; } final int height = getHeight(); // draw indicator line rectPaint.setColor(indicatorColor); // default: line below current tab View currentTab = tabsContainer.getChildAt(currentPosition); float lineLeft = currentTab.getLeft() + intervalWidth; float lineRight = currentTab.getRight() - intervalWidth; // if there is an offset, start interpolating left and right coordinates between current and next tab if (currentPositionOffset > 0f && currentPosition < tabCount - 1) { View nextTab = tabsContainer.getChildAt(currentPosition + 1); final float nextTabLeft = nextTab.getLeft() + intervalWidth; final float nextTabRight = nextTab.getRight() - intervalWidth; lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft); lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight); } canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint); // draw underline rectPaint.setColor(underlineColor); canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint); // draw divider dividerPaint.setColor(dividerColor); for (int i = 0; i < tabCount - 1; i++) { View tab = tabsContainer.getChildAt(i); canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint); } } private void setCurrentItem(int currentPosition) { this.currentPosition = currentPosition; } public interface OnRefreshListener{ void onRefresh(int position); } public void setOnRefreshListener(OnRefreshListener listener){ this.listener = listener; } }

    attrs.xml:文件夹下

    <declare-styleable name="PagerSlidingTabStrip"> <attr name="pstsIndicatorColor" format="color" /> <attr name="pstsUnderlineColor" format="color" /> <attr name="pstsDividerColor" format="color" /> <attr name="pstsIndicatorHeight" format="dimension" /> <attr name="pstsUnderlineHeight" format="dimension" /> <attr name="pstsDividerPadding" format="dimension" /> <attr name="pstsTabPaddingLeftRight" format="dimension" /> <attr name="pstsScrollOffset" format="dimension" /> <attr name="pstsTabBackground" format="reference" /> <attr name="pstsShouldExpand" format="boolean" /> <attr name="pstsTextAllCaps" format="boolean" /> <attr name="pstsSelectedTabTextColor" format="color" /> <attr name="textSelectedSize" format="dimension" /> <attr name="intervalWidth" format="dimension" /> </declare-styleable>

    可以自定义设置属性

    @BindView(R.id.mtabs_fragment) TabsView tabs; private String[] titles; titles = getResources().getStringArray(R.array.artist_tabs_array); tabs.setTabs(titles); tabs.setOnRefreshListener(this);

    @Override public void onRefresh(int position) { artistListview.smoothScrollByOffset(position); //此处做点击处理即可 }

    转载请注明原文地址: https://ju.6miu.com/read-679341.html

    最新回复(0)