gpt4 book ai didi

android - RecyclerView 为列表中的每个项目使用不同的 ItemTouchHelper

转载 作者:行者123 更新时间:2023-12-04 09:08:20 26 4
gpt4 key购买 nike

我正在尝试为列表中的每个项目实现一个具有不同 ItemTouchHelper 的 Recycleview。
我知道的唯一方法是将 ItemTouchHelper 直接添加到 RecycleView 而不是项目。
我正在尝试做的示例:
我有一个包含 4 个项目的列表,所有项目我都可以向左滑动。

  • 第一个项目将显示一个删除按钮。
  • 第二项将显示删除按钮和编辑按钮。
  • 第三项显示删除按钮。
  • 第四项显示复制、删除和编辑按钮。

  • *列表可以有很多项目。
    有人知道怎么做吗?

    最佳答案

    想法
    所以基本上你的问题是关于如何添加一个独特的 ItemTouchHelper每个RecyclerView项目基于项目类型。
    无需深入了解您希望每个项目在 ItemTouchHelper 中有何不同的细节。滑动操作,就像您说的添加一些按钮功能,如复制、编辑和删除。我会说到点子上 有什么不同ItemTouchHelper的滑动不同的项目。
    步骤
    第一步:区分RecyclerView使用 POJO 字段的项目
    因此,首先您需要在 POJO 中创建一个字段(通常是 intenum )以区分不同的项目。
    第 2 步:实现自定义 ItemTouchHelper.SimpleCallback
    创建自定义 ItemTouchHelper.SimpleCallback获取 RecyclerView 列表的类items 到它的构造函数中。
    接下来,覆盖 onChildDraw() ItemTouchHelper 调用在 RecyclerViewonDraw()打回来;这是正确的地方,因为它在 RecyclerView 绘制其单个项目时被调用。
    因此,在此方法中,您可以实现滑动时每个项目的外观。并且因为它需要一个 ViewHolder 实例,所以你可以用 ViewHolder.getAdapterPosition() 获得被刷过的项目位置。 ,并从提供的项目列表中,您可以获取此特定位置的刷卡项目。
    示例
    这是一个简单的示例,它是一个颜色列表,每当您滑动某个项目时,它就会反射(reflect)背景颜色。
    这是它的样子:

    POJO
    对于上面提到的第 1 步,我将值存储到 colorValue 中。 field

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

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

    }

    关于android - RecyclerView 为列表中的每个项目使用不同的 ItemTouchHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63404794/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com