Skip to content

Commit

Permalink
1.修复自定义 recyler 报错问题
Browse files Browse the repository at this point in the history
2.修复label的行数与显示一致,却可以点击实现更多的问题
  • Loading branch information
Zzhengsr committed Aug 31, 2020
1 parent e04dac8 commit da6d0e6
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 63 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ android.useAndroidX=true
android.enableJetifier=true
```

**后期计划(加入了新公司,只能空闲时间搞了)**

- 兼容kotlin、androidx和support
- 添加 ViewPager2
- 多行 Tab 的效果

## TabFlowLayout 效果图

<table align="center">
Expand Down
2 changes: 1 addition & 1 deletion TAB_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ flowLayout.setViewPager(viewpager) .
* @ setUnSelectedColor //没有选中的颜色,如果为 TabColorTextView 不需要些这个
*/
flowLayout.setViewPager(mViewPager)
.setTextId(R.id.item_text)
.setTextId(R.id.item_text) //必填,不然 Textview 没效果
.setSelectedColor(Color.WHITE)
.setUnSelectedColor(getResources().getColor(R.color.unselect))
.setDefaultPosition(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ protected void onCreate(Bundle savedInstanceState) {
LabelFlowLayout flowLayout = findViewById(R.id.labelflow);

//自定义属性
/* LabelBean bean = new LabelBean();
LabelBean bean = new LabelBean();
bean.showLines = 3;
bean.showMoreLayoutId = R.layout.show_more;
bean.showMoreColor = Color.WHITE;
bean.handUpLayoutId = R.layout.handup;

flowLayout.setLabelBean(bean);*/
flowLayout.setLabelBean(bean);
flowLayout.setAdapter(new LabelFlowAdapter<String>(R.layout.item_textview,mTitle) {
@Override
public void bindView(View view, String data, int position) {
Expand Down
2 changes: 1 addition & 1 deletion tablib/src/main/java/com/zhengsr/tablib/bean/TabBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public String toString() {
", tabOrientation=" + tabOrientation +
", actionOrientation=" + actionOrientation +
", isAutoScroll=" + isAutoScroll +
", visualCount=" + visualCount +
'}';
}

}
96 changes: 87 additions & 9 deletions tablib/src/main/java/com/zhengsr/tablib/utils/AttrsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.Log;

import com.zhengsr.tablib.FlowConstants;
import com.zhengsr.tablib.R;
Expand All @@ -15,36 +16,113 @@
* describe:自定义属性转换类
*/
public class AttrsUtils {
private static final String TAG = "AttrsUtils";
/**
* 获取TabBean的自定义数据
* @param ta
* @return
*/

public static TabBean getTabBean(TypedArray ta){
TabBean bean = new TabBean();

bean.tabType = ta.getInteger(R.styleable.TabFlowLayout_tab_type, -1);
bean.tabClickAnimTime = ta.getInt(R.styleable.TabFlowLayout_tab_click_animTime, 300);
bean.tabOrientation = ta.getInteger(R.styleable.TabFlowLayout_tab_orientation, FlowConstants.HORIZONTATAL);
bean.isAutoScroll = ta.getBoolean(R.styleable.TabFlowLayout_tab_isAutoScroll, true);
bean.visualCount = ta.getInteger(R.styleable.TabFlowLayout_tab_visual_count, -1);

bean.tabColor = ta.getColor(R.styleable.TabFlowLayout_tab_color, Color.RED);
bean.tabWidth = ta.getDimensionPixelSize(R.styleable.TabFlowLayout_tab_width, -1);
bean.tabHeight = ta.getDimensionPixelSize(R.styleable.TabFlowLayout_tab_height, -1);
bean.tabColor = ta.getColor(R.styleable.TabFlowLayout_tab_color, Color.RED);

bean.tabRoundSize = ta.getDimensionPixelSize(R.styleable.TabFlowLayout_tab_round_size,10);

bean.tabMarginLeft = ta.getDimensionPixelSize(R.styleable.TabFlowLayout_tab_margin_l, 0);
bean.tabMarginTop = ta.getDimensionPixelSize(R.styleable.TabFlowLayout_tab_margin_t, 0);
bean.tabMarginRight = ta.getDimensionPixelSize(R.styleable.TabFlowLayout_tab_margin_r, 0);
bean.tabMarginBottom = ta.getDimensionPixelSize(R.styleable.TabFlowLayout_tab_margin_b, 0);
bean.isAutoScroll = ta.getBoolean(R.styleable.TabFlowLayout_tab_item_autoScale, false);

bean.tabItemRes = ta.getResourceId(R.styleable.TabFlowLayout_tab_item_res,-1);
bean.tabClickAnimTime = ta.getInt(R.styleable.TabFlowLayout_tab_click_animTime, 300);
bean.autoScale = ta.getBoolean(R.styleable.TabFlowLayout_tab_item_autoScale, false);
bean.scaleFactor = ta.getFloat(R.styleable.TabFlowLayout_tab_scale_factor, 1);

bean.tabOrientation = ta.getInteger(R.styleable.TabFlowLayout_tab_orientation, FlowConstants.HORIZONTATAL);
bean.actionOrientation = ta.getInteger(R.styleable.TabFlowLayout_tab_action_orientaion,-1);
bean.tabRoundSize = ta.getDimensionPixelSize(R.styleable.TabFlowLayout_tab_round_size,10);
bean.tabItemRes = ta.getResourceId(R.styleable.TabFlowLayout_tab_item_res,-1);
bean.isAutoScroll = ta.getBoolean(R.styleable.TabFlowLayout_tab_isAutoScroll, true);
bean.visualCount = ta.getInteger(R.styleable.TabFlowLayout_tab_visual_count, -1);

ta.recycle();
return bean;
}


/**
* 对比数据,xml的数据会被用户的覆盖
* @param xmlBean
* @param userBean
* @return
*/
public static TabBean diffTabBean(TabBean xmlBean,TabBean userBean){
//todo 是有有工具帮忙优化?
if (userBean.tabType != -1){
xmlBean.tabType = userBean.tabType;
}
if (userBean.tabColor != -2){
xmlBean.tabColor = userBean.tabColor;
}
if (userBean.tabWidth != -1){
xmlBean.tabWidth = userBean.tabWidth;
}

if (userBean.tabHeight != -1){
xmlBean.tabHeight = userBean.tabHeight;
}

if (userBean.tabRoundSize != -1){
xmlBean.tabRoundSize = userBean.tabRoundSize;
}
if (userBean.tabMarginLeft != -1){
xmlBean.tabMarginLeft = userBean.tabMarginLeft;
}
if (userBean.tabMarginTop != -1){
xmlBean.tabMarginTop = userBean.tabMarginTop;
}
if (userBean.tabMarginRight != -1){
xmlBean.tabMarginRight = userBean.tabMarginRight;
}
if (userBean.tabMarginBottom != -1){
xmlBean.tabMarginBottom = userBean.tabMarginBottom;
}


if (userBean.tabClickAnimTime != -1){
xmlBean.tabClickAnimTime = userBean.tabClickAnimTime;
}
if (userBean.tabItemRes != -1){
xmlBean.tabItemRes = userBean.tabItemRes;
}
if (userBean.autoScale){
xmlBean.autoScale = true;
}

if (userBean.scaleFactor != 1){
xmlBean.scaleFactor = userBean.scaleFactor;
}
if (userBean.tabOrientation != FlowConstants.HORIZONTATAL){
xmlBean.tabOrientation = userBean.tabOrientation;
}
if (userBean.actionOrientation != -1){
xmlBean.actionOrientation = userBean.actionOrientation;
}

if (!userBean.isAutoScroll){
xmlBean.isAutoScroll = false;
}

if (userBean.visualCount != -1){
xmlBean.visualCount = userBean.visualCount;
}

return xmlBean;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public BaseAction() {
mTabRect = new RectF();
}

/**
* 配置数据
* @param parentView
*/
public void config(TabFlowLayout parentView) {
mParentView = parentView;
if (parentView.getChildCount() > 0 && mTabBean!= null) {
Expand Down Expand Up @@ -92,7 +96,7 @@ public void config(TabFlowLayout parentView) {
}

}
if (mTabBean.isAutoScroll && mTabBean.scaleFactor > 1) {
if (mTabBean.autoScale && mTabBean.scaleFactor > 1) {
child.setScaleX(mTabBean.scaleFactor);
child.setScaleY(mTabBean.scaleFactor);
}
Expand Down Expand Up @@ -321,13 +325,13 @@ public void autoScaleView() {
lastView.animate()
.scaleX(1)
.scaleY(1)
.setDuration(mTabBean.tabWidth)
.setDuration(mTabBean.tabClickAnimTime)
.setInterpolator(new LinearInterpolator())
.start();
curView.animate()
.scaleX(mTabBean.scaleFactor)
.scaleY(mTabBean.scaleFactor)
.setDuration(mTabBean.tabWidth)
.setDuration(mTabBean.tabClickAnimTime)
.setInterpolator(new LinearInterpolator())
.start();
}
Expand Down Expand Up @@ -532,7 +536,6 @@ protected void valueChange(TabValue value) {

/**
* 拿到自定义属性
*
* @param bean
*/
public void configAttrs(TabBean bean) {
Expand All @@ -553,14 +556,7 @@ public int getLastIndex() {
return mLastIndex;
}

/**
* 配置动态属性
* xml的属性会被替换
* @param bean
*/
public void setBean(TabBean bean) {
mTabBean = bean;
}


public ViewPager getViewPager() {
return mViewPager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,12 @@ public class ResAction extends BaseAction {
@Override
public void configAttrs(TabBean bean) {
super.configAttrs(bean);
if (bean.tabItemRes != -1) {
mDrawable = mContext.getResources().getDrawable(bean.tabItemRes);
}
}

@Override
public void setBean(TabBean bean) {
super.setBean(bean);
mRes = bean.tabItemRes;

}



@Override
public void config(TabFlowLayout parentView) {
super.config(parentView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public class RoundAction extends BaseAction {
private float mRound;



@Override
public void setBean(TabBean bean) {
super.setBean(bean);

public void configAttrs(TabBean bean) {
super.configAttrs(bean);
if (bean.tabRoundSize !=-1) {
mRound = bean.tabRoundSize;
}
}



@Override
public void config(TabFlowLayout parentView) {
super.config(parentView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FlowLayout extends ViewGroup {
/**
* labelflowlayout
*/
private List<Integer> mLineHeights = new ArrayList<>();
protected List<Integer> mLineHeights = new ArrayList<>();
private List<List<View>> mAllViews = new ArrayList<>();
private int mLabelLines = -1;
private boolean isLabelMoreLine;
Expand Down Expand Up @@ -273,7 +273,7 @@ private void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
* 确定是否换行
*/

if (lineWidth + cWidth > widthSize - (getPaddingLeft() + getPaddingRight())) {
if (lineWidth + cWidth > (widthSize - (getPaddingLeft() + getPaddingRight()))) {

//换行
height += lineHeight;
Expand Down Expand Up @@ -326,7 +326,7 @@ private void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
mViewHeight = height;
//把测量完成的高,重设置给父控件

setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? widthSize : lineWidth, height);
setMeasuredDimension( widthSize, height);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public boolean onTouchEvent(MotionEvent event) {
if (mBitRect.contains(x, y)) {
if (mAdapter != null) {
mBitmap = null;
if (isHasMoreView){
if (isHasMoreView && isLabelMoreLine()){
//显示全部了
setLabelLines(-1);
mAdapter.onShowMoreClick(mMoreView);
Expand Down
Loading

0 comments on commit da6d0e6

Please sign in to comment.