如何更新嵌套RecyclerView项目的项?

5

我有一个RecyclerView(父级)在Fragment中填充了一些RecyclerViews(子级),我想通过某个操作(例如点击操作)直接从Fragment更新每个嵌套的RecyclerView(子级),如何以完美和高效的方式实现此目标?

初始的ParentAdapter

     ParentAdapter adapter = new ParentAdapter(dataSet);
     mBinding.recyclerView.setAdapter(adapter);

ParentAdapter.java

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    private final RecyclerView.RecycledViewPool viewPool;
    private List<CartItemCategory> dataSet;

    public MainAdapter(List<CartItemCategory> dataSet) {

        this.dataSet = dataSet;
        
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ShoppingCardParentItemLayoutBinding mBinding = ShoppingCardParentItemLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(mBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.mBinding.setCartItemCategory(dataSet.get(position));

        ChildAdapter childAdapter = new ChildAdapter(dataSet.get(position).getCartItems());
        holder.mBinding.recyclerView.setHasFixedSize(true);
        holder.mBinding.recyclerView.setAdapter(childAdapter);
        holder.mBinding.recyclerView.setRecycledViewPool(viewPool);
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ShoppingCardParentItemLayoutBinding mBinding;

        ViewHolder(@NonNull ShoppingCardParentItemLayoutBinding mBinding) {
            super(mBinding.getRoot());
            this.mBinding = mBinding;
        }
    }

}

ChildAdapter.java

public class ChildAdapter extends RecyclerView.Adapter<InnerAdapter.ViewHolder> {

    private List<CartItem> cartItems;

    public ChildAdapter(List<CartItem> cartItems) {
        this.cartItems = cartItems;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ShoppingCardItemLayoutBinding mBinding = ShoppingCardItemLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(mBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.mBinding.setCartItem(cartItems.get(position));
    }

    @Override
    public int getItemCount() {
        return cartItems.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ShoppingCardItemLayoutBinding mBinding;

        ViewHolder(@NonNull ShoppingCardItemLayoutBinding mBinding) {
            super(mBinding.getRoot());
            this.mBinding = mBinding;
            }
    }

}

父RecyclerView

<androidx.recyclerview.widget.RecyclerView
  android:id="@+id/recycler_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
>

子项布局

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
1个回答

8

在您的子适配器中,您可以创建一些诸如以下方法的插入、更新、删除等方法:

public class ChildAdapter extends RecyclerView.Adapter<InnerAdapter.ViewHolder> {
// ...
public void update(int position, CartItem newCartItem) {
  cartItems.set(position, newCartItem);
  notifyDataSetChanged();                    // <--- THIS IS THE MAIN THING
}

public void add(CartItem newCartItem) {
  cartItems.add(newCartItem);
  notifyDataSetChanged();                    // <--- THIS IS THE MAIN THING
}

public void delete(int position) {
  cartItems.remove(position);
}

// ...
}

之后,您可以在按钮单击事件中像这样调用这些方法:

Button btn = findViewById(R.id.btn);
btn.setOnClickListener( v -> {
  CartItem cartItem = new CartItem(); // <-- initialize properly
  childAdapter.add(newCartItem); 
// OR to update
// childAdapter.update(position,  newCartItem);  // <-- the position where you want to update 

// OR to delete
// childAdapter.delete(position); // <-- item at position that you want to delete

});

我认为您可以用以下方法完成:

如何访问每个子项的 childAdapter?

我认为您需要以下 2 个步骤:

  1. 获取 RecyclerView 中第 i 个位置的 viewHolder
  2. 从 recyclerView 获取 Adapter

您可以按照以下方式获取 viewHolder: ViewHolder vh = myRecyclerView.findViewHolderForAdapterPosition(pos); (参考资料)

而要获取 adapter,可以这样写:ChildAdapter adapter = (ChildAdapter)mRecyclerView.getAdapter();

所以,假设您想要从 YourActivity / YourFragment 中获取 childAdapter。将以上两者结合起来,您可以像这样实现:

//... inside your activity / fragment
 
private void myUpdate(int i, int j, CartItem cartItem) {
  // 1. get ith item of the parent recyclerView
  ViewHolder ithChildViewHolder = parentRecyclerView.
                                  findViewHolderForAdapterPosition(pos); 
  // 2. get it's recyclerview
  RecyclerView ithChildsRecyclerView = ithChildViewHolder.
                                       mBinding.
                                       recyclerView;
  // 3. get ithRecyclerView's adapter
  ChildAdapter ithChildAdapter = (ChildAdapter) ithChildsRecyclerView.getAdapter();

  // 4. now you should be able to do updates
  //    update at position j with new item
  ithChildAdapter.update( j, newCartItem); 
 
}

请注意,我是在没有项目的情况下编写的。因此可能会有一些小错误。但应该是这样的。请相应地进行更改。尝试一下,让我知道是否有效。祝你好运。

1
嗨,谢谢您的回复,我如何访问每个子项的childAdapter? - Danial Nazari
1
你好。回复晚了很抱歉。我已经更新了我的答案。请查看一下。 - Qazi Fahim Farhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接