使Recycler视图项不可刷新

时间:2018-06-01 06:08:55

标签: android android-recyclerview

在我的回收站视图中,我需要将标题项设置为不可转换。我重写方法getSwipeDirs()和getMovementFlags()并返回0作为标题位置,但现在它也能够滑动。我的代码出了什么问题。

Nb:我只是向一个方向滑动(从左到右)

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import com.example.shanto.inventory.adapters.MaterialAdapter;
import com.example.shanto.inventory.adapters.MaterialDetailAdapter;



public class RecyclerItemTouchHelper_MaterialDetail extends ItemTouchHelper.SimpleCallback {
    private RecyclerItemTouchHelperListener listener;

    public RecyclerItemTouchHelper_MaterialDetail(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {
        super(dragDirs, swipeDirs);
        this.listener = listener;
    }

   /* @Override
    public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        *//*if (viewHolder instanceof MaterialDetailAdapter.HeaderVH) return 0;
        return super.getSwipeDirs(recyclerView, viewHolder);*//*
        int position = viewHolder.getAdapterPosition();
        return position == 0 ? 0 : super.getSwipeDirs(recyclerView, viewHolder);
    }*/
    @Override
    public int getMovementFlags(RecyclerView recyclerView,
                                RecyclerView.ViewHolder viewHolder) {
        if (viewHolder instanceof MaterialDetailAdapter.HeaderVH){
            return makeMovementFlags(0, 0);
        } else {
            return makeMovementFlags(0, super.getSwipeDirs(recyclerView, viewHolder));
        }
    }
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        return true;
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (viewHolder != null) {
            final View foregroundView = ((MaterialDetailAdapter.MaterialVH) viewHolder).item;

            getDefaultUIUtil().onSelected(foregroundView);
        }
    }

    @Override
    public void onChildDrawOver(Canvas c, RecyclerView recyclerView,
                                RecyclerView.ViewHolder viewHolder, float dX, float dY,
                                int actionState, boolean isCurrentlyActive) {
        final View foregroundView = ((MaterialDetailAdapter.MaterialVH) viewHolder).item;
        getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
                actionState, isCurrentlyActive);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        final View foregroundView = ((MaterialDetailAdapter.MaterialVH) viewHolder).item;
        getDefaultUIUtil().clearView(foregroundView);
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView,
                            RecyclerView.ViewHolder viewHolder, float dX, float dY,
                            int actionState, boolean isCurrentlyActive) {
        final View foregroundView = ((MaterialDetailAdapter.MaterialVH) viewHolder).item;

        getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
                actionState, isCurrentlyActive);
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());
    }

    @Override
    public int convertToAbsoluteDirection(int flags, int layoutDirection) {
        return super.convertToAbsoluteDirection(flags, layoutDirection);
    }

    public interface RecyclerItemTouchHelperListener {
        void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
    }
}

在这里,我更新了用于滑动的代码

1 个答案:

答案 0 :(得分:0)

很抱歉,我使用以下代码在活动中滑动

ItemTouchHelper.SimpleCallback itemTouchHelperCallback1 = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT ) {
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                // Row is swiped from recycler view
                // remove it from adapter
            }

            @Override
            public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }
        };

其中SimpleCallback(0,ItemTouchHelper.LEFT)使项目总是向左滑动,这让我很困惑。