Nested Recyclerview with dynamic data

1.8k Views Asked by At

I want to display Horizontal list inside vertical list. If initially i set data , it is displaying fine. but i need to update inner list dynamically. but inner list onBindViewHolder is not getting called when updating.

Main adapter

  private RecyclerView recyclerColors; 

 public class MyViewHolder extends RecyclerView.ViewHolder {
    private FilterColorAdapter mFilterColorAdpater;

  public MyViewHolder(View view) {
        super(view);

   recyclerColors =  view.findViewById(R.id.recyclerColors);
        final LinearLayoutManager linearLayoutManagerVideos = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        recyclerColors.setLayoutManager(linearLayoutManagerVideos);
        mFilterColorAdpater = new FilterColorAdapter(context);
        recyclerColors.setAdapter(mFilterColorAdpater);
        recyclerColors.setNestedScrollingEnabled(false);
    }
}

 @Override
public void onBindViewHolder(MyViewHolder holder, final int position) {

  if(matched){
     holder.mFilterColorAdpater.setFilterColorList(movie.getFilterData());
            holder.mFilterColorAdpater.setRowIndex(position);
  }

My Inner adapter

public class FilterColorAdapter extends RecyclerView.Adapter<FilterColorAdapter.MyViewHolder> {

private List<String> listColors;
private Context mContext;
private int mRowIndex = -1;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public ImageView imgCircle;

    public MyViewHolder(View view) {
        super(view);
        imgCircle = view.findViewById(R.id.imgCircle);
    }
}

public FilterColorAdapter(Context mContext) {
    this.mContext = mContext;
}

public void setFilterColorList(List<String> listColors) {
    this.listColors = listColors;
    notifyDataSetChanged();
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder Rowholder, int position) {
    MyViewHolder holder = (MyViewHolder) Rowholder;
    Util.changeImageViewColor(holder.imgCircle, listColors.get(position));
}

 }
1

There are 1 best solutions below

4
On

Change as following, problem will be solved:

private RecyclerView recyclerColors; 

public class MyViewHolder extends RecyclerView.ViewHolder {

public MyViewHolder(View view) {
     super(view);

     recyclerColors =  view.findViewById(R.id.recyclerColors);
     final LinearLayoutManager linearLayoutManagerVideos = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
     recyclerColors.setLayoutManager(linearLayoutManagerVideos);
     recyclerColors.setNestedScrollingEnabled(false);
}

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {

    if(matched){
        FilterColorAdapter mFilterColorAdpater = new FilterColorAdapter(context);
        recyclerColors.setAdapter(mFilterColorAdpater);
        holder.mFilterColorAdpater.setFilterColorList(movie.getFilterData());
        holder.mFilterColorAdpater.setRowIndex(position);
    }
}
}

and:

public void setFilterColorList(List<String> listColors) {
    this.listColors.clear();
    this.listColors.addAll(listColors);
    notifyDataSetChanged();
}