I am using recyclerview 22.2.0 and the helper class ItemTouchHelper.SimpleCallback to enable swipe-to-dismiss option to my list. But as I have a type of header on it, I
First in recyclerView at onCreateViewHolder method, set tag for each viewHolder type, like below code:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
if(ITEM_TYPE_NORMAL == viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.deposite_card_view, viewGroup, false);
ItemViewHolder holder = new ItemViewHolder(context, v);
holder.itemView.setTag("normal");
return holder;
} else {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_header, viewGroup, false);
HeaderViewHolder holder = new HeaderViewHolder(context, v);
holder.itemView.setTag("header");
return holder;
}
}
then in ItemTouchHelper.Callback implementation, update getMovementFlags method, like below:
public class SwipeController extends ItemTouchHelper.Callback {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if("normal".equalsIgnoreCase((String) viewHolder.itemView.getTag())) {
return makeMovementFlags(0, LEFT | RIGHT);
} else {
return 0;
}
}
at end attach to recyclerView:
final SwipeController swipeController = new SwipeController();
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(swipeController);
itemTouchHelper.attachToRecyclerView(recyclerView);