Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadCym committed Jul 8, 2016
2 parents c3eb5bd + 7db74a7 commit db14d4d
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
-[BaseRecyclerAdapter之添加动画](http://www.jianshu.com/p/fa3f97c19263)
-[BaseRecyclerAdapter之添加不同布局(头部尾部)](http://www.jianshu.com/p/9d75c22f0964)
-[BaseRecyclerAdapter之添加不同布局(优化篇)](http://www.jianshu.com/p/cf29d4e45536)
-[分析整合版](http://www.jianshu.com/p/b1ad50633732)

[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-BaseRecyclerViewAdapterHelper-green.svg?style=true)](https://android-arsenal.com/details/1/3644)
[![](https://jitpack.io/v/CymChad/BaseRecyclerViewAdapterHelper.svg)](https://jitpack.io/#CymChad/BaseRecyclerViewAdapterHelper)
# BaseRecyclerViewAdapterHelper
![logo](http://upload-images.jianshu.io/upload_images/972352-1d77e0a75a4a7c0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
一个强大并且灵活的RecyclerViewAdapter,欢迎使用。(喜欢的可以**Star**一下)
Expand Down Expand Up @@ -35,7 +37,7 @@
然后在dependencies添加:
```
dependencies {
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:v1.8.4'
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:v1.8.8'
}
```

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-BaseRecyclerViewAdapterHelper-green.svg?style=true)](https://android-arsenal.com/details/1/3644)
[![](https://jitpack.io/v/CymChad/BaseRecyclerViewAdapterHelper.svg)](https://jitpack.io/#CymChad/BaseRecyclerViewAdapterHelper)
# BaseRecyclerViewAdapterHelper([中文版文档](https://github.com/CymChad/BaseRecyclerViewAdapterHelper/blob/master/README-cn.md)
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/972352-1d77e0a75a4a7c0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Powerful and flexible RecyclerAdapter
Expand Down Expand Up @@ -31,7 +32,7 @@ allprojects {
Add the dependency
```groovy
dependencies {
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:v1.8.4'
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:v1.8.8'
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.chad.baserecyclerviewadapterhelper;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
Expand Down Expand Up @@ -60,6 +62,10 @@ public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
((CardView)viewHolder.itemView).setCardBackgroundColor(Color.WHITE);
}
};
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(20);
paint.setColor(Color.BLACK);
OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
@Override
public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
Expand All @@ -81,16 +87,27 @@ public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
Log.d(TAG, "View Swiped: " + pos);
}

@Override
public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
canvas.drawColor(ContextCompat.getColor(ItemDragAndSwipeUseActivity.this, R.color.color_light_blue));
canvas.drawText("Just some text", 0, 40, paint);
}
};

mAdapter = new ItemDragAdapter(mData);
mItemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter);
mItemTouchHelper = new ItemTouchHelper(mItemDragAndSwipeCallback);
mItemTouchHelper.attachToRecyclerView(mRecyclerView);

//mItemDragAndSwipeCallback.setDragMoveFlags(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN);
mItemDragAndSwipeCallback.setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
mAdapter.enableSwipeItem();
mAdapter.setOnItemSwipeListener(onItemSwipeListener);
mAdapter.enableDragItem(mItemTouchHelper);
mAdapter.setOnItemDragListener(listener);

mRecyclerView.setAdapter(mAdapter);
mItemTouchHelper.attachToRecyclerView(mRecyclerView);
}

private List<String> generateData(int size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import android.animation.Animator;
import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.v4.view.MotionEventCompat;
Expand Down Expand Up @@ -610,15 +611,15 @@ public void onBindViewHolder(final RecyclerView.ViewHolder holder, int positions
if (mToggleViewId != NO_TOGGLE_VIEW) {
View toggleView = ((BaseViewHolder) holder).getView(mToggleViewId);
if (toggleView != null) {
toggleView.setTag(holder);
toggleView.setTag(R.id.BaseQuickAdapter_viewholder_support, holder);
if (mDragOnLongPress) {
toggleView.setOnLongClickListener(mOnToggleViewLongClickListener);
} else {
toggleView.setOnTouchListener(mOnToggleViewTouchListener);
}
}
} else {
holder.itemView.setTag(holder);
holder.itemView.setTag(R.id.BaseQuickAdapter_viewholder_support, holder);
holder.itemView.setOnLongClickListener(mOnToggleViewLongClickListener);
}
}
Expand Down Expand Up @@ -931,7 +932,7 @@ public void setToggleDragOnLongPress(boolean longPress) {
@Override
public boolean onLongClick(View v) {
if (mItemTouchHelper != null && itemDragEnabled) {
mItemTouchHelper.startDrag((RecyclerView.ViewHolder) v.getTag());
mItemTouchHelper.startDrag((RecyclerView.ViewHolder) v.getTag(R.id.BaseQuickAdapter_viewholder_support));
}
return true;
}
Expand All @@ -943,7 +944,7 @@ public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN
&& !mDragOnLongPress) {
if (mItemTouchHelper != null && itemDragEnabled) {
mItemTouchHelper.startDrag((RecyclerView.ViewHolder) v.getTag());
mItemTouchHelper.startDrag((RecyclerView.ViewHolder) v.getTag(R.id.BaseQuickAdapter_viewholder_support));
}
return true;
} else {
Expand Down Expand Up @@ -1077,4 +1078,10 @@ public void onItemSwiped(RecyclerView.ViewHolder viewHolder) {
notifyItemRemoved(viewHolder.getAdapterPosition());
}

public void onItemSwiping(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
if (mOnItemSwipeListener != null && itemSwipeEnabled) {
mOnItemSwipeListener.onItemSwipeMoving(canvas, viewHolder, dX, dY, isCurrentlyActive);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.chad.library.adapter.base.callback;

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import com.chad.library.R;
import com.chad.library.adapter.base.BaseQuickAdapter;

/**
Expand All @@ -12,12 +15,13 @@ public class ItemDragAndSwipeCallback extends ItemTouchHelper.Callback {

// private static final String TAG = ItemDragAndSwipeCallback.class.getSimpleName();

private BaseQuickAdapter mAdapter;
BaseQuickAdapter mAdapter;

private static final float THRESHOLD_SWIPE = 0.7f;
private static final float THRESHOLD_MOVE = 0.1f;
float mMoveThreshold = 0.1f;
float mSwipeThreshold = 0.7f;

private int mActionState = ItemTouchHelper.ACTION_STATE_IDLE;
int mDragMoveFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
int mSwipeMoveFlags = ItemTouchHelper.END;

public ItemDragAndSwipeCallback(BaseQuickAdapter adapter) {
mAdapter = adapter;
Expand All @@ -37,10 +41,10 @@ public boolean isItemViewSwipeEnabled() {
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
mAdapter.onItemDragStart(viewHolder);
mActionState = actionState;
viewHolder.itemView.setTag(R.id.BaseQuickAdapter_dragging_support, true);
} else if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
mAdapter.onItemSwipeStart(viewHolder);
mActionState = actionState;
viewHolder.itemView.setTag(R.id.BaseQuickAdapter_swiping_support, true);
}
super.onSelectedChanged(viewHolder, actionState);
}
Expand All @@ -49,19 +53,21 @@ public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionStat
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);

if (mActionState == ItemTouchHelper.ACTION_STATE_DRAG) {
if (viewHolder.itemView.getTag(R.id.BaseQuickAdapter_dragging_support) != null
&& (Boolean)viewHolder.itemView.getTag(R.id.BaseQuickAdapter_dragging_support)) {
mAdapter.onItemDragEnd(viewHolder);
} else if (mActionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
viewHolder.itemView.setTag(R.id.BaseQuickAdapter_dragging_support, false);
}
if (viewHolder.itemView.getTag(R.id.BaseQuickAdapter_swiping_support) != null
&& (Boolean)viewHolder.itemView.getTag(R.id.BaseQuickAdapter_swiping_support)) {
mAdapter.onItemSwipeClear(viewHolder);
viewHolder.itemView.setTag(R.id.BaseQuickAdapter_swiping_support, false);
}
mActionState = ItemTouchHelper.ACTION_STATE_IDLE;
}

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
int swipeFlags = ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
return makeMovementFlags(mDragMoveFlags, mSwipeMoveFlags);
}

@Override
Expand All @@ -85,11 +91,80 @@ public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

@Override
public float getMoveThreshold(RecyclerView.ViewHolder viewHolder) {
return THRESHOLD_MOVE;
return mMoveThreshold;
}

@Override
public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
return THRESHOLD_SWIPE;
return mSwipeThreshold;
}

/**
* Set the fraction that the user should move the View to be considered as swiped.
* The fraction is calculated with respect to RecyclerView's bounds.
* <p>
* Default value is .5f, which means, to swipe a View, user must move the View at least
* half of RecyclerView's width or height, depending on the swipe direction.
*
* @param swipeThreshold A float value that denotes the fraction of the View size. Default value
* is .8f .
*/
public void setSwipeThreshold(float swipeThreshold) {
mSwipeThreshold = swipeThreshold;
}


/**
* Set the fraction that the user should move the View to be considered as it is
* dragged. After a view is moved this amount, ItemTouchHelper starts checking for Views
* below it for a possible drop.
*
* @param moveThreshold A float value that denotes the fraction of the View size. Default value is
* .1f .
*/
public void setMoveThreshold(float moveThreshold) {
mMoveThreshold = moveThreshold;
}

/**
* <p>Set the drag movement direction.</p>
* <p>The value should be ItemTouchHelper.UP, ItemTouchHelper.DOWN, ItemTouchHelper.LEFT, ItemTouchHelper.RIGHT or their combination.</p>
* You can combine them like ItemTouchHelper.UP | ItemTouchHelper.DOWN, it means that the item could only move up and down when dragged.
* @param dragMoveFlags the drag movement direction. Default value is ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT.
*/
public void setDragMoveFlags(int dragMoveFlags) {
mDragMoveFlags = dragMoveFlags;
}

/**
* <p>Set the swipe movement direction.</p>
* <p>The value should be ItemTouchHelper.START, ItemTouchHelper.END or their combination.</p>
* You can combine them like ItemTouchHelper.START | ItemTouchHelper.END, it means that the item could swipe to both left or right.
* @param swipeMoveFlags the swipe movement direction. Default value is ItemTouchHelper.END.
*/
public void setSwipeMoveFlags(int swipeMoveFlags) {
mSwipeMoveFlags = swipeMoveFlags;
}

@Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
View itemView = viewHolder.itemView;

c.save();
if (dX > 0) {
c.clipRect(itemView.getLeft(), itemView.getTop(), itemView.getLeft() + dX, itemView.getBottom());
c.translate(itemView.getLeft(), itemView.getTop());
} else {
c.clipRect(itemView.getRight() + dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
c.translate(itemView.getRight() + dX, itemView.getTop());
}

mAdapter.onItemSwiping(c, viewHolder, dX, dY, isCurrentlyActive);
c.restore();

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

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;

/**
Expand All @@ -23,5 +24,16 @@ public interface OnItemSwipeListener {
*/
void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos);

/**
* Draw on the empty edge when swipe moving
* @param canvas the empty edge's canvas
* @param viewHolder The ViewHolder which is being interacted by the User or it was
* interacted and simply animating to its original position
* @param dX The amount of horizontal displacement caused by user's action
* @param dY The amount of vertical displacement caused by user's action
* @param isCurrentlyActive True if this view is currently being controlled by the user or
* false it is simply animating back to its original state.
*/
void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive);

}
6 changes: 6 additions & 0 deletions library/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="BaseQuickAdapter_viewholder_support" type="id"/>
<item name="BaseQuickAdapter_swiping_support" type="id"/>
<item name="BaseQuickAdapter_dragging_support" type="id"/>
</resources>

0 comments on commit db14d4d

Please sign in to comment.