nested recyclerview inside adapter

2.8k views Asked by At

I am developing an android app where I have used nested RecyclerView but I am not achieving what I want.I want to achieve on this list it should show Title and below multiple images like json below how can I achieve that I am using two viewholder and it should scrool as well. json structure

below Adapter class public class UranAdapter extends RecyclerView.Adapter {

public List<Exhibit> exhibitList;
public Context context;

public UranAdapter(List<Exhibit> uranList, Context context) {
    this.exhibitList = uranList;
    this.context = context;

}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


    View itemView;
    switch (viewType) {
        case Exhibit.TEXT_TYPE:
            itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exhibit_list, parent, false);
            return new ViewHolder(itemView);
        case Exhibit.IMAGE_TYPE:
            itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exhibit_list2, parent, false);
            return new ImageViewHolder(itemView);

    }
    return null;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

}


@Override
public int getItemViewType(int position) {


    return exhibitList.get(position).type;
}

public int getItemCount() {
    return exhibitList.size();
}


public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Exhibit exhibit = exhibitList.get(position);
    switch (exhibit.type) {

        case Exhibit.TEXT_TYPE:
            ((ViewHolder) holder).exhibition_textView.setText(exhibit.getTitle());

            break;


        case Exhibit.IMAGE_TYPE:
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ((ViewHolder) holder).exhibition_imageView.setImageResource(exhibit.image);
            }


            break;

    }

}



public static class ViewHolder extends RecyclerView.ViewHolder {


    public ImageView exhibition_imageView;
    TextView exhibition_textView;

    public ViewHolder(View view) {
        super(view);
        exhibition_textView = (TextView) view.findViewById(R.id.exhibition_textview);


    }
}
    public static class ImageViewHolder extends RecyclerView.ViewHolder {


        public ImageView exhibition_imageView;
        TextView exhibition_textView;

        public ImageViewHolder(View view) {
            super(view);
            exhibition_imageView = (ImageView) view.findViewById(R.id.exhibition_imageview);


        }
    }
}

below MainActivity

public class MainActivity extends AppCompatActivity {

public List<Exhibit> exhibitList = new ArrayList<>();
Context context;
RecyclerView recyclerView;


public UranAdapter uranAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ApiInterface apiInterface = ApiClient.getApiService();
    Call<ExhibitsLoader> call = apiInterface.getExhibitList();
    call.enqueue(new Callback<ExhibitsLoader>() {
        @Override
        public void onResponse(Call<ExhibitsLoader> call, Response<ExhibitsLoader> response) {
            exhibitList = response.body().getExhibitList();
            exhibitList.add(new Exhibit(Exhibit.IMAGE_TYPE));
            RecyclerView recyclerView = findViewById(R.id.recycler_view);
            recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            uranAdapter = new UranAdapter(exhibitList, context); // changes
            recyclerView.setAdapter(uranAdapter);

        }

        @Override
        public void onFailure(Call<ExhibitsLoader> call, Throwable t) {

        }
    });


}

}

below image what I want image

4

There are 4 answers

0
Davidson Silva On

Use Expandable CardView for this .. Like : https://www.youtube.com/watch?v=z9qScBaKfnM&t=727s

0
GensaGames On

You forget to call notifyDatasetChanged() after setting the adapter.

        uranAdapter = new UranAdapter(exhibitList, context); // changes
        recyclerView.setAdapter(uranAdapter);
        uranAdapter.notifyDatasetChanged();
1
Krzysztof Kubicki On

I think what you need here is flatMap the embedded image list into one-level list and you're going correctly to have two ViewHolder of different types, one for title, and one for image. Don't understand why your ViewHolders are static.

0
engspa12 On

Why don't you use two RecyclerViews, one inside the other?

One RecyclerView will have the title and a children RecyclerView. The second RecyclerView(the children RecyclerView) will have just the images.

Check this post: Recyclerview inside Recyclerview , get click position of child row inside parent Adapter