问题描述
我想添加2种类型:
- with image
- li>
在我的 recyclerview
中。我知道我需要覆盖从 FirebaseRecyclerAdapter
的方法,但我不知道如何。
帮我请这个。
@Override
public int getItemViewType(int position){
return mModelLayout;
抽象保护无效populateViewHolder(VH viewHolder,T模型,int位置);
$ b所以我想从T模型类型的项目通过int(1 - 与图像,2 - 无),根据这个信息更改mModelLayout。我可以添加if else语句吗?解决方案这个答案建立在hatboysam的答案上。我花了很长时间才弄清楚所有事情是如何组合在一起的,所以我想我会在这里详细描述解决方案:
mRecyclerViewAdapter = new FirebaseRecyclerAdapter< DataObjectModel,ViewHolder>(
DataObjectModel.class,R.layout.defaultitem_layout,ViewHolder.class,mDatabaseRef){
$ b $ @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());
}
$ b $ @Override
public int getItemViewType(int position){
DataObjectModel dataobject = getItem(position);
if(dataobject.getType()!= null&& dataobject.getType()。equals(image)){
//带有图片的项目的布局
return R.layout.my_image_item);
} else if(dataobject.getType()!= null&& datobject.getType()。equals(noimage)){
//没有图片的项目布局
返回R.layout.my_no_image_item;
}
}
};
// getName()和setText()是您必须在public static class ChatHolder扩展了RecyclerView.ViewHolderclass
//这里是ViewHolder类的一个很好的测量方法
pre $ code> 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);
$ b $ public void setText(String text){
TextView field =(TextView)mView.findViewById(R.id.message_text);
field.setText(text);
}
}
I want to add 2 types:-
- with image
- without image
in my recyclerview
. I know that I need to override method from FirebaseRecyclerAdapter
but I don't know how.Help me please with this.
@Override
public int getItemViewType(int position) {
return mModelLayout;
}
abstract protected void populateViewHolder(VH viewHolder, T model, int position);
So I want to get from T-model type of item via int (1 - with image, 2 - without) and change mModelLayout basing on this info. Can I just add if else statement there ?
This answer builds on hatboysam's answer. It took me ages to figure out how everything fits together so I thought I'd describe the solution thoroughly here:
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() and setText() are methods you have to write in the "public static class ChatHolder extends RecyclerView.ViewHolder" class
// Here's the ViewHolder Class for good measure
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);
}
}
这篇关于FirebaseRecyclerAdapter和Android上的多项目类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!