【问题标题】:RecyclerView with different ItemTouchHelper for each item on the listRecyclerView 为列表中的每个项目使用不同的 ItemTouchHelper
【发布时间】:2020-08-14 00:08:13
【问题描述】:

我正在尝试为列表中的每个项目实现具有不同 ItemTouchHelper 的 Recycleview。

我知道的唯一方法是将 ItemTouchHelper 直接添加到 RecycleView 而不是项目。

我正在尝试做的示例:

我有一个包含 4 个项目的列表,我可以向左滑动所有项目。

  • 第一个项目将显示一个删除按钮。

  • 第二个项目将显示删除按钮和编辑按钮。

  • 第三项显示一个删除按钮。

  • 第四项显示复制、删除和编辑按钮。

*列表可以有很多项。

有人知道怎么做吗?

【问题讨论】:

    标签: android android-recyclerview swipe itemtouchhelper android-swipe


    【解决方案1】:

    想法

    所以基本上你的问题是关于如何根据项目类型为每个 RecyclerView 项目添加一个唯一的 ItemTouchHelper

    没有深入了解您希望每个项目如何在ItemTouchHelper 滑动操作中有所不同的细节,就像您所说的添加一些按钮功能,如复制、编辑和删除。我将直接指出如何区分ItemTouchHelper 对不同项目的滑动。

    步骤

    第 1 步:使用 POJO 字段区分 RecyclerView 项目

    因此,首先您需要在 POJO 中创建一个字段(通常是 intenum),以区分不同的项目。

    第 2 步:实现自定义 ItemTouchHelper.SimpleCallback

    创建一个自定义ItemTouchHelper.SimpleCallback 类,将RecyclerView 项的列表带入其构造函数。

    接下来,覆盖ItemTouchHelperRecyclerViewonDraw() 回调上调用的onChildDraw();这是正确的地方,因为它在 RecyclerView 绘制其单个项目时被调用。

    因此,在此方法中,您可以实现滑动时每个项目的外观。并且由于它需要一个 ViewHolder 实例,因此您可以通过ViewHolder.getAdapterPosition() 获取滑动项目的位置,并从提供的项目列表中获取该特定位置的滑动项目。

    示例

    这是一个简单的示例,它是一个颜色列表,当您滑动某个项目时,它会反映背景颜色。

    看起来是这样的:

    POJO

    对于上述第 1 步,我将值存储到 colorValue 字段中

    class ColorItem {
    
        String colorName;
        int colorValue;
    
        public ColorItem(String colorName, int colorValue) {
            this.colorName = colorName;
            this.colorValue = colorValue;
        }
    
        public String getColorName() {
            return colorName;
        }
    
        public void setColorName(String colorName) {
            this.colorName = colorName;
        }
    
        public int getColorValue() {
            return colorValue;
        }
    
        public void setColorValue(int colorValue) {
            this.colorValue = colorValue;
        }
    }
    

    RecyclerView 适配器(没有花哨的代码)

    public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.CustomViewHolder> {
    
        List<ColorItem> mColors;
    
        // Constructor
        RecyclerAdapter(List<ColorItem> colors) {
            this.mColors = colors;
        }
    
        @NonNull
        @Override
        public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            View listItem = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item, parent, false);
            return new CustomViewHolder(listItem);
        }
    
        @Override
        public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
            holder.tvColorName.setText(mColors.get(position).getColorName());
        }
    
        @Override
        public int getItemCount() {
            return mColors.size();
        }
    
        class CustomViewHolder extends RecyclerView.ViewHolder implements {
    
            TextView tvColorName;
    
            CustomViewHolder(@NonNull View listItem) {
                super(listItem);
                tvColorName = listItem.findViewById(R.id.tvColorName);
            }
    
          
        }
    }
    

    自定义ItemTouchHelper.SimpleCallback

    
    public class ItemSwipeCallback extends ItemTouchHelper.SimpleCallback {
    
        private final List<ColorItem> mColorItems;
        private Context mContext;
    
        public interface OnTouchListener {
            void onSwiped(RecyclerView.ViewHolder viewHolder, int direction);
        }
    
        private OnTouchListener mOnTouchListener;
    
        public ItemSwipeCallback(Context context, List<ColorItem> items, int dragDirs, int swipeDirs, OnTouchListener onTouchListener) {
            super(dragDirs, swipeDirs);
            mContext = context;
            mColorItems = items;
            mOnTouchListener = onTouchListener;
        }
    
    
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }
    
        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            mOnTouchListener.onSwiped(viewHolder, direction);
        }
    
        @Override
        public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    
            // Getting the swiped item
            ColorItem item = mColorItems.get(viewHolder.getAdapterPosition());
    
            // Get the color of the swiped item (the thing that differentiates among items)
            ColorDrawable background = new ColorDrawable(mContext.getResources().getColor(item.getColorValue()));
    
            // Changing the color of the background item
            View itemView = viewHolder.itemView;
            int backgroundCornerOffset = 25; //so mBackground is behind the rounded corners of itemView
    
            if (dX > 0) { // Swiping to the right
                background.setBounds(itemView.getLeft(), itemView.getTop(),
                        itemView.getLeft() + ((int) dX) + backgroundCornerOffset, itemView.getBottom());
            } else if (dX < 0) { // Swiping to the left
                background.setBounds(itemView.getRight() + ((int) dX) - backgroundCornerOffset,
                        itemView.getTop(), itemView.getRight(), itemView.getBottom());
            } else { // view is unSwiped
                background.setBounds(0, 0, 0, 0);
            }
    
            background.draw(c);
    
        }
    }
    

    活动

    public class MainActivity extends AppCompatActivity {
    
        ArrayList<ColorItem> mColors;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mColors = new ArrayList<>();
            populateColors();
            setupRecyclerView();
    
        }
    
        private void setupRecyclerView() {
            RecyclerAdapter adapter = new RecyclerAdapter(this, mColors);
            RecyclerView recyclerview = findViewById(R.id.recyclerview);
            RecyclerView.LayoutManager layoutMgr = new LinearLayoutManager(getApplicationContext());
            recyclerview.setLayoutManager(layoutMgr);
            recyclerview.setAdapter(adapter);
    
            ItemTouchHelper helper = new ItemTouchHelper(new ItemSwipeCallback(this, mColors,
                    0, ItemTouchHelper.RIGHT, new ItemSwipeCallback.OnTouchListener() {
    
                @Override
                public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                    // Do something here
                }
            }));
    
            helper.attachToRecyclerView(recyclerview);
        }
    
        private void populateColors() {
            mColors.add(new ColorItem("Red", R.color.red));
            mColors.add(new ColorItem("White", R.color.white));
            mColors.add(new ColorItem("Green", R.color.green));
            mColors.add(new ColorItem("Yellow", R.color.yellow));
            mColors.add(new ColorItem("Black", R.color.black));
            mColors.add(new ColorItem("Red", R.color.red));
            mColors.add(new ColorItem("White", R.color.white));
            mColors.add(new ColorItem("Green", R.color.green));
            mColors.add(new ColorItem("Yellow", R.color.yellow));
            mColors.add(new ColorItem("Black", R.color.black));
            mColors.add(new ColorItem("Red", R.color.red));
            mColors.add(new ColorItem("White", R.color.white));
            mColors.add(new ColorItem("Green", R.color.green));
            mColors.add(new ColorItem("Yellow", R.color.yellow));
            mColors.add(new ColorItem("Black", R.color.black));
        }
    
    }
    

    【讨论】:

    • 喜欢你的解释。它帮助我实施了我的解决方案。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    相关资源
    最近更新 更多