-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
687 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/lzx/demo/adapter/ItemDragAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.lzx.demo.adapter; | ||
|
||
import android.content.Context; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
import com.lzx.demo.R; | ||
import com.lzx.demo.base.BaseItemDraggableAdapter; | ||
import com.lzx.demo.bean.ItemModel; | ||
|
||
/** | ||
* Created by luoxw on 2016/6/20. | ||
*/ | ||
public class ItemDragAdapter extends BaseItemDraggableAdapter<ItemModel,ItemDragAdapter.ViewHolder> { | ||
|
||
private LayoutInflater mLayoutInflater; | ||
|
||
public ItemDragAdapter(Context context) { | ||
mLayoutInflater = LayoutInflater.from(context); | ||
} | ||
|
||
@Override | ||
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||
return new ViewHolder(mLayoutInflater.inflate(R.layout.sample_item_text, parent, false)); | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | ||
|
||
String item = "item"+position; | ||
|
||
ViewHolder viewHolder = (ViewHolder) holder; | ||
viewHolder.textView.setText(item); | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return mDataList.size(); | ||
} | ||
|
||
public class ViewHolder extends RecyclerView.ViewHolder { | ||
|
||
private TextView textView; | ||
|
||
public ViewHolder(View itemView) { | ||
super(itemView); | ||
textView = (TextView) itemView.findViewById(R.id.info_text); | ||
} | ||
} | ||
} |
207 changes: 207 additions & 0 deletions
207
app/src/main/java/com/lzx/demo/base/BaseItemDraggableAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
package com.lzx.demo.base; | ||
|
||
import android.graphics.Canvas; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.view.MotionEventCompat; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.support.v7.widget.helper.ItemTouchHelper; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
|
||
import com.lzx.demo.R; | ||
import com.lzx.demo.interfaces.OnItemDragListener; | ||
import com.lzx.demo.interfaces.OnItemSwipeListener; | ||
|
||
import java.util.Collections; | ||
|
||
public abstract class BaseItemDraggableAdapter<T, K extends RecyclerView.ViewHolder> extends ListBaseAdapter { | ||
|
||
private static final int NO_TOGGLE_VIEW = 0; | ||
protected int mToggleViewId = NO_TOGGLE_VIEW; | ||
protected ItemTouchHelper mItemTouchHelper; | ||
protected boolean itemDragEnabled = false; | ||
protected boolean itemSwipeEnabled = false; | ||
protected OnItemDragListener mOnItemDragListener; | ||
protected OnItemSwipeListener mOnItemSwipeListener; | ||
protected boolean mDragOnLongPress = true; | ||
|
||
protected View.OnTouchListener mOnToggleViewTouchListener; | ||
protected View.OnLongClickListener mOnToggleViewLongClickListener; | ||
|
||
private static final String ERROR_NOT_SAME_ITEMTOUCHHELPER = "Item drag and item swipe should pass the same ItemTouchHelper"; | ||
|
||
|
||
|
||
/** | ||
* Set the toggle view's id which will trigger drag event. | ||
* If the toggle view id is not set, drag event will be triggered when the item is long pressed. | ||
* | ||
* @param toggleViewId the toggle view's id | ||
*/ | ||
public void setToggleViewId(int toggleViewId) { | ||
mToggleViewId = toggleViewId; | ||
} | ||
|
||
/** | ||
* Set the drag event should be trigger on long press. | ||
* Work when the toggleViewId has been set. | ||
* | ||
* @param longPress by default is true. | ||
*/ | ||
public void setToggleDragOnLongPress(boolean longPress) { | ||
mDragOnLongPress = longPress; | ||
if (mDragOnLongPress) { | ||
mOnToggleViewTouchListener = null; | ||
mOnToggleViewLongClickListener = new View.OnLongClickListener() { | ||
@Override | ||
public boolean onLongClick(View v) { | ||
if (mItemTouchHelper != null && itemDragEnabled) { | ||
mItemTouchHelper.startDrag((RecyclerView.ViewHolder) v.getTag(R.id.BaseQuickAdapter_viewholder_support)); | ||
} | ||
return true; | ||
} | ||
}; | ||
} else { | ||
mOnToggleViewTouchListener = new View.OnTouchListener() { | ||
@Override | ||
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(R.id.BaseQuickAdapter_viewholder_support)); | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
}; | ||
mOnToggleViewLongClickListener = null; | ||
} | ||
} | ||
|
||
/** | ||
* Enable drag items. | ||
* Use itemView as the toggleView when long pressed. | ||
* | ||
* @param itemTouchHelper {@link ItemTouchHelper} | ||
*/ | ||
public void enableDragItem(@NonNull ItemTouchHelper itemTouchHelper) { | ||
enableDragItem(itemTouchHelper, NO_TOGGLE_VIEW, true); | ||
} | ||
|
||
/** | ||
* Enable drag items. Use the specified view as toggle. | ||
* | ||
* @param itemTouchHelper {@link ItemTouchHelper} | ||
* @param toggleViewId The toggle view's id. | ||
* @param dragOnLongPress If true the drag event will be trigger on long press, otherwise on touch down. | ||
*/ | ||
public void enableDragItem(@NonNull ItemTouchHelper itemTouchHelper, int toggleViewId, boolean dragOnLongPress) { | ||
itemDragEnabled = true; | ||
mItemTouchHelper = itemTouchHelper; | ||
setToggleViewId(toggleViewId); | ||
setToggleDragOnLongPress(dragOnLongPress); | ||
} | ||
|
||
/** | ||
* Disable drag items. | ||
*/ | ||
public void disableDragItem() { | ||
itemDragEnabled = false; | ||
mItemTouchHelper = null; | ||
} | ||
|
||
public boolean isItemDraggable() { | ||
return itemDragEnabled; | ||
} | ||
|
||
public void enableSwipeItem() { | ||
itemSwipeEnabled = true; | ||
} | ||
|
||
public void disableSwipeItem() { | ||
itemSwipeEnabled = false; | ||
} | ||
|
||
public boolean isItemSwipeEnable() { | ||
return itemSwipeEnabled; | ||
} | ||
|
||
/** | ||
* @param onItemDragListener Register a callback to be invoked when drag event happen. | ||
*/ | ||
public void setOnItemDragListener(OnItemDragListener onItemDragListener) { | ||
mOnItemDragListener = onItemDragListener; | ||
} | ||
|
||
public int getViewHolderPosition(RecyclerView.ViewHolder viewHolder) { | ||
return viewHolder.getAdapterPosition(); | ||
} | ||
|
||
public void onItemDragStart(RecyclerView.ViewHolder viewHolder) { | ||
if (mOnItemDragListener != null && itemDragEnabled) { | ||
mOnItemDragListener.onItemDragStart(viewHolder, getViewHolderPosition(viewHolder)); | ||
} | ||
} | ||
|
||
public void onItemDragMoving(RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) { | ||
int from = getViewHolderPosition(source); | ||
int to = getViewHolderPosition(target); | ||
|
||
if (from < to) { | ||
for (int i = from; i < to; i++) { | ||
Collections.swap(mDataList, i, i + 1); | ||
} | ||
} else { | ||
for (int i = from; i > to; i--) { | ||
Collections.swap(mDataList, i, i - 1); | ||
} | ||
} | ||
notifyItemMoved(source.getAdapterPosition(), target.getAdapterPosition()); | ||
|
||
if (mOnItemDragListener != null && itemDragEnabled) { | ||
mOnItemDragListener.onItemDragMoving(source, from, target, to); | ||
} | ||
} | ||
|
||
public void onItemDragEnd(RecyclerView.ViewHolder viewHolder) { | ||
if (mOnItemDragListener != null && itemDragEnabled) { | ||
mOnItemDragListener.onItemDragEnd(viewHolder, getViewHolderPosition(viewHolder)); | ||
} | ||
} | ||
|
||
public void setOnItemSwipeListener(OnItemSwipeListener listener) { | ||
mOnItemSwipeListener = listener; | ||
} | ||
|
||
public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder) { | ||
if (mOnItemSwipeListener != null && itemSwipeEnabled) { | ||
mOnItemSwipeListener.onItemSwipeStart(viewHolder, getViewHolderPosition(viewHolder)); | ||
} | ||
} | ||
|
||
public void onItemSwipeClear(RecyclerView.ViewHolder viewHolder) { | ||
if (mOnItemSwipeListener != null && itemSwipeEnabled) { | ||
mOnItemSwipeListener.clearView(viewHolder, getViewHolderPosition(viewHolder)); | ||
} | ||
} | ||
|
||
public void onItemSwiped(RecyclerView.ViewHolder viewHolder) { | ||
if (mOnItemSwipeListener != null && itemSwipeEnabled) { | ||
mOnItemSwipeListener.onItemSwiped(viewHolder, getViewHolderPosition(viewHolder)); | ||
} | ||
|
||
int pos = getViewHolderPosition(viewHolder); | ||
|
||
mDataList.remove(pos); | ||
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); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.