没有用拖动侦听器触发的android运动事件

问题描述 投票:0回答:1

问题

所以我有一个由卡片视图填充的回收者视图,所有附加了拖动侦听器,运动事件工作正常,直到我设置一个视图被拖动,所以这条线被注释掉了,所有日志都打印得很完美

holder.handleView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (MotionEventCompat.getActionMasked(event)){
                case MotionEvent.ACTION_DOWN:
                    Log.d("Adptr" , "ACTION_DOWN");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation_pressed));
                    //mDragStartListener.onStartDrag(holder);
                    return true;
                case MotionEvent.ACTION_UP:
                    Log.d("Adptr" , "ACTION_UP");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
                case MotionEvent.ACTION_CANCEL:
                    Log.d("Adptr" , "ACTION_CANCEL");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
            }
            return false;
        }
    });

但如果我取消注释该行

mDragStartListener.onStartDrag(holder);

然后不再调用ACTION_UP而且我对为什么感到困惑

监听器是一个接口

public interface OnStartDragListener {
    void onStartDrag(RecyclerView.ViewHolder viewHolder);
}

我在我的活动中实现它并将其传递给适配器

@Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
        mItemTouchHelper.startDrag(viewHolder);
    }

我的onMove方法是这样的

 @Override
  public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                      RecyclerView.ViewHolder target) {
    mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
    return true;
}

在我的适配器中,我有一个来自界面的onItemMove

public interface ItemTouchHelperAdapter {
  boolean onItemMove(int fromPosition, int toPosition);
  void onItemDismiss(int position);
}


@Override
  public boolean onItemMove(int fromPosition, int toPosition) {
    if (fromPosition < toPosition){
        for (int i = fromPosition; i < toPosition; i++){
            Collections.swap(cardMakerList,i,i+1);
        }
    }else{
        for (int i = fromPosition; i > toPosition; i--){
            Collections.swap(cardMakerList,i,i-1);
        }
    }
    notifyItemMoved(fromPosition,toPosition);
    return true;
}
android adapter drag motionevent
1个回答
1
投票

尝试添加以下内容并运行:

holder.handleView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (MotionEventCompat.getActionMasked(event)){
                case MotionEvent.ACTION_DOWN:
                    Log.d("Adptr" , "ACTION_DOWN");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation_pressed));
                    //mDragStartListener.onStartDrag(holder);
                    return true;
                case MotionEvent.ACTION_MOVE:
                        Log.d("Adptr" , "ACTION_MOVE");
                        return true;
                case MotionEvent.ACTION_UP:
                    Log.d("Adptr" , "ACTION_UP");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
                case MotionEvent.ACTION_CANCEL:
                    Log.d("Adptr" , "ACTION_CANCEL");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
            }
            return false;
        }
    });

根据您的代码,流程不正确。如果您在ACTION_UP之前的任何Motion动作上返回false,则不会调用ACTION_UP的代码。

P.S:我更喜欢从onTouch返回true,除非我必须处理特殊情况。如果有特殊情况返回适当的布尔值。

holder.handleView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (MotionEventCompat.getActionMasked(event)){
                case MotionEvent.ACTION_DOWN:
                    Log.d("Adptr" , "ACTION_DOWN");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation_pressed));
                    //mDragStartListener.onStartDrag(holder);
                    return true;
                case MotionEvent.ACTION_UP:
                    Log.d("Adptr" , "ACTION_UP");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
                case MotionEvent.ACTION_CANCEL:
                    Log.d("Adptr" , "ACTION_CANCEL");
                    holder.cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.elevation));
                    break;
            }
            return true;
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.