我有一个itemtouchhelper问题,我有一个arraylist,我在recyclerview中显示来上下拖动item。但有些东西我想锁上(不能拖走)。这是我的代码:

ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            return makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
                    ItemTouchHelper.DOWN | ItemTouchHelper.UP);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {

            int positionHolder = viewHolder.getAdapterPosition();
            int positionTarget = target.getAdapterPosition();
            Log.e(TAG, "onMove: "+positionHolder+"----"+positionTarget );
            positionInclu = mAdapter.getCashesSize() > 0 ? 0 : -1;
            if(positionInclu == 0) {
                positionExclu= mAdapter.getExcludedSize() > 0 ? mAdapter.getCashesSize()+1 : -1;
            }
            else {
                positionExclu = 0;
            }
            if(positionHolder == 0 || positionTarget == 0 || positionHolder == positionExclu || positionTarget == positionExclu){
                mAdapter.notifyItemMoved(positionHolder,positionHolder);
                return false;
            }
            if((positionHolder < positionExclu && positionTarget > positionExclu)||(positionHolder > positionExclu && positionTarget < positionExclu)) {
                mAdapter.notifyItemMoved(positionHolder,positionHolder);
                return false;
            }


            if(isFirst) {
                listFisrt.addAll(mAdapter.getAccountList());
                isFirst = false;
            }
            if(positionHolder < positionExclu || positionInclu == -1 || positionExclu == -1) {
                Collections.swap(mAdapter.getAccountList(), viewHolder.getAdapterPosition() - 1, target.getAdapterPosition() - 1);
                mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
            }
            else {
                Collections.swap(mAdapter.getAccountList(), viewHolder.getAdapterPosition() - 2, target.getAdapterPosition() - 2);
                mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
            }

            ArrayList<AccountItem> listAccountAfterEdit = mAdapter.getAccountList();
            int length = listAccountAfterEdit.size();

            for (int i = 0; i < length; i++) {
                listAccountAfterEdit.get(i).setmSortIndex(i);
            }

            if(!isEquals(listAccountAfterEdit, listFisrt)) {
                txtSave.setVisibility(View.VISIBLE);
            }
            else {
                txtSave.setVisibility(View.INVISIBLE);
            }
            return true;
        }

        @Override
        public boolean canDropOver(RecyclerView recyclerView, RecyclerView.ViewHolder current, RecyclerView.ViewHolder target) {
            return super.canDropOver(recyclerView, current, target);
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

        }
    });

positioninclude和positionexclu是我要锁定的位置(在recyclerview中的位置)

最佳答案

最后,经过对itemTouchhelper类的大量研究,我这样解决了这个问题。
我所做的是将标志设置为action_state_idle,以便停止拖放。希望它能帮助像我这样的人。
下面是Kotlin版本。如果在Java中任何人都需要相同的代码,我可以转换它。

//defines the enabled move directions in each state (idle, swiping, dragging).
    override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
        // The position i want to lock
        if (list[viewHolder.adapterPosition].trim().isEmpty()){
            return makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.DOWN or ItemTouchHelper.UP)
        }
        // The position i want to lock
        if (viewHolder.adapterPosition == list.size - 1){
            return makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.DOWN or ItemTouchHelper.UP)
        }
        // else enabling ACTION_STATE_DRAG
        return makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
                ItemTouchHelper.DOWN or ItemTouchHelper.UP or ItemTouchHelper.START or ItemTouchHelper.END)
    }

07-24 09:47