我想添加两种类型: –
>带图像
>没有形象
在我的recyclerview中.我知道我需要覆盖FirebaseRecyclerAdapter的方法,但我不知道如何.
请帮帮我.
@Override
public int getItemViewType(int position) {
return mModelLayout;
}
abstract protected void populateViewHolder(VH viewHolder, T model, int position);
所以我想通过int(1 – 带图像,2 – 没有)从T模型类型的项目中获取并根据此信息更改mModelLayout.我可以在那里添加if else语句吗?
最佳答案 这个答案建立在hatboysam的答案之上.我花了很长时间才弄清楚所有东西是如何组合在一起的,所以我想我会在这里彻底描述解决方案:
mRecyclerViewAdapter = new FirebaseRecyclerAdapter<DataObjectModel, ViewHolder>(
DataObjectModel.class, R.layout.defaultitem_layout, ViewHolder.class, mDatabaseRef) {
@Override
public void populateViewHolder(ViewHolder viewholder, final DataObjectModel dataobject, int position) {
if (getItemViewType(position) == R.layout.my_image_item){
viewholder.setName(dataobject.getName());
}
else if (getItemViewType(position) == R.layout.my_no_image_item{
viewholder.setText(dataobject.getText());
}
@Override
public int getItemViewType(int position) {
DataObjectModel dataobject = getItem(position);
if (dataobject.getType() != null && dataobject.getType().equals("image")) {
// Layout for an item with an image
return R.layout.my_image_item);
} else if (dataobject.getType() != null && dataobject.getType().equals("noimage")) {
// Layout for an item without an image
return R.layout.my_no_image_item;
}
}
};
// getName()和setText()是你必须在“公共静态类ChatHolder扩展RecyclerView.ViewHolder”类中编写的方法
//这是适合衡量标准的ViewHolder类
public static class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public ViewHolder (View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name) {
TextView field = (TextView) mView.findViewById(R.id.name_text);
field.setText(name);
}
public void setText(String text) {
TextView field = (TextView) mView.findViewById(R.id.message_text);
field.setText(text);
}
}