Skip to content
This repository has been archived by the owner on Dec 1, 2017. It is now read-only.

Support api 23 #276

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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;
}

Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
10 changes: 8 additions & 2 deletions core/src/main/java/org/lucasr/twowayview/ItemClickSupport.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down