Skip to content

try add verticalpadding and horizontalpadding between tags support #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# FlowLayout
Android流式布局,支持单选、多选等,适合用于产品标签等。

## 特色

##特色
* 以setAdapter形式注入数据
* 直接设置selector为background即可完成标签选则的切换,类似CheckBox
* 支持控制选择的Tag数量,比如:单选、多选
Expand All @@ -13,12 +13,16 @@ Android流式布局,支持单选、多选等,适合用于产品标签等。
* 支持adapter.notifyDataChanged
* Activity重建(或者旋转)后,选择的状态自动保存

##效果图
## 效果图

<img src="flowlayout_03.gif" width="320px"/>

<img src="sc.png" width="320px"/>

<img src="horizontal_padding_and_vertical_padding_sample.png" width="320px"/>

<img src="auto_stretch_sample.png" width="320px"/>

## 用法

```java
Expand All @@ -30,26 +34,34 @@ dependencies {
### 声明
布局文件中声明:

```java
<com.zhy.view.flowlayout.TagFlowLayout
android:id="@+id/id_flowlayout"
zhy:max_select="-1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20dp">
</com.zhy.view.flowlayout.TagFlowLayout>
```
<com.zhy.view.flowlayout.TagFlowLayout
android:id="@+id/id_flowlayout"
zhy:max_select="-1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
zhy:vertical_padding="17dp"
zhy:horizontal_padding="17dp"
zhy:auto_stretch="true"
android:padding="20dp">
</com.zhy.view.flowlayout.TagFlowLayout>
```

支持属性:

`max_select`:-1为不限制选择数量,>=1的数字为控制选择tag的数量
| 属性 | 单位 | 说明 |
| ------------------ | ------- | ------------------------------------------------------------ |
| max_select | integer | -1为不限制选择数量,>=1的数字为控制选择tag的数量 |
| horizontal_padding | dp | 标签之间水平padding |
| vertical_padding | dp | 标签之间垂直padding |
| auto_stretch | boolean | 标签两端对齐,中间间隙均分 |

支持通过state=checked来控制选中和取消,也可以自己在Adapter
的onSelected和unSelected中分别处理显示。

###设置数据
### 设置数据

```java
```
mFlowLayout.setAdapter(new TagAdapter<String>(mVals)
{
@Override
Expand All @@ -68,7 +80,7 @@ getView中回调,类似ListView等用法。

### 对于选中状态

```java
```
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/tag_select_textcolor"
Expand Down Expand Up @@ -98,9 +110,9 @@ public void unSelected(int position, View view) {
```


###事件
### 事件

```java
```
mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()
{
@Override
Expand All @@ -114,7 +126,7 @@ mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()

点击标签时的回调。

```java
```
mFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener()
{
@Override
Expand All @@ -126,9 +138,9 @@ mFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener()
```
选择多个标签时的回调。

##预先设置Item选中
## 预先设置Item选中

```java
```
//预先设置选中
mAdapter.setSelectedList(1,3,5,7,8,9);
//获得所有选中的pos集合
Expand All @@ -137,3 +149,7 @@ flowLayout.getSelectedList();







Binary file added auto_stretch_sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ public class FlowLayout extends ViewGroup {
protected List<Integer> mLineWidth = new ArrayList<Integer>();
private int mGravity;
private List<View> lineViews = new ArrayList<>();
private int mTagVerticalPadding, mTagHorizontalPadding;
private boolean mAutoStretch;

public FlowLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TagFlowLayout);
mGravity = ta.getInt(R.styleable.TagFlowLayout_tag_gravity, LEFT);
mTagVerticalPadding = ta.getDimensionPixelSize(R.styleable.TagFlowLayout_vertical_padding, 0);
mTagHorizontalPadding = ta.getDimensionPixelSize(R.styleable.TagFlowLayout_horizontal_padding, 0);
mAutoStretch = ta.getBoolean(R.styleable.TagFlowLayout_auto_stretch, false);
int layoutDirection = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault());
if (layoutDirection == LayoutDirection.RTL) {
if (mGravity == LEFT) {
Expand Down Expand Up @@ -63,13 +68,13 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int lineHeight = 0;

int cCount = getChildCount();

for (int i = 0; i < cCount; i++) {
View child = getChildAt(i);
if (child.getVisibility() == View.GONE) {
if (i == cCount - 1) {
lineWidth -= mTagHorizontalPadding;
width = Math.max(lineWidth, width);
height += lineHeight;
height += lineHeight - mTagVerticalPadding;
}
continue;
}
Expand All @@ -80,20 +85,22 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int childWidth = child.getMeasuredWidth() + lp.leftMargin
+ lp.rightMargin;
int childHeight = child.getMeasuredHeight() + lp.topMargin
+ lp.bottomMargin;
+ lp.bottomMargin + mTagVerticalPadding;

if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight()) {
lineWidth -= mTagHorizontalPadding;
width = Math.max(width, lineWidth);
lineWidth = childWidth;
height += lineHeight;
lineHeight = childHeight;
} else {
lineWidth += childWidth;
lineWidth += childWidth + mTagHorizontalPadding;
lineHeight = Math.max(lineHeight, childHeight);
}
if (i == cCount - 1) {
lineWidth -= mTagHorizontalPadding;
width = Math.max(lineWidth, width);
height += lineHeight;
height += lineHeight - mTagVerticalPadding;
}
}
setMeasuredDimension(
Expand Down Expand Up @@ -125,29 +132,30 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
MarginLayoutParams lp = (MarginLayoutParams) child
.getLayoutParams();

int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin + mTagVerticalPadding;

if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width - getPaddingLeft() - getPaddingRight()) {
if (childWidth + lineWidth > width - getPaddingLeft() - getPaddingRight()) {
lineWidth -= mTagHorizontalPadding;
mLineHeight.add(lineHeight);
mAllViews.add(lineViews);
mLineWidth.add(lineWidth);

lineWidth = 0;
lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
lineHeight = childHeight;
lineViews = new ArrayList<View>();
}
lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
lineHeight = Math.max(lineHeight, childHeight + lp.topMargin
+ lp.bottomMargin);
lineWidth += childWidth + mTagHorizontalPadding;
lineHeight = Math.max(lineHeight, childHeight);
lineViews.add(child);

}
lineWidth -= mTagHorizontalPadding;
lineHeight -= mTagVerticalPadding;
mLineHeight.add(lineHeight);
mLineWidth.add(lineWidth);
mAllViews.add(lineViews);


int left = getPaddingLeft();
int top = getPaddingTop();

Expand All @@ -173,8 +181,12 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
Collections.reverse(lineViews);
break;
}

for (int j = 0; j < lineViews.size(); j++) {
int lineChildCount = lineViews.size();
int gap = 0;
if (lineChildCount > 1) {
gap = (width - currentLineWidth) / (lineChildCount - 1);
}
for (int j = 0; j < lineChildCount; j++) {
View child = lineViews.get(j);
if (child.getVisibility() == View.GONE) {
continue;
Expand All @@ -191,11 +203,11 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
child.layout(lc, tc, rc, bc);

left += child.getMeasuredWidth() + lp.leftMargin
+ lp.rightMargin;
+ lp.rightMargin + mTagHorizontalPadding;
if (mAutoStretch) left += gap;
}
top += lineHeight;
}

}

@Override
Expand Down
3 changes: 3 additions & 0 deletions flowlayout-lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
<enum name="center" value="0" />
<enum name="right" value="1" />
</attr>
<attr name="vertical_padding" format="dimension" />
<attr name="horizontal_padding" format="dimension" />
<attr name="auto_stretch" format="boolean" />
</declare-styleable>
</resources>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.