删除项目时,它会触发onLongClick

时间:2018-04-04 09:47:02

标签: android onclick recycler-adapter

我通过滚动或滑动删除了recycleView元素,但在删除第一个元素后,它会触发onLongClick。问题是什么?以及如何解决?

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);
}

}

启动对话框的活动类,例如安装了toast。

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) {
            Toast.makeText(getApplicationContext(), "!!!", Toast.LENGTH_SHORT).show();
        }
    }));

StackOverFlow不喜欢字段中没有足够的文本,只有代码,我不知道还能添加什么。

并添加TouchListener

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);
}

}

0 个答案:

没有答案