I override in SimpleItemTouchHelper
class the method onChildDraw
the following way:
@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) {
float width = (float) viewHolder.itemView.getWidth();
float alpha = 1.0f - Math.abs(dX) / width;
viewHolder.itemView.setAlpha(alpha);
viewHolder.itemView.setTranslationX(dX);
} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY,
actionState, isCurrentlyActive);
}
}
This simply enables a fade in animation when you swipe to left or right .
In my adapter, I've implemented the ItemTouchHelperAdapter
and override onItemDismiss
the following way in order to show an AlertDialog
to confirm if you want to remove the selected element:
public class TeambuilderAdapter extends RecyclerView.Adapter<TeambuilderAdapter.ViewHolder> implements ItemTouchHelperAdapter {
@Override
public void onItemDismiss(int position) {
DialogInterface.OnClickListener dialogClickListener =(dialog, which) -> {
if (which == DialogInterface.BUTTON_POSITIVE) {
mData.remove(position);
notifyItemRemoved(position);
((PokemonTeambuilderTabActivity) mContext).refreshTeamList();
}
};
new AlertDialog . Builder (mContext)
.setTitle(mContext.getString(R.string.remove_team))
.setMessage(mContext.getString(R.string.remove_team_question))
.setPositiveButtonIcon(ContextCompat.getDrawable(mContext, R.drawable.ic_baseline_check_circle_14))
.setPositiveButton(mContext.getString(R.string.confirm), dialogClickListener)
.setNegativeButtonIcon(ContextCompat.getDrawable(mContext, R.drawable.ic_baseline_cancel_14))
.setNegativeButton(mContext.getString(R.string.decline), dialogClickListener)
.show();
}
...
}
All is working, the problem resides that if I press the cancel button, the item swiped disappears and only appears again when I re-enter the activity.
How can I restore the original position when I press cancel or I leave the dialog?
from Android ItemTouchHelper onChildDraw make itemView return to its original position
No comments:
Post a Comment