There is a recyclewView, according to which deletion takes place by moving the item to the left, changing the item is a simple click and longClick, by which I set the contextMenu. But the problem is that when I delete an item, longClick is triggered. The most interesting thing is that when removed from the last elements it does not work, but when you remove the first elements and in the middle of the standing ones, it works. How to change the code ???
RecyclerTouchListener - actions on recycleView:
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{ private GestureDetector gestureDetector; private ClickListener clickListener; public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) { this.clickListener = clickListener; gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { return true; } @Override public void onLongPress(MotionEvent e) { View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); if (child != null && clickListener != null) { clickListener.onLongClick(child, recyclerView.getChildPosition(child)); } } }); } @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { View child = rv.findChildViewUnder(e.getX(), e.getY()); if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) { clickListener.onClick(child, rv.getChildPosition(child)); } return false; } @Override public void onTouchEvent(RecyclerView rv, MotionEvent e) { } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { } public interface ClickListener { void onClick(View view, int position); void onLongClick(View view, int position); } }
public class ItemTouchHelper extends SimpleCallback { private ItemTouchHelperListener helperListener; public ItemTouchHelper(int dragDirs, int swipeDirs, ItemTouchHelperListener helperListener) { super(dragDirs, swipeDirs); this.helperListener = helperListener; } @Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { if (viewHolder != null){ final View view = ((NotesAdapter.NotesHolder)viewHolder).foreground; getDefaultUIUtil().onSelected(view); } } @Override public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { final View view = ((NotesAdapter.NotesHolder)viewHolder).foreground; getDefaultUIUtil().onDrawOver(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive); } @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { final View view = ((NotesAdapter.NotesHolder)viewHolder).foreground; getDefaultUIUtil().clearView(view); } @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { final View view = ((NotesAdapter.NotesHolder)viewHolder).foreground; getDefaultUIUtil().onDraw(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive); } @Override public int convertToAbsoluteDirection(int flags, int layoutDirection) { return super.convertToAbsoluteDirection(flags, layoutDirection); } @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { return true; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { helperListener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition()); } public interface ItemTouchHelperListener{ void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position); } }
Clicking on recycleView:
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() { @Override public void onClick(View view, final int position) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); ViewGroup viewGroupRecycle = findViewById(R.id.layoutParent); View viewUpdate = getLayoutInflater().inflate(R.layout.create, viewGroupRecycle); final EditText text = viewUpdate.findViewById(R.id.edit_text); builder.setView(viewUpdate); builder.setTitle(note_update); final int id = notesList.get(position).getId(); String textStr = notesList.get(position).getText(); text.setText(textStr); text.postDelayed(new Runnable() { @Override public void run() { InputMethodManager keyboard = (InputMethodManager) MainActivity.this .getSystemService(Context.INPUT_METHOD_SERVICE); assert keyboard != null; keyboard.showSoftInput(text, 0); } }, 50); builder.setPositiveButton(save, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); final AlertDialog alertDialog = builder.create(); alertDialog.show(); alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String strText = removeSeparator(text); Boolean check = checked(strText); if (check){ updatable(strText, id); prepareData(); alertDialog.dismiss(); } else message(error); } }); } @Override public void onLongClick(View view, int position) { MainActivity.this.openContextMenu(view); } }));
RecyclerViewhas a special classItemTouchHelperand itsonSwiped()method overridden by it - everything is implemented with three lines of code. - pavlofff