Skip to content

Commit

Permalink
Merge pull request #1288 from FrankKwok/master
Browse files Browse the repository at this point in the history
fix #1286
  • Loading branch information
CymChad authored Jul 6, 2017
2 parents 56b0893 + f23da73 commit 0cc66b1
Showing 1 changed file with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
* @see RecyclerView.OnItemTouchListener
*/
public abstract class SimpleClickListener implements RecyclerView.OnItemTouchListener {
public static String TAG = "SimpleClickListener";

private GestureDetectorCompat mGestureDetector;
private RecyclerView recyclerView;
protected BaseQuickAdapter baseQuickAdapter;
public static String TAG = "SimpleClickListener";
private boolean mIsPrepressed = false;
private boolean mIsShowPress = false;
private View mPressedView = null;
Expand All @@ -43,15 +44,15 @@ public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
this.recyclerView = rv;
this.baseQuickAdapter = (BaseQuickAdapter) recyclerView.getAdapter();
mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new ItemTouchHelperGestureListener(recyclerView));
}else if (recyclerView!=rv){
} else if (recyclerView != rv) {
this.recyclerView = rv;
this.baseQuickAdapter = (BaseQuickAdapter) recyclerView.getAdapter();
mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new ItemTouchHelperGestureListener(recyclerView));
}
if (!mGestureDetector.onTouchEvent(e) && e.getActionMasked() == MotionEvent.ACTION_UP && mIsShowPress) {
if (mPressedView!=null){
if (mPressedView != null) {
BaseViewHolder vh = (BaseViewHolder) recyclerView.getChildViewHolder(mPressedView);
if (vh == null ||!isHeaderOrFooterView(vh.getItemViewType())) {
if (vh == null || !isHeaderOrFooterView(vh.getItemViewType())) {
mPressedView.setPressed(false);
}
}
Expand Down Expand Up @@ -99,7 +100,7 @@ public void onShowPress(MotionEvent e) {
@Override
public boolean onSingleTapUp(MotionEvent e) {
if (mIsPrepressed && mPressedView != null) {
if (recyclerView.getScrollState()!=RecyclerView.SCROLL_STATE_IDLE){
if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
return false;
}
final View pressedView = mPressedView;
Expand All @@ -115,7 +116,7 @@ public boolean onSingleTapUp(MotionEvent e) {
View childView = pressedView.findViewById(childClickViewId);
if (childView != null) {
if (inRangeOfView(childView, e) && childView.isEnabled()) {
if (nestViewIds!=null&&nestViewIds.contains(childClickViewId)){
if (nestViewIds != null && nestViewIds.contains(childClickViewId)) {
return false;
}
setPressViewHotSpot(e, childView);
Expand All @@ -127,29 +128,27 @@ public boolean onSingleTapUp(MotionEvent e) {
childView.setPressed(false);
}
}

}
setPressViewHotSpot(e,pressedView);
setPressViewHotSpot(e, pressedView);
mPressedView.setPressed(true);
for (Integer childClickViewId : childClickViewIds) {
View childView = pressedView.findViewById(childClickViewId);
if (childView!=null){
if (childView != null) {
childView.setPressed(false);
}
}
onItemClick(baseQuickAdapter, pressedView, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
} else {
setPressViewHotSpot(e,pressedView);
setPressViewHotSpot(e, pressedView);
mPressedView.setPressed(true);
if (childClickViewIds != null && childClickViewIds.size() > 0) {
for (Integer childClickViewId : childClickViewIds) {
View childView = pressedView.findViewById(childClickViewId);
if (childView!=null){
if (childView != null) {
childView.setPressed(false);
}
}
}

onItemClick(baseQuickAdapter, pressedView, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
}
resetPressedView(pressedView);
Expand All @@ -159,16 +158,16 @@ public boolean onSingleTapUp(MotionEvent e) {
}

private void resetPressedView(final View pressedView) {
if (pressedView!=null){
if (pressedView != null) {
pressedView.postDelayed(new Runnable() {
@Override
public void run() {
if (pressedView!=null){
if (pressedView != null) {
pressedView.setPressed(false);
}

}
},50);
}, 50);
}

mIsPrepressed = false;
Expand All @@ -177,9 +176,9 @@ public void run() {

@Override
public void onLongPress(MotionEvent e) {
boolean isChildLongClick =false;
if (recyclerView.getScrollState()!=RecyclerView.SCROLL_STATE_IDLE){
return ;
boolean isChildLongClick = false;
if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
return;
}
if (mIsPrepressed && mPressedView != null) {
mPressedView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
Expand All @@ -191,8 +190,8 @@ public void onLongPress(MotionEvent e) {
for (Integer longClickViewId : longClickViewIds) {
View childView = mPressedView.findViewById(longClickViewId);
if (inRangeOfView(childView, e) && childView.isEnabled()) {
if (nestViewIds!=null&&nestViewIds.contains(longClickViewId)){
isChildLongClick=true;
if (nestViewIds != null && nestViewIds.contains(longClickViewId)) {
isChildLongClick = true;
break;
}
setPressViewHotSpot(e, childView);
Expand All @@ -204,35 +203,32 @@ public void onLongPress(MotionEvent e) {
}
}
}
if (!isChildLongClick){

if (!isChildLongClick) {
onItemLongClick(baseQuickAdapter, mPressedView, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
setPressViewHotSpot(e,mPressedView);
setPressViewHotSpot(e, mPressedView);
mPressedView.setPressed(true);
if (longClickViewIds != null) {
for (Integer longClickViewId : longClickViewIds) {
View childView = mPressedView.findViewById(longClickViewId);
childView.setPressed(false);
if (childView != null) {
childView.setPressed(false);
}
}
}
mIsShowPress = true;
}

}

}
}


}

private void setPressViewHotSpot(final MotionEvent e,final View mPressedView) {
private void setPressViewHotSpot(final MotionEvent e, final View mPressedView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
/**
* when click Outside the region ,mPressedView is null
*/
if (mPressedView !=null && mPressedView.getBackground() != null) {
mPressedView.getBackground().setHotspot(e.getRawX(), e.getY()-mPressedView.getY());
if (mPressedView != null && mPressedView.getBackground() != null) {
mPressedView.getBackground().setHotspot(e.getRawX(), e.getY() - mPressedView.getY());
}
}
}
Expand Down Expand Up @@ -263,7 +259,7 @@ private void setPressViewHotSpot(final MotionEvent e,final View mPressedView) {

public boolean inRangeOfView(View view, MotionEvent ev) {
int[] location = new int[2];
if (view==null||!view.isShown()){
if (view == null || !view.isShown()) {
return false;
}
view.getLocationOnScreen(location);
Expand All @@ -282,18 +278,18 @@ private boolean isHeaderOrFooterPosition(int position) {
/**
* have a headview and EMPTY_VIEW FOOTER_VIEW LOADING_VIEW
*/
if (baseQuickAdapter==null){
if (recyclerView!=null){
baseQuickAdapter= (BaseQuickAdapter) recyclerView.getAdapter();
}else {
if (baseQuickAdapter == null) {
if (recyclerView != null) {
baseQuickAdapter = (BaseQuickAdapter) recyclerView.getAdapter();
} else {
return false;
}
}
int type = baseQuickAdapter.getItemViewType(position);
return (type == EMPTY_VIEW || type == HEADER_VIEW || type == FOOTER_VIEW || type == LOADING_VIEW);
}
private boolean isHeaderOrFooterView(int type) {

private boolean isHeaderOrFooterView(int type) {
return (type == EMPTY_VIEW || type == HEADER_VIEW || type == FOOTER_VIEW || type == LOADING_VIEW);
}
}
Expand Down

0 comments on commit 0cc66b1

Please sign in to comment.