我正在使用 aConcatAdapter
为我的RecyclerView
. 我正在使用两个RecyclerAdapters
,一个不需要LayoutManager
,一个需要一个GridLayoutManager
。如何正确设置类LayoutManager
内RecyclerAdapter
?我试过这样做,它不会抛出任何错误,但数据不显示。
@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);
}
没有语法错误,但我不确定我做错了什么。(当我在没有 的情况下对其进行测试时,Grid Recycler 工作正常ConcatAdapter
)。
这是我关注的 Medium 帖子中的 Kotlin 语法,所以我不确定等效的 Java 语法是什么。
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
}
}
}