Skip to content

Commit

Permalink
LabelFlowlayout
Browse files Browse the repository at this point in the history
增加BaseLabelItem,增加Header,可实现换行
  • Loading branch information
Zzhengsr committed Mar 29, 2023
1 parent b8b7da9 commit 0714a69
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 14 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Label_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,12 @@ bean.showMoreColor = Color.WHITE;
flowLayout.setLabelBean(bean);
```

### 3.3.7参考代码:
### 3.3.7 主动换行

你可能想有个 Header 换行,然后再显示不同的数据,这个时候,需要你的数据类继承 BaseLabelItem,把 isHeader 设置为ture,
即可实现自动换行。

### 参考代码:

[布局代码](https://github.com/LillteZheng/FlowHelper/blob/master/app/src/main/res/layout/activity_label.xml)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ implementation 'com.github.LillteZheng:FlowHelper:v2.3'


## 版本信息:
- v2.4 : 修复TabLabelLayout 在调用 resetStatus,第一个无法点击的问题。并增加 BaseLabelItem,Bean 类继承它,可实现换行
- v2.3 : 修改TabVpFlowLayout 快速点击时,TabColorTextView 还有残留,和 tab 没有转移过去的问题
- v2.0 : 内置部分控件,减少接入成本,并优化一些bug,和关闭demo ,viewpager 内存泄露的问题
- v1.37 : 增加 tab_width_equals_text ,让 rect 根据 text 的长度变化,修复 TabColorTextView 加粗不起作用的问题
Expand Down
1 change: 0 additions & 1 deletion appx/src/main/java/com/zhengsr/tabhelper/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text);
textView.setTextColor(getResources().getColor(R.color.color_selector));

}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.zhengsr.tabhelper.activity;

import android.graphics.Color;
import android.icu.text.CaseMap;
import android.os.Bundle;
import android.service.quicksettings.Tile;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.zhengsr.tabhelper.CommonUtils;
import com.zhengsr.tabhelper.R;
import com.zhengsr.tablib.bean.BaseLabelItem;
import com.zhengsr.tablib.view.adapter.LabelFlowAdapter;
import com.zhengsr.tablib.view.flow.LabelFlowLayout;

Expand All @@ -31,14 +34,25 @@ protected void onCreate(Bundle savedInstanceState) {

private void singleFlow(){
LabelFlowLayout flowLayout = findViewById(R.id.singleflow);
List<SingleBean> datas = new ArrayList();
for (int i = 0; i < mTitle.size(); i++) {
SingleBean bean = new SingleBean();
bean.name = mTitle.get(i);
if (i == 0 || i == 3){
bean.isHeader = true;
bean.name = "Header";
}
datas.add(bean);
}
final LabelFlowAdapter adapter;
flowLayout.setAdapter(adapter = new LabelFlowAdapter<String>(R.layout.item_textview,mTitle) {
flowLayout.setAdapter(adapter = new LabelFlowAdapter<SingleBean>(R.layout.item_textview,datas) {
@Override
public void bindView(View view, String data, int position) {
setText(view, R.id.item_text,data)
public void bindView(View view, SingleBean data, int position) {
setText(view, R.id.item_text,data.name)
. setTextColor(view, R.id.item_text,getResources().getColor(R.color.unselect));
}


@Override
public void onItemSelectState(View view, boolean isSelected) {
super.onItemSelectState(view, isSelected);
Expand All @@ -63,6 +77,9 @@ public void onClick(View view) {

}

class SingleBean extends BaseLabelItem{
public String name;
}

private void searchFlow(){
LabelFlowLayout flowLayout = findViewById(R.id.search_flow);
Expand Down Expand Up @@ -151,4 +168,7 @@ public boolean onItemLongClick(View view,int position) {
}


public void test(View view) {

}
}
6 changes: 6 additions & 0 deletions appx/src/main/res/layout/activity_label.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
app:label_maxSelectCount="3" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="test"
android:text="测试"/>
</LinearLayout>

</ScrollView>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.zhengsr.tablib.bean;

/**
* @author by zhengshaorui 2023/3/29
* describe:Label
*/
public class BaseLabelItem {
public boolean isHeader;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@
* @auther by zhengshaorui on 2020/1/8
* describe: 标签瀑布流布局,支持单选,多选
*/
public class LabelFlowLayout extends ScrollFlowLayout {
public class LabelFlowLayout extends ScrollFlowLayout<LabelFlowAdapter> {
private static final String TAG = "LabelFlowLayout";
/**
* logic
*/
private LabelFlowAdapter mAdapter;
private int mMaxSelectCount;
private int mLastPosition = -1;
private boolean isHasMoreView;
Expand Down Expand Up @@ -431,7 +430,6 @@ public void setAutoScroll(boolean autoScroll) {
}



/**
* 设置自定义属性
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* @author by zhengshaorui 2021/5/23 06:40
* describe:用来获取通用的自定义属性,和一些常用的配置
*/
public class AbsFlowLayout extends ScrollFlowLayout {
public class AbsFlowLayout extends ScrollFlowLayout<TabFlowAdapter> {
private static final String TAG = AbsFlowLayout.class.getSimpleName();
/**
* attrs
Expand All @@ -55,7 +55,6 @@ public class AbsFlowLayout extends ScrollFlowLayout {
*/
protected BaseAction mAction;
private TabConfig mTabConfig;
protected TabFlowAdapter mAdapter;
protected Scroller mScroller;
protected int mLastScrollPos = 0;
protected int mLastIndex = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import androidx.annotation.Nullable;

import com.zhengsr.tablib.FlowConstants;
import com.zhengsr.tablib.bean.BaseLabelItem;
import com.zhengsr.tablib.view.adapter.BaseFlowAdapter;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -18,7 +20,7 @@
* @author by zhengshaorui on 2019/10/8
* Describe: 瀑布流布局,这个类只用来测量子控件,不做其他操作
*/
class FlowLayout extends ViewGroup {
class FlowLayout<T extends BaseFlowAdapter> extends ViewGroup {
private static final String TAG = "FlowLayout";
protected int mViewWidth;
protected int mViewHeight;
Expand All @@ -35,6 +37,7 @@ class FlowLayout extends ViewGroup {
private int mLabelLines = -1;
private boolean isLabelMoreLine;
protected int mLineWidth;
protected T mAdapter;

public FlowLayout(Context context) {
this(context, null);
Expand Down Expand Up @@ -162,7 +165,7 @@ private void measureTabHorizontal(int widthMeasureSpec, int heightMeasureSpec) {
int widthPixels = getResources().getDisplayMetrics().widthPixels;
if (MeasureSpec.EXACTLY == widthMode) {
widthPixels = widthSize;
}else {
} else {
if (mVisibleCount != -1) {
MarginLayoutParams parentParams = (MarginLayoutParams) getLayoutParams();
widthPixels -= (getPaddingStart() + getPaddingEnd() + parentParams.leftMargin + parentParams.rightMargin);
Expand Down Expand Up @@ -271,9 +274,11 @@ private void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
/**
* 确定是否换行
*/
boolean isFull = lineWidth + cWidth > (widthSize - (getPaddingLeft() + getPaddingRight()));
boolean isHeader = isHeader(i);

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

if (isFull) {
//换行
height += lineHeight;

Expand All @@ -297,10 +302,32 @@ private void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {

} else {
//未换行
if (isHeader) {
//查看上一个
if (i > 0 && getChildAt(i - 1) != null) {
//// 如果当前子视图是头部视图,并且上一个子视图不为空,则另起一行
height += lineHeight;
mLineHeights.add(lineHeight);
mAllViews.add(lineViews);
lineViews = new ArrayList<>();
}
}
lineWidth += cWidth;
lineHeight = Math.max(lineHeight, cHeight);
mLineWidth = Math.max(mLineWidth, lineWidth);
lineViews.add(child);

if (isHeader) {
//换行
height += lineHeight;


mLineHeights.add(lineHeight);
mAllViews.add(lineViews);
lineViews = new ArrayList<>();
}


}

//加最后一行
Expand Down Expand Up @@ -408,6 +435,15 @@ public boolean isVerticalMove() {
return isVertical() || isLabelFlow();
}

private boolean isHeader(int index) {
if (mAdapter != null && mAdapter.getDatas() != null) {
Object bean = mAdapter.getDatas().get(index);
if (bean instanceof BaseLabelItem) {
return ((BaseLabelItem) bean).isHeader;
}
}
return false;
}

public boolean isLabelMoreLine() {
return isLabelMoreLine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@

import androidx.annotation.Nullable;

import com.zhengsr.tablib.view.adapter.BaseFlowAdapter;

/**
* @author by zhengshaorui on 2019/10/8
* Describe: 滚动类,用来移动
*/
public class ScrollFlowLayout extends FlowLayout {
public class ScrollFlowLayout<T extends BaseFlowAdapter> extends FlowLayout<T> {
private static final String TAG = "ScrollFlowLayout";
private int mTouchSlop;
private float mLastPos;
Expand Down

0 comments on commit 0714a69

Please sign in to comment.