Skip to content

Commit

Permalink
移除构造参数 ,修改说明文档,修改长按事件对 headview 和 footview 的影响
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen authored and Allen committed Aug 6, 2016
1 parent 0d3b752 commit 36e03a4
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 69 deletions.
10 changes: 5 additions & 5 deletions README-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class QuickAdapter extends BaseQuickAdapter<Status> {
```
Adapter
```java
mRecyclerView.addOnItemTouchListener(new OnItemClickListener(mRecyclerView,mQuickAdapter){
mRecyclerView.addOnItemTouchListener(new OnItemClickListener( ){

@Override
public void SimpleOnItemClick(BaseQuickAdapter adapter, View view, int position) {
Expand Down Expand Up @@ -94,7 +94,7 @@ mRecyclerView.addOnItemTouchListener(new OnItemClickListener(mRecyclerView,mQuic
```
Activity
```java
mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener(mRecyclerView,mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener( ) {
@Override
public void SimpleOnItemChildClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
Expand All @@ -104,7 +104,7 @@ Activity
```
#设置 it item long click
```java
mRecyclerView.addOnItemTouchListener(new OnItemLongClickListener(mRecyclerView,mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new OnItemLongClickListener( ) {
@Override
public void SimpleOnItemLongClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
Expand All @@ -130,7 +130,7 @@ Activity
```
然后
```java
mRecyclerView.addOnItemTouchListener(new OnItemChildLongClickListener(mRecyclerView,mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new OnItemChildLongClickListener( ) {
@Override
public void SimpleOnItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
Expand All @@ -139,7 +139,7 @@ Activity
```
# 如果你想实现多种点击事件,你可以实现 SimpleClickListener类。提供了丰富的事件点击封装
```java
mRecyclerView.addOnItemTouchListener(new SimpleClickListener(mRecyclerView, mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new SimpleClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class QuickAdapter extends BaseQuickAdapter<Status> {
#Use it item click
Adapter
```java
mRecyclerView.addOnItemTouchListener(new OnItemClickListener(mRecyclerView,mQuickAdapter){
mRecyclerView.addOnItemTouchListener(new OnItemClickListener( ){

@Override
public void SimpleOnItemClick(BaseQuickAdapter adapter, View view, int position) {
Expand Down Expand Up @@ -91,7 +91,7 @@ first you should register child view id
and then

```java
mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener(mRecyclerView,mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener( ) {
@Override
public void SimpleOnItemChildClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
Expand All @@ -101,7 +101,7 @@ and then
```
#Use it item long click
```java
mRecyclerView.addOnItemTouchListener(new OnItemLongClickListener(mRecyclerView,mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new OnItemLongClickListener( ) {
@Override
public void SimpleOnItemLongClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
Expand All @@ -127,7 +127,7 @@ Adapter
```
Activity
```java
mRecyclerView.addOnItemTouchListener(new OnItemChildLongClickListener(mRecyclerView,mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new OnItemChildLongClickListener( ) {
@Override
public void SimpleOnItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
Expand All @@ -137,7 +137,7 @@ Activity
#if you wish to implement various forms of click
Activity
```java
mRecyclerView.addOnItemTouchListener(new SimpleClickListener(mRecyclerView, mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new SimpleClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.chad.baserecyclerviewadapterhelper.adapter.QuickClickAdapter;
Expand All @@ -23,7 +24,7 @@ public class RecyclerClickItemActivity extends Activity {
private QuickClickAdapter mQuickAdapter;
private static final int PAGE_SIZE = 10;
private static String TAG = "RecyclerClickItemActivity";

private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -34,11 +35,12 @@ protected void onCreate(Bundle savedInstanceState) {
mQuickAdapter.addHeaderView(getHeadView());
mQuickAdapter.addFooterView(getFootView());
mRecyclerView.setAdapter(mQuickAdapter);

/**
* Item clcik
*/

mRecyclerView.addOnItemTouchListener(new OnItemClickListener(mRecyclerView, mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {

@Override
public void SimpleOnItemClick(BaseQuickAdapter adapter, View view, int position) {
Expand Down Expand Up @@ -78,28 +80,28 @@ public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int positi
/**
* this is sample code
*/
/* mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener(mRecyclerView,mQuickAdapter) {
/* mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
@Override
public void SimpleOnItemChildClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
}
});
mRecyclerView.addOnItemTouchListener(new OnItemLongClickListener(mRecyclerView,mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new OnItemLongClickListener() {
@Override
public void SimpleOnItemLongClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
}
});
mRecyclerView.addOnItemTouchListener(new OnItemChildLongClickListener(mRecyclerView,mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new OnItemChildLongClickListener() {
@Override
public void SimpleOnItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
}
});
mRecyclerView.addOnItemTouchListener(new SimpleClickListener(mRecyclerView, mQuickAdapter) {
mRecyclerView.addOnItemTouchListener(new SimpleClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.chad.library.adapter.base.listener;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
Expand All @@ -13,9 +12,6 @@
**/

public abstract class OnItemChildClickListener extends SimpleClickListener {
public OnItemChildClickListener(RecyclerView recyclerView, BaseQuickAdapter baseQuickAdapter) {
super(recyclerView, baseQuickAdapter);
}

@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.chad.library.adapter.base.listener;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
Expand All @@ -14,14 +13,6 @@
public abstract class OnItemChildLongClickListener extends SimpleClickListener {


/**
* @param recyclerView the parent recycleView
* @param baseQuickAdapter this helper need the BaseQuickAdapter
*/
public OnItemChildLongClickListener(RecyclerView recyclerView, BaseQuickAdapter baseQuickAdapter) {
super(recyclerView, baseQuickAdapter);
}

@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.chad.library.adapter.base.listener;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
Expand All @@ -14,9 +13,7 @@
* {@link SimpleClickListener}
*/
public abstract class OnItemClickListener extends SimpleClickListener {
public OnItemClickListener(RecyclerView recyclerView, BaseQuickAdapter baseQuickAdapter) {
super(recyclerView, baseQuickAdapter);
}


@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.chad.library.adapter.base.listener;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
Expand All @@ -12,13 +11,6 @@
public abstract class OnItemLongClickListener extends SimpleClickListener {


/**
* @param recyclerView the parent recycleView
* @param baseQuickAdapter this helper need the BaseQuickAdapter
*/
public OnItemLongClickListener(RecyclerView recyclerView, BaseQuickAdapter baseQuickAdapter) {
super(recyclerView, baseQuickAdapter);
}


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -19,7 +20,7 @@

/**
* Created by AllenCoder on 2016/8/03.
*
* <p>
* This can be useful for applications that wish to implement various forms of click and longclick and childView click
* manipulation of item views within the RecyclerView. SimpleClickListener may intercept
* a touch interaction already in progress even if the SimpleClickListener is already handling that
Expand All @@ -35,25 +36,21 @@ public abstract class SimpleClickListener implements RecyclerView.OnItemTouchLis
protected BaseQuickAdapter baseQuickAdapter;
public static String TAG = "SimpleClickListener";

/**
* @param recyclerView the parent recycleView
* @param baseQuickAdapter this helper need the BaseQuickAdapter
*/

public SimpleClickListener(RecyclerView recyclerView, BaseQuickAdapter baseQuickAdapter) {
this.recyclerView = recyclerView;
this.baseQuickAdapter = baseQuickAdapter;
mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new ItemTouchHelperGestureListener());
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
if (recyclerView == null) {
this.recyclerView = rv;
this.baseQuickAdapter = (BaseQuickAdapter) recyclerView.getAdapter();
mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new ItemTouchHelperGestureListener(recyclerView));
}
mGestureDetector.onTouchEvent(e);
return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
Log.e(TAG, "onTouchEvent: ");
mGestureDetector.onTouchEvent(e);
}

Expand All @@ -62,18 +59,20 @@ public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}

private class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener {
private RecyclerView recyclerView;

public ItemTouchHelperGestureListener(RecyclerView recyclerView) {
this.recyclerView = recyclerView;
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());

if (child != null) {
BaseViewHolder vh = (BaseViewHolder) recyclerView.getChildViewHolder(child);

/**
* have a headview and EMPTY_VIEW FOOTER_VIEW LOADING_VIEW
*/
int type= baseQuickAdapter.getItemViewType(vh.getLayoutPosition());
if (type == EMPTY_VIEW || type == HEADER_VIEW || type == FOOTER_VIEW || type == LOADING_VIEW){
if (isHeaderOrFooterPosition(vh.getLayoutPosition())) {
return false;
}
childClickViewIds = vh.getChildClickViewIds();
Expand All @@ -82,15 +81,15 @@ public boolean onSingleTapUp(MotionEvent e) {
for (Iterator it = childClickViewIds.iterator(); it.hasNext(); ) {
View childView = child.findViewById((Integer) it.next());
if (inRangeOfView(childView, e)) {
onItemChildClick(baseQuickAdapter,childView, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
onItemChildClick(baseQuickAdapter, childView, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
return true;
}
}


onItemClick(baseQuickAdapter,child, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
onItemClick(baseQuickAdapter, child, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
} else {
onItemClick(baseQuickAdapter,child, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
onItemClick(baseQuickAdapter, child, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
}


Expand All @@ -103,19 +102,18 @@ public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null) {
BaseViewHolder vh = (BaseViewHolder) recyclerView.getChildViewHolder(child);
int type= baseQuickAdapter.getItemViewType(vh.getLayoutPosition());
if (type != EMPTY_VIEW && type != HEADER_VIEW && type != FOOTER_VIEW && type != LOADING_VIEW){
longClickViewIds =vh.getItemChildLongClickViewIds();
if (longClickViewIds!=null&&longClickViewIds.size()>0){
if (!isHeaderOrFooterPosition(vh.getLayoutPosition())) {
longClickViewIds = vh.getItemChildLongClickViewIds();
if (longClickViewIds != null && longClickViewIds.size() > 0) {
for (Iterator it = longClickViewIds.iterator(); it.hasNext(); ) {
View childView = child.findViewById((Integer) it.next());
if (inRangeOfView(childView, e)) {
onItemChildLongClick(baseQuickAdapter,childView, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
return ;
onItemChildLongClick(baseQuickAdapter, childView, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
return;
}
}
}
onItemLongClick(baseQuickAdapter,child, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
onItemLongClick(baseQuickAdapter, child, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount());
}

}
Expand All @@ -132,7 +130,7 @@ public void onLongPress(MotionEvent e) {
* will be a view provided by the adapter)
* @param position The position of the view in the adapter.
*/
public abstract void onItemClick(BaseQuickAdapter adapter,View view, int position);
public abstract void onItemClick(BaseQuickAdapter adapter, View view, int position);

/**
* callback method to be invoked when an item in this view has been
Expand All @@ -142,8 +140,10 @@ public void onLongPress(MotionEvent e) {
* @param position The position of the view int the adapter
* @return true if the callback consumed the long click ,false otherwise
*/
public abstract void onItemLongClick(BaseQuickAdapter adapter,View view, int position);
public abstract void onItemLongClick(BaseQuickAdapter adapter, View view, int position);

public abstract void onItemChildClick(BaseQuickAdapter adapter, View view, int position);

public abstract void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position);

public boolean inRangeOfView(View view, MotionEvent ev) {
Expand All @@ -160,6 +160,13 @@ public boolean inRangeOfView(View view, MotionEvent ev) {
return true;
}

private boolean isHeaderOrFooterPosition(int position) {
/**
* have a headview and EMPTY_VIEW FOOTER_VIEW LOADING_VIEW
*/
int type = baseQuickAdapter.getItemViewType(position);
return (type == EMPTY_VIEW || type == HEADER_VIEW || type == FOOTER_VIEW || type == LOADING_VIEW);
}

}

Expand Down

0 comments on commit 36e03a4

Please sign in to comment.