From 502773ecf8f93b98c8983d245bb9e80d3083b310 Mon Sep 17 00:00:00 2001 From: NanSailor Date: Thu, 22 Dec 2016 17:29:37 +0800 Subject: [PATCH] Update TagFlowLayout.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使得标签支持响应长按事件操作 --- .../zhy/view/flowlayout/TagFlowLayout.java | 82 ++++++++++++++----- 1 file changed, 63 insertions(+), 19 deletions(-) diff --git a/flowlayout-lib/src/main/java/com/zhy/view/flowlayout/TagFlowLayout.java b/flowlayout-lib/src/main/java/com/zhy/view/flowlayout/TagFlowLayout.java index 986af2b..12ce1b6 100644 --- a/flowlayout-lib/src/main/java/com/zhy/view/flowlayout/TagFlowLayout.java +++ b/flowlayout-lib/src/main/java/com/zhy/view/flowlayout/TagFlowLayout.java @@ -1,4 +1,4 @@ -package com.zhy.view.flowlayout; +package com.koalac.dispatcher.widget; import android.content.Context; import android.content.res.TypedArray; @@ -11,13 +11,17 @@ import android.view.MotionEvent; import android.view.View; +import com.koalac.dispatcher.R; +import com.zhy.view.flowlayout.FlowLayout; +import com.zhy.view.flowlayout.TagView; + import java.util.HashSet; import java.util.Iterator; import java.util.Set; -/** - * Created by zhy on 15/9/10. - */ + + + public class TagFlowLayout extends FlowLayout implements TagAdapter.OnDataChangedListener { private TagAdapter mTagAdapter; @@ -91,6 +95,22 @@ public interface OnTagClickListener private OnTagClickListener mOnTagClickListener; + + + public interface OnLongTagClickListener + { + boolean onLongTagClick(View view, int position, FlowLayout parent); + } + + private OnLongTagClickListener mOnLongTagClickListener; + + + public void setOnLongTagClickListener(OnLongTagClickListener onLongTagClickListener) { + mOnLongTagClickListener = onLongTagClickListener; + if (mOnLongTagClickListener != null) setLongClickable(true); + + } + public void setOnTagClickListener(OnTagClickListener onTagClickListener) { mOnTagClickListener = onTagClickListener; @@ -100,13 +120,15 @@ public void setOnTagClickListener(OnTagClickListener onTagClickListener) public void setAdapter(TagAdapter adapter) { + //if (mTagAdapter == adapter) + // return; mTagAdapter = adapter; mTagAdapter.setOnDataChangedListener(this); mSelectedView.clear(); changeAdapter(); } - + @SuppressWarnings("ResourceType") private void changeAdapter() { removeAllViews(); @@ -162,7 +184,7 @@ private void changeAdapter() @Override public boolean onTouchEvent(MotionEvent event) { - if (event.getAction() == MotionEvent.ACTION_UP) + if (event.getAction() == MotionEvent.ACTION_UP||event.getAction() == MotionEvent.ACTION_DOWN) { mMotionEvent = MotionEvent.obtain(event); } @@ -172,26 +194,48 @@ public boolean onTouchEvent(MotionEvent event) @Override public boolean performClick() { - if (mMotionEvent == null) return super.performClick(); - - int x = (int) mMotionEvent.getX(); - int y = (int) mMotionEvent.getY(); - mMotionEvent = null; + if (mMotionEvent != null&&mMotionEvent.getAction() == MotionEvent.ACTION_UP){ + TagView child = getChildByMotionEvent(); + int pos = findPosByView(child); + if (child != null) + { + doSelect(child, pos); + if (mOnTagClickListener != null) + { + return mOnTagClickListener.onTagClick(child.getTagView(), pos, this); + } + } + return true; + } + return super.performClick(); + } - TagView child = findChild(x, y); - int pos = findPosByView(child); - if (child != null) - { - doSelect(child, pos); - if (mOnTagClickListener != null) + @Override + public boolean performLongClick() { + if (mMotionEvent != null&&mMotionEvent.getAction() == MotionEvent.ACTION_DOWN){ + TagView child = getChildByMotionEvent(); + int pos = findPosByView(child); + if (child != null) { - return mOnTagClickListener.onTagClick(child.getTagView(), pos, this); + if (mOnLongTagClickListener != null) + { + return mOnLongTagClickListener.onLongTagClick(child.getTagView(), pos, this); + } } + return true; } - return true; + return super.performLongClick(); } + private TagView getChildByMotionEvent() { + int x = (int) mMotionEvent.getX(); + int y = (int) mMotionEvent.getY(); + mMotionEvent = null; + + return findChild(x, y); + } + public void setMaxSelectCount(int count) { if (mSelectedView.size() > count)