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
).
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
}
}
}