0

I'm using a ConcatAdapter to set a header for my RecyclerView. I am using two RecyclerAdapters, one that requires no LayoutManager and one that requires a GridLayoutManager. How do I correctly set a LayoutManager inside the RecyclerAdapter class? I've tried doing it, and it doesn't throw any errors, but the data doesn't display.

@NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.viewholder_card_preview, parent, false);

        int spacing = GridAdapterSpacingUtils.convertIntToDP(context, GRID_VIEW_SPACING);

        RecyclerView recyclerView = view.findViewById(R.id.recyclerview);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, GRID_VIEW_ROWCOUNT);
        recyclerView.addItemDecoration(new GridAdapterItemDecoration(
                GRID_VIEW_ROWCOUNT, spacing, false)
        );

        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(this);
        
        return new ViewHolder(view);
    }

No Syntax errors, but I'm unsure what I am doing incorrectly. (The Grid Recycler works fine when I test it without a ConcatAdapter).

enter image description here

This is the Kotlin syntax from the Medium post I followed so I'm not sure what the equivalent Java Syntax is.

class BaseGridConcatAdapter(private val context: Context, private val animalAdapter: AnimalAdapter,private val spanCount:Int) :
    RecyclerView.Adapter<BaseConcatHolder<*>>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseConcatHolder<*> {
        val view = LayoutInflater.from(context).inflate(R.layout.animal_concat_row,parent,false)
        view.rv_animal_concat.layoutManager = GridLayoutManager(context, spanCount)
        return ConcatViewHolder(view)
    }

    override fun getItemCount(): Int  = 1

    override fun onBindViewHolder(holder: BaseConcatHolder<*>, position: Int) {
        when(holder){
            is ConcatViewHolder -> holder.bind(animalAdapter)
            else -> throw IllegalArgumentException("No viewholder to show this data, did you forgot to add it to the onBindViewHolder?")
        }
    }

    inner class ConcatViewHolder(itemView: View): BaseConcatHolder<AnimalAdapter>(itemView){
        override fun bind(adapter: AnimalAdapter) {
            itemView.rv_animal_concat.adapter = adapter
        }
    }
}

https://medium.com/@gsaillen95/how-to-inflate-different-layoutmanagers-for-each-adapter-with-concatadapter-76cdee6266ca

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83

2 Answers2

1

Don't set the adapter to the RecyclerView in the adapter,

set it in the activity or in fragment.

for more than one layouts, use ViewType. you can also use Grid view layout with setSpanSize() to set up the layouts.

check out this image. i am working on it now example

working on this kind of work now

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
0

When using extends RecyclerView.Adapter<RecyclerView.ViewHolder> there is a callback method for when the RecyclerView is attached, if you do it above and try getting reference to the RecyclerView in onCreateViewHolder it will return null.

@Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        //Change the layout manager here
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, GRID_VIEW_COLUMN_COUNT);
        recyclerView.setLayoutManager(gridLayoutManager);
    }

In my case, I was trying to add multiple LayoutManager types to a concat adapter (e.g HorizontalLayout, VerticalLayout, GridLayout managers). I still don't know if this is possible, but if someone has an answer for this, please post it and I can mark yours correct.

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83