ItemTouchHelper with Multi ViewType - android

public class ItemCallBack extends ItemTouchHelper.Callback {
private BaseDragAdapter adapter;
private List list;
public ItemCallBack(BaseDragAdapter adapter, List list) {
this.adapter = adapter;
this.list = list;
}
#Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = 0;
int swipeFlags = 0;
int startPosition = viewHolder.getAdapterPosition();
if (startPosition != 0) {
if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
} else if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
} else if (recyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
}
}
return makeMovementFlags(dragFlags, swipeFlags);
}
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
int startPosition = viewHolder.getAdapterPosition();
int targetPosition = target.getAdapterPosition();
adapter.onMoved(startPosition, targetPosition);
return true;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { //左右拖动删除
int position = viewHolder.getAdapterPosition();
adapter.onSwiped(position);
}
#Override
public boolean isLongPressDragEnabled() {
return true;
}
#Override
public boolean isItemViewSwipeEnabled() {
return false;
}
#Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
viewHolder.itemView.setScaleX(1.2f);
viewHolder.itemView.setScaleY(1.2f);
}
super.onSelectedChanged(viewHolder, actionState);
}
#Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setScaleX(1.0f);
viewHolder.itemView.setScaleY(1.0f);
onOrderListener.order();
}
#Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
private OnOrderListener onOrderListener;
public void setOnPictureClickListener(OnOrderListener onOrderListener) {
if (onOrderListener != null) {
this.onOrderListener = onOrderListener;
}
}
public interface OnOrderListener {
void order();
}
}
#Override
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_ADD;
} else if (position == 1) {
return TYPE_COVER;
}
return TYPE_NORMAL;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ADD) {
View view = LayoutInflater.from(context).inflate(R.layout.item_upload_pic_add, null);
return new AddHolder(view);
} else if (viewType == TYPE_COVER) {
View view = LayoutInflater.from(context).inflate(R.layout.item_upload_pic_cover, null);
return new CoverHolder(view);
} else {
View view = LayoutInflater.from(context).inflate(R.layout.item_upload_pic_normal, null);
return new NormalHolder(view);
}
}
I wanna swap item with ItemTouchHelper.
The issue is:
When i drag and swap item that viewtype is TYPE_NORNAL,animation work well;
But When i try to switch between different types(ex:TYPE_NORMAL and TYPE_COVER),animation does not execute correctly.
How to make the animation execute correctly?

I had the same issue. Resoved it with setting
setHasStableIds(true);
on adapter, and overriding getItemId() so that it returns unique id for each item I have in adapter. If you do not have unique id for items you can spawn Map and generate these id's when you get new items for adapter. Then lookup these id's in getItemId(), using item as a key.

Override getMovementFlags and set your swipeFlags depends of your viewType
override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder
): Int {
val swipeFlags = if (viewHolder.itemViewType == ListItem.TYPE_BODY)
ItemTouchHelper.RIGHT
else 0
return makeMovementFlags(0, swipeFlags)
}

Related

where to write callback function for ItemTouchHelper which is used with RecyclerView

I want to write a piece of code into where item drag-drop is over. I got this answer https://stackoverflow.com/a/36275415/1684778 but I am confused about how to achieve this(where i have to implement that answer)
I want to write onDrop instead of onItemMove, how to achieve it.
DragItemTouchHelper
public class DragItemTouchHelper extends ItemTouchHelper.Callback {
public static final float ALPHA_FULL = 1.0f;
private final MoveHelperAdapter mAdapter;
public DragItemTouchHelper(MoveHelperAdapter adapter) {
mAdapter = adapter;
}
#Override
public boolean isLongPressDragEnabled() {
return true;
}
#Override
public boolean isItemViewSwipeEnabled() {
return false;
}
#Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
// Set movement flags based on the layout manager
if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
final int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
} else {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
}
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
if (source.getItemViewType() != target.getItemViewType()) {
return false;
}
// Notify the adapter of the move
mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
return true;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
}
#Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
// Fade out the view as it is swiped out of the parent's bounds
final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
viewHolder.itemView.setAlpha(alpha);
viewHolder.itemView.setTranslationX(dX);
} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
#Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
// We only want the active item to change
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
if (viewHolder instanceof TouchViewHolder) {
// Let the view holder know that this item is being moved or dragged
TouchViewHolder itemViewHolder = (TouchViewHolder) viewHolder;
itemViewHolder.onItemSelected();
}
}
super.onSelectedChanged(viewHolder, actionState);
}
#Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setAlpha(ALPHA_FULL);
if (viewHolder instanceof TouchViewHolder) {
// Tell the view holder it's time to restore the idle state
TouchViewHolder itemViewHolder = (TouchViewHolder) viewHolder;
itemViewHolder.onItemClear();
}
}
public interface MoveHelperAdapter {
boolean onItemMove(int fromPosition, int toPosition);
}
public interface TouchViewHolder {
void onItemSelected();
void onItemClear();
}
}
You can put your class that extends ItemTouchHelper callback in the end of the activity class where your RecyclerView is and attach it to RecyclerView defined in activity or fragment as below
ItemTouchHelper touchHelper = new ItemTouchHelper(your_custom_callback);
touchHelper.attachToRecyclerView(recyclerView);

How to disable Swipe left and up in RecyclerView?

I want to perform only the right swipe on RecyclerView.
But it swipes on both sides and also an upside.
How to disable swipe left and up?
Want to perform delete functionality on the right swipe.
It works but it swipes all the side of items. How to prevent this?
NotificationFragment.java
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
if (viewHolder instanceof NotificationListAdapter.ViewHolder) {
mAdapter.removeItem(viewHolder.getAdapterPosition());
}
}
NotificationListAdapter.java
public void removeItem(int position) {
callDeleteService(position);
}
RecyclerItemTouchHelper
public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
private RecyclerItemTouchHelperListener listener;
public RecyclerItemTouchHelper(int dragDirs ,int swipeDirs ,RecyclerItemTouchHelperListener listener) {
super( dragDirs ,swipeDirs );
this.listener = listener;
}
#Override
public boolean onMove(RecyclerView recyclerView ,RecyclerView.ViewHolder viewHolder ,RecyclerView.ViewHolder target) {
return true;
}
#Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder ,int actionState) {
if (viewHolder != null) {
final View foregroundView = ((NotificationListAdapter.ViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().onSelected( foregroundView );
}
}
#Override
public void onChildDrawOver(Canvas c ,RecyclerView recyclerView ,
RecyclerView.ViewHolder viewHolder ,float dX ,float dY ,
int actionState ,boolean isCurrentlyActive) {
final View foregroundView = ((NotificationListAdapter.ViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().onDrawOver( c ,recyclerView ,foregroundView ,dX ,dY ,
actionState ,isCurrentlyActive );
}
#Override
public void clearView(RecyclerView recyclerView ,RecyclerView.ViewHolder viewHolder) {
final View foregroundView = ((NotificationListAdapter.ViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().clearView( foregroundView );
}
#Override
public void onChildDraw(Canvas c ,RecyclerView recyclerView ,
RecyclerView.ViewHolder viewHolder ,float dX ,float dY ,
int actionState ,boolean isCurrentlyActive) {
final View foregroundView = ((NotificationListAdapter.ViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().onDraw( c ,recyclerView ,foregroundView ,dX ,dY ,
actionState ,isCurrentlyActive );
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder ,int direction) {
// if (direction == ItemTouchHelper.END){
listener.onSwiped( viewHolder ,direction ,viewHolder.getAdapterPosition() );
// }
// else {
// Log.d( "Swipe LEFT : ", " DO NOTHING" );
// }
}
#Override
public int getMovementFlags(RecyclerView recyclerView ,RecyclerView.ViewHolder viewHolder) {
int dragFlags = 0;
int swipeFlags = ItemTouchHelper.LEFT;
return makeMovementFlags( dragFlags ,swipeFlags );
}
#Override
public int convertToAbsoluteDirection(int flags ,int layoutDirection) {
return super.convertToAbsoluteDirection( flags ,layoutDirection );
}
public interface RecyclerItemTouchHelperListener {
void onSwiped(RecyclerView.ViewHolder viewHolder ,int direction ,int position);
}
}
I want it like a 1st image.
We can define swipe directions by using SimpleCallback().
For RIGHT:
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.RIGHT, this);
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
For LEFT:
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this);
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
For Both:
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);

RecyclerView - how to get position only on drag and on drop? [duplicate]

I have a problem with ItemTouchHelper of RecyclerView.
I am making a game. The game board is actually a RecyclerView. RecyclerView has GridLayoutManager with some span count. I want to implement drag & drop recyclerview's items. Any item can dragging over all directions (up, down, left, right).
private void initializeLayout() {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutFrozen(true);
recyclerView.setNestedScrollingEnabled(false);
// set layout manager
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), BOARD_SIZE,
LinearLayoutManager.VERTICAL, true);
recyclerView.setLayoutManager(layoutManager);
// Extend the Callback class
ItemTouchHelper.Callback itemTouchCallback = new ItemTouchHelper.Callback() {
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
Log.w(TAG, "onMove");
return false;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// Application does not include swipe feature.
}
#Override
public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
Log.d(TAG, "onMoved");
// this is calling every time, but I need only when user dropped item, not after every onMove function.
}
#Override
public boolean isItemViewSwipeEnabled() {
return false;
}
#Override
public boolean isLongPressDragEnabled() {
return true;
}
#Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END;
int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
}
};
ItemTouchHelper touchHelper = new ItemTouchHelper(itemTouchCallback);
touchHelper.attachToRecyclerView(recyclerView);
}
SO, why ItemTouchHelper's onMoved function works when I still dragging item on the RecyclerView ? How can I achieve this ?
While dragging and dropping an item, the onMove() can be called more than once, but the clearView() will be called once. So you can use this to indicate the drag was over(drop was happened).
And use two variables dragFrom and dragTo to trace the really position in a completed "drag & drop".
private ItemTouchHelper.Callback dragCallback = new ItemTouchHelper.Callback() {
int dragFrom = -1;
int dragTo = -1;
#Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
return makeMovementFlags(ItemTouchHelper.UP|ItemTouchHelper.DOWN|ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT,
0);
}
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition();
int toPosition = target.getAdapterPosition();
if(dragFrom == -1) {
dragFrom = fromPosition;
}
dragTo = toPosition;
adapter.onItemMove(fromPosition, toPosition);
return true;
}
private void reallyMoved(int from, int to) {
// I guessed this was what you want...
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
#Override
public boolean isLongPressDragEnabled() {
return true;
}
#Override
public boolean isItemViewSwipeEnabled() {
return false;
}
#Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
if(dragFrom != -1 && dragTo != -1 && dragFrom != dragTo) {
reallyMoved(dragFrom, dragTo);
}
dragFrom = dragTo = -1;
}
};
adapter.onItemMove(fromPosition, toPosition) was like below:
public void onItemMove(int fromPosition, int toPosition) {
list.add(toPosition, list.remove(fromPosition));
notifyItemMoved(fromPosition, toPosition);
}
The onSelectedChanged(RecyclerView.ViewHolder, int) callback provides information about the current actionState:
- ACTION_STATE_IDLE:
- ACTION_STATE_DRAG
- ACTION_STATE_SWIPE
So you could keep track whether the order changed, and when the state changes to ACTION_STATE_IDLE, you can do what you need to do!
Example:
private final class MyCallback extends ItemTouchHelper.Callback {
private boolean mOrderChanged;
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
// Check if positions of viewHolders correspond to underlying model, and if not, flip the items in the model and set the mOrderChanged flag
mOrderChanged = true;
}
#Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState == ItemTouchHelper.ACTION_STATE_IDLE && mOrderChanged) {
doSomething();
mOrderChanged = false;
}
}
I did some tests and onSelectedChanged(RecyclerView.ViewHolder?, Int) seemed most reliable for me to detect end of the gesture (drop). The method is called whenever an item is being dragged and passed action state of ACTION_STATE_DRAG. When the drag is over, it is called with action state of ACTION_STATE_IDLE.
See my solution below. The onItemDrag(Int, Int) callback is used for reordering items in an adapter as the item is being dragged. On the other hand the onItemDragged(Int, Int) callback is intended for updating positions in a database at the end of the gesture.
class ItemGestureHelper(private val listener: OnItemGestureListener) : ItemTouchHelper.Callback() {
interface OnItemGestureListener {
fun onItemDrag(fromPosition: Int, toPosition: Int): Boolean
fun onItemDragged(fromPosition: Int, toPosition: Int)
fun onItemSwiped(position: Int)
}
private var dragFromPosition = -1
private var dragToPosition = -1
// Other methods omitted...
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
// Item is being dragged, keep the current target position
dragToPosition = target.adapterPosition
return listener.onItemDrag(viewHolder.adapterPosition, target.adapterPosition)
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
listener.onItemSwiped(viewHolder.adapterPosition)
}
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
super.onSelectedChanged(viewHolder, actionState)
when (actionState) {
ItemTouchHelper.ACTION_STATE_DRAG -> {
viewHolder?.also { dragFromPosition = it.adapterPosition }
}
ItemTouchHelper.ACTION_STATE_IDLE -> {
if (dragFromPosition != -1 && dragToPosition != -1 && dragFromPosition != dragToPosition) {
// Item successfully dragged
listener.onItemDragged(dragFromPosition, dragToPosition)
// Reset drag positions
dragFromPosition = -1
dragToPosition = -1
}
}
}
}
}
You must implement OnMove listener in you adapter:
Collections.swap(youCoolList, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
like this man doing
https://medium.com/#ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf#.blviq6jxp
special grid example
https://medium.com/#ipaulpro/drag-and-swipe-with-recyclerview-6a6f0c422efd#.xb74uu7ke

android - RecyclerView item not clickable after drag and drop in Nougat (Android 7.0)

I'm facing this issue only on android Nougat. After drag and dropping an item, the OnClick event doesn't fire on this item anymore.
Here's the adapter :
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.VideoListViewHolder> implements ItemTouchHelperAdapter {
private List<Media> videos;
private LayoutInflater inflater;
private Context context;
private int selectedItemPosition;
private Media selectedMedia;
private static final int NOTHING_SELECTED = -1;
public VideoListAdapter(Context context, List<Media> videos, Delegate delegate) {
inflater = LayoutInflater.from(context);
setVideos(videos);
this.context = context;
setSelectedItem(NOTHING_SELECTED);
}
public void setSelectedItem(int pos) {
selectedItemPosition = pos;
if(pos != NOTHING_SELECTED)
selectedMedia = videos.get(pos);
else
selectedMedia = null;
}
public void setVideos(List<Media> videos) {
this.videos = videos;
setSelectedItem(NOTHING_SELECTED);
}
private void onVideosReordered() {
if(selectedMedia != null)
setSelectedItem(videos.indexOf(selectedMedia));
}
public void unselectAll() {
setSelectedItem(NOTHING_SELECTED);
notifyDataSetChanged();
}
#Override
public VideoListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_video_list, parent, false);
return new VideoListViewHolder(view);
}
#Override
public void onBindViewHolder(final VideoListViewHolder holder, int position) {
String imagePath = videos.get(position).getFilePath();
boolean isEmptyItem = imagePath == null || imagePath.isEmpty();
if (isEmptyItem) {
holder.video_list_item_selected.setVisibility(View.GONE);
holder.img_image.setVisibility(View.GONE);
} else {
UtilsMedia.loadThumbnailImageNoSpinAnimation(holder.img_image, imagePath);
boolean isSelected = selectedItemPosition == position;
if (isSelected && !isEmptyItem)
holder.video_list_item_selected.setVisibility(View.VISIBLE);
else
holder.video_list_item_selected.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return videos.size();
}
#Override
public boolean onItemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++)
Collections.swap(videos, i, i + 1);
} else {
for (int i = fromPosition; i > toPosition; i--)
Collections.swap(videos, i, i - 1);
}
notifyItemMoved(fromPosition, toPosition);
return true;
}
#Override
public void onItemDropped() {
onVideosReordered();
notifyDataSetChanged();
}
class VideoListViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder {
public ImageView img_image;
public ImageView video_list_item_selected;
public FrameLayout fl_root;
public Context context;
public boolean isSelected;
private static final String TAG = "VideoListViewHolder";
public VideoListViewHolder(View view) {
super(view);
img_image = (ImageView) view.findViewById(R.id.img_image);
video_list_item_selected = (ImageView) view.findViewById(R.id.video_list_item_selected);
fl_root = (FrameLayout) view.findViewById(R.id.fl_root);
context = view.getContext();
isSelected = false;
fl_root.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int currentPosition = getAdapterPosition();
if (selectedItemPosition == currentPosition) {
setSelectedItem(NOTHING_SELECTED);
} else {
if (selectedItemPosition != -1)
notifyItemChanged(selectedItemPosition);
setSelectedItem(currentPosition);
}
notifyItemChanged(currentPosition);
}
});
}
#Override
public void onItemSelected() {
final Animation zoomOut = AnimationUtils.loadAnimation(context, R.anim.zoom_out);
zoomOut.setDuration(300);
fl_root.startAnimation(zoomOut);
}
#Override
public void onItemClear() {
final Animation zoomIn = AnimationUtils.loadAnimation(context, R.anim.zoom_in);
zoomIn.setDuration(300);
fl_root.startAnimation(zoomIn);
}
}
}
And here is the item touch helper callback :
public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
public static final float ALPHA_FULL = 1.0f;
protected final ItemTouchHelperAdapter mAdapter;
public ItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
mAdapter = adapter;
}
#Override
public boolean isLongPressDragEnabled() {
return true;
}
#Override
public boolean isItemViewSwipeEnabled() {
return false;
}
#Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
final int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
}
private int moveSource = -1;
private int moveTarget = -1;
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
if (source.getItemViewType() != target.getItemViewType())
return false;
if(moveSource == -1)
moveSource = source.getAdapterPosition();
moveTarget = target.getAdapterPosition();
mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
return true;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
}
#Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
#Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemSelected();
}
super.onSelectedChanged(viewHolder, actionState);
}
#Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setAlpha(ALPHA_FULL);
if (viewHolder instanceof ItemTouchHelperViewHolder) {
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemClear();
}
if(moveSource != -1 && moveTarget != -1 && moveSource != moveTarget)
mAdapter.onItemDropped();
moveSource = moveTarget = -1;
}
}
Any help would be appreciated, thanks !
I had to remove every notifyDataSet changed and replace them by other notifyXXX (notifyItemRangeInserted, notifyItemRangeRemoved, notifyItemRangeChanged, etc)

how to create drag and drop in recyclerview without drag header and footer?

I created drag and drop in recylerview using ItemTouchHelper.Callback,it is working.But i have header and footer in recylerview i dont want to drag header and footer.how to solve this,this is my code
recylerviewActivity
ItemTouchHelper.Callback callback = new SwipeAndDrag(pick_up_mAdapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(recyclerView);
SwipeAndDrag.java
public class SwipeAndDrag extends ItemTouchHelper.Callback {
private final ItemTouchHelperAdapter mAdapter;
private RouteInformation points;
public SwipeAndDrag(ItemTouchHelperAdapter adapter) {
mAdapter = adapter;
}
#Override
public boolean isLongPressDragEnabled() {
return false;
}
#Override
public boolean isItemViewSwipeEnabled() {
return true;
}
#Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}
}
You should modify your getMovementFlags method like this (tested it and it seems ok) :
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder){
if(viewHolder instanceof yourFooterOrHeaderViewholderClass){
int dragFlags = 0; // then, they can't be dragged
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}else{ // if(viewHolder instanceof yourNormalItemViewHolderClass)
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
}
Then, only your items should be drag&dropped, whereas your header and footer couldn't be manually moved.

Categories

Resources