From 360434a61c6ec9b668898d3b53a8d6bac59be55f Mon Sep 17 00:00:00 2001 From: "rong.liu" Date: Fri, 1 Jul 2016 16:36:32 +0800 Subject: [PATCH 1/2] Add some code to support api 23 --- .../twowayview/ClickItemTouchListener.java | 50 +++++++++++++++++-- .../lucasr/twowayview/ItemClickSupport.java | 10 +++- .../widget/DividerItemDecoration.java | 11 ++++ 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/lucasr/twowayview/ClickItemTouchListener.java b/core/src/main/java/org/lucasr/twowayview/ClickItemTouchListener.java index 5e57e50..2063a46 100644 --- a/core/src/main/java/org/lucasr/twowayview/ClickItemTouchListener.java +++ b/core/src/main/java/org/lucasr/twowayview/ClickItemTouchListener.java @@ -6,14 +6,16 @@ import android.support.v4.view.MotionEventCompat; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.OnItemTouchListener; +import android.util.Log; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; abstract class ClickItemTouchListener implements OnItemTouchListener { - private static final String LOGTAG = "ClickItemTouchListener"; + private static final String TAG = "ClickItemTouchListener"; private final GestureDetectorCompat mGestureDetector; + private boolean disallowIntercept = false; ClickItemTouchListener(RecyclerView hostView) { mGestureDetector = new ItemClickGestureDetector(hostView.getContext(), @@ -38,7 +40,10 @@ public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent even return false; } - mGestureDetector.onTouchEvent(event); + if(!disallowIntercept){ + mGestureDetector.onTouchEvent(event); + return false; + } return false; } @@ -48,6 +53,11 @@ public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) { // intercepting touch events in the host RecyclerView. } + @Override + public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { + this.disallowIntercept = disallowIntercept; + } + abstract boolean performItemClick(RecyclerView parent, View view, int position, long id); abstract boolean performItemLongClick(RecyclerView parent, View view, int position, long id); @@ -113,7 +123,8 @@ public boolean onSingleTapUp(MotionEvent event) { if (mTargetChild != null) { mTargetChild.setPressed(false); - final int position = mHostView.getChildPosition(mTargetChild); + final int position = mHostView.getChildAdapterPosition(mTargetChild); + //If custom RecycleView.Adapter doesn't override method getItemId , id will be NO_ID which means -1. final long id = mHostView.getAdapter().getItemId(position); handled = performItemClick(mHostView, mTargetChild, position, id); @@ -141,7 +152,8 @@ public void onLongPress(MotionEvent event) { return; } - final int position = mHostView.getChildPosition(mTargetChild); + final int position = mHostView.getChildAdapterPosition(mTargetChild); + //If custom RecycleView.Adapter doesn't override method getItemId , id will be NO_ID which means -1. final long id = mHostView.getAdapter().getItemId(position); final boolean handled = performItemLongClick(mHostView, mTargetChild, position, id); @@ -150,5 +162,35 @@ public void onLongPress(MotionEvent event) { mTargetChild = null; } } + + @Override + public boolean onContextClick(MotionEvent e) { + Log.d(TAG, "onContextClick: "); + return super.onContextClick(e); + } + + @Override + public boolean onDoubleTap(MotionEvent e) { + Log.d(TAG, "onDoubleTap: "); + return super.onDoubleTap(e); + } + + @Override + public boolean onDoubleTapEvent(MotionEvent e) { + Log.d(TAG, "onDoubleTapEvent: "); + return super.onDoubleTapEvent(e); + } + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + Log.d(TAG, "onFling: "); + return super.onFling(e1, e2, velocityX, velocityY); + } + + @Override + public boolean onSingleTapConfirmed(MotionEvent e) { + Log.d(TAG, "onSingleTapConfirmed: "); + return super.onSingleTapConfirmed(e); + } } } diff --git a/core/src/main/java/org/lucasr/twowayview/ItemClickSupport.java b/core/src/main/java/org/lucasr/twowayview/ItemClickSupport.java index 186f7e6..957cda8 100644 --- a/core/src/main/java/org/lucasr/twowayview/ItemClickSupport.java +++ b/core/src/main/java/org/lucasr/twowayview/ItemClickSupport.java @@ -1,11 +1,13 @@ package org.lucasr.twowayview; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.HapticFeedbackConstants; import android.view.SoundEffectConstants; import android.view.View; public class ItemClickSupport { + private static final String TAG = "ItemClickSupport"; /** * Interface definition for a callback to be invoked when an item in the * RecyclerView has been clicked. @@ -79,13 +81,17 @@ public void setOnItemLongClickListener(OnItemLongClickListener listener) { mItemLongClickListener = listener; } + public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { + mTouchListener.onRequestDisallowInterceptTouchEvent(disallowIntercept); + } + public static ItemClickSupport addTo(RecyclerView recyclerView) { ItemClickSupport itemClickSupport = from(recyclerView); if (itemClickSupport == null) { itemClickSupport = new ItemClickSupport(recyclerView); recyclerView.setTag(R.id.twowayview_item_click_support, itemClickSupport); } else { - // TODO: Log warning + Log.w(TAG,"RecyclerView already binds another ItemClickSupport instance!!!"); } return itemClickSupport; @@ -94,7 +100,7 @@ public static ItemClickSupport addTo(RecyclerView recyclerView) { public static void removeFrom(RecyclerView recyclerView) { final ItemClickSupport itemClickSupport = from(recyclerView); if (itemClickSupport == null) { - // TODO: Log warning + Log.w(TAG,"Not found any ItemClickSupport instance which is bound by recyclerView!!!"); return; } diff --git a/layouts/src/main/java/org/lucasr/twowayview/widget/DividerItemDecoration.java b/layouts/src/main/java/org/lucasr/twowayview/widget/DividerItemDecoration.java index 65079ad..58c3ce5 100644 --- a/layouts/src/main/java/org/lucasr/twowayview/widget/DividerItemDecoration.java +++ b/layouts/src/main/java/org/lucasr/twowayview/widget/DividerItemDecoration.java @@ -75,6 +75,12 @@ private static ItemSpacingOffsets createSpacing(Drawable verticalDivider, return spacing; } + + @Override + public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { + onDraw(c,parent); + } + @Override public void onDrawOver(Canvas c, RecyclerView parent) { final BaseLayoutManager lm = (BaseLayoutManager) parent.getLayoutManager(); @@ -117,6 +123,11 @@ public void onDrawOver(Canvas c, RecyclerView parent) { } } + @Override + public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { + getItemOffsets(outRect, parent.getChildPosition(view), parent); + } + @Override public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { mItemSpacing.getItemOffsets(outRect, itemPosition, parent); From 1c276b0a774027b546d02064dfed850623dd80a6 Mon Sep 17 00:00:00 2001 From: "rong.liu" Date: Fri, 1 Jul 2016 16:41:12 +0800 Subject: [PATCH 2/2] Add some code to support api 23 --- .../twowayview/ClickItemTouchListener.java | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/core/src/main/java/org/lucasr/twowayview/ClickItemTouchListener.java b/core/src/main/java/org/lucasr/twowayview/ClickItemTouchListener.java index 2063a46..72b9a65 100644 --- a/core/src/main/java/org/lucasr/twowayview/ClickItemTouchListener.java +++ b/core/src/main/java/org/lucasr/twowayview/ClickItemTouchListener.java @@ -162,35 +162,5 @@ public void onLongPress(MotionEvent event) { mTargetChild = null; } } - - @Override - public boolean onContextClick(MotionEvent e) { - Log.d(TAG, "onContextClick: "); - return super.onContextClick(e); - } - - @Override - public boolean onDoubleTap(MotionEvent e) { - Log.d(TAG, "onDoubleTap: "); - return super.onDoubleTap(e); - } - - @Override - public boolean onDoubleTapEvent(MotionEvent e) { - Log.d(TAG, "onDoubleTapEvent: "); - return super.onDoubleTapEvent(e); - } - - @Override - public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { - Log.d(TAG, "onFling: "); - return super.onFling(e1, e2, velocityX, velocityY); - } - - @Override - public boolean onSingleTapConfirmed(MotionEvent e) { - Log.d(TAG, "onSingleTapConfirmed: "); - return super.onSingleTapConfirmed(e); - } } }