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);
Related
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);
I'm trying to create a feature where if the user swipes LEFT on an item in the RecyclerView, the item will be deleted.
My RecyclerView is inside a Fragment which is inside a FrameLayout controlled by a BottomNavigation.
I have the code where it works when the RecyclerView is in its own Activity but when I transfer it into a Fragment, the swiping functionality breaks. I'm unable to swipe at all.
In my Fragment, I'm extending the RecyclerItemTouchHelperListener interface.
interface RecyclerItemTouchHelperListener {
void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
}
Then I create a Callback
ItemTouchHelper.SimpleCallback ItemTouchHelperCallbackLeft = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this);
new ItemTouchHelper(ItemTouchHelperCallbackLeft).attachToRecyclerView(recyclerView);
Now I create a function called OnSwiped to handle the swiping.
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
if (viewHolder instanceof ListAdapter.ViewHolder) {
if (direction == ItemTouchHelper.LEFT) {
String cityName = stationList.get(viewHolder.getAdapterPosition()).getData().getCity();
//Create a backup of the deleted item in case user wants to undo delete
Station deletedStation = stationList.get(viewHolder.getAdapterPosition());
int deletedStationIndex = viewHolder.getAdapterPosition();
//Remove the item from RecyclerView
listAdapter.removeItem(deletedStationIndex);
Snackbar snackbar = Snackbar.make(coordinatorLayout, cityName + " removed from list!", Snackbar.LENGTH_LONG);
snackbar.setAction("UNDO", new View.OnClickListener() {
#Override
public void onClick(View view) {
// undo is selected, restore the deleted item
listAdapter.restoreItem(deletedStation, position);
}
});
snackbar.show();
}
}
}
And here is the RecyclerItemTouchHelper class.
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 onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());
}
#Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (viewHolder != null) {
final View foregroundView = ((ListAdapter.ViewHolder) viewHolder).listViewForeground;
getDefaultUIUtil().onSelected(foregroundView);
}
}
#Override
public void onChildDrawOver(Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((ListAdapter.ViewHolder) viewHolder).listViewForeground;
getDefaultUIUtil().onDrawOver(canvas, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
}
#Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final View foregroundView = ((ListAdapter.ViewHolder) viewHolder).listViewForeground;
getDefaultUIUtil().clearView(foregroundView);
}
#Override
public void onChildDraw(Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((ListAdapter.ViewHolder) viewHolder).listViewForeground;
getDefaultUIUtil().onDraw(canvas, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
}
#Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
return super.convertToAbsoluteDirection(flags, layoutDirection);
}
}
Keep in mind that everything works when the RecyclerView is inside its own Activity, but when I transfer my RecyclerView into a Fragment this swiping function does not work.
I've fixed my issue by moving the Callback after I initialize my RecyclerView.
recyclerView = itemView.findViewById(R.id.recyclerView);
ItemTouchHelper.SimpleCallback ItemTouchHelperCallbackLeft = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this);
new ItemTouchHelper(ItemTouchHelperCallbackLeft).attachToRecyclerView(recyclerView);
I want to implement stack of cards from bottom to top swipe each overlap previous card stack.Someone implement this using RecyclerView with custom Layout manager.I don't have enough code.
How to do this using Recyclerview.
Try this way:
public class StackLayoutManager extends LinearLayoutManager {
public StackLayoutManager(Context context) {
super(context);
setStackFromEnd(true);
}
#Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
#Override
public boolean canScrollHorizontally() {
return false;
}
#Override
public boolean canScrollVertically() {
return false;
}
#Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
super.onLayoutChildren(recycler, state);
updatePosition();
}
private void updatePosition() {
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
ViewCompat.setTranslationY(view, -view.getTop());
}
}
}
Than use it with your RecyclerView:
recyclerView.setLayoutManager(new StackLayoutManager(this));
And finally add ItemTouchHelper for handling swiping:
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
int pos = viewHolder.getAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
adapter.onRemove(viewHolder);
}
}
});
itemTouchHelper.attachToRecyclerView(recyclerView);
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)
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)
}