Skip to content

Commit

Permalink
LabelFlowLayout 单选,设置了 setSelects 之后,点击第一个没反应,以及 onItemSelectState 没回调
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzhengsr committed Feb 12, 2020
1 parent d9821ff commit 90c0fd9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Label_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ flowLayout.setAdapter(adapter = new LabelFlowAdapter<String>(R.layout.item_textv
其实只需要配置 flowLayout.setMaxSelectCount(3); 就可以了,然后adapter 中重写:
```
@Override
public void onReachMacCount(List<Integer> ids, int count) {
super.onReachMacCount(ids, count);
public void onReachMaxCount(List<Integer> ids, int count) {
super.onReachMaxCount(ids, count);
Toast.makeText(LabelActivity.this, "最多只能选中 "+count+" 个"+" 已选中坐标: "+ids, Toast.LENGTH_SHORT).show();
}
```
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ allprojects {
```

```
implementation 'com.github.LillteZheng:FlowHelper:v1.18'
implementation 'com.github.LillteZheng:FlowHelper:v1.19'
```

**如果要支持 AndroidX ,如果你的工程已经有以下代码,直接关联即可:**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.zhengsr.tabhelper.activity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zhengsr.tabhelper.CommonUtils;
Expand All @@ -20,7 +16,7 @@
import java.util.List;

public class LabelActivity extends AppCompatActivity {
private List<String> mTitle = new ArrayList<>(Arrays.asList("新闻", "娱乐", "学习", "测试后", "新闻", "娱乐", "学习", "测试后","新闻", "娱乐", "学习", "测试后", "新闻", "娱乐", "学习"));
private List<String> mTitle = new ArrayList<>(Arrays.asList("新闻", "娱乐", "学习", "测试后", "新闻", "娱乐", "学习"));
private List<String> mTitle2 = new ArrayList<>(Arrays.asList("Life is like an ocean Only strong willed ".split(" ")));
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -62,6 +58,8 @@ public void onClick(View view) {
}
});

flowLayout.setSelects(6);

}


Expand All @@ -75,6 +73,7 @@ public void bindView(View view, String data, int position) {
view.setBackground(CommonUtils.getColorDrawable(10));
}
});

}

private void multiFlow(){
Expand All @@ -89,8 +88,8 @@ public void bindView(View view, String data, int position) {


@Override
public void onReachMacCount(List<Integer> ids, int count) {
super.onReachMacCount(ids, count);
public void onReachMaxCount(List<Integer> ids, int count) {
super.onReachMaxCount(ids, count);
Toast.makeText(LabelActivity.this, "最多只能选中 "+count+" 个"+" 已选中坐标: "+ids, Toast.LENGTH_SHORT).show();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public LabelFlowAdapter(int layoutId, List<T> data) {
* @param ids
* @param count
*/
public void onReachMacCount(List<Integer> ids, int count){}
public void onReachMaxCount(List<Integer> ids, int count){}

/**
* 上个焦点和当前焦点的焦点情况,方便自定义动画,或者其他属性
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -140,7 +139,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
@Override
public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_UP){
if (event.getAction() == MotionEvent.ACTION_UP) {
float x = event.getX();
float y = event.getY();
if (mBitRect.contains(x, y)) {
Expand Down Expand Up @@ -256,7 +255,7 @@ public void onClick(View v) {
if (getSelectedCount() > mMaxSelectCount) {
v.setSelected(false);
adapter.onItemSelectState(v, false);
adapter.onReachMacCount(getSelecteds(), mMaxSelectCount);
adapter.onReachMaxCount(getSelecteds(), mMaxSelectCount);
return;
}
}
Expand Down Expand Up @@ -319,6 +318,7 @@ public List<Integer> getSelecteds() {
View view = getChildAt(i);
if (view.isSelected()) {
indexs.add(i);

}
}
return indexs;
Expand All @@ -332,12 +332,21 @@ public List<Integer> getSelecteds() {
*/
public void setSelects(Integer... indexs) {
if (indexs != null && indexs.length > 0) {

for (int i = 0; i < indexs.length; i++) {
for (int j = 0; j < getChildCount(); j++) {
View view = getChildAt(j);
if (j == indexs[i]) {
view.setSelected(true);
mLastPosition = j;
if (mAdapter != null) {
mAdapter.onItemSelectState(view, true);
}
break;
} else {
if (mAdapter != null) {
mAdapter.onItemSelectState(view, false);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,6 @@ public void computeScroll() {
}
}


if (offset <= 0){
offset = 0;
}
Expand Down

0 comments on commit 90c0fd9

Please sign in to comment.