如何使用不同类型的项目制作 RecyclerView? [复制]
Posted
技术标签:
【中文标题】如何使用不同类型的项目制作 RecyclerView? [复制]【英文标题】:How to make RecyclerView with different types of items? [duplicate] 【发布时间】:2020-12-23 00:27:11 【问题描述】:我有不同类型的项目列表。在这种情况下如何制作 RecyclerView? 我有用户类。在这个类中,我有变量地址地址类型。地址类也有自己的变量。 我试着按照我的建议去做,但它不能正常工作。仅显示用户数据。地址数据不输出。
我的第一个项目类:
public class User
private Integer id;
private String name;
private String username;
private String email;
private Address address;
getter,setter
我的 seconf 项目类:
public class Address
private String street;
private String suite;
private String city;
getters, setters
我的适配器类:
public class ResultAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
private static int TYPE_ADDRESS = 1;
private static int TYPE_USER = 2;
private LayoutInflater layoutInflater;
private Context context;
private List<User> users;
public ResultAdapter(Context context, List<User> users)
this.users = users;
this.layoutInflater = LayoutInflater.from(context);
this.context = context;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
if (viewType == TYPE_ADDRESS)
return new AddressViewHolder (layoutInflater.inflate(R.layout.list_item, parent, false));
else if(viewType == TYPE_USER)
return new UsersViewHolder(layoutInflater.inflate(R.layout.list_item, parent, false));
else return new AddressViewHolder(layoutInflater.inflate(R.layout.list_item, parent, false));
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position)
if (holder instanceof AddressViewHolder)
Address address = new Address();
((AddressViewHolder) holder).city.setText(address.getCity());
else if (holder instanceof UsersViewHolder)
final User user = users.get(position);
((UsersViewHolder) holder).name.setText(user.getName());
@Override
public int getItemCount()
return users.size();
@Override
public int getItemViewType(int position)
if (position==1)
return TYPE_ADDRESS;
else
return TYPE_USER;
public static class AddressViewHolder extends RecyclerView.ViewHolder
final TextView city;
public AddressViewHolder(@NonNull View itemView)
super(itemView);
city = itemView.findViewById(R.id.city);
public static class UsersViewHolder extends RecyclerView.ViewHolder
final TextView name;
public UsersViewHolder(@NonNull View itemView)
super(itemView);
name = itemView.findViewById(R.id.name);
【问题讨论】:
谢谢。我认为这是我需要的。 如果我的回答对你有帮助,请标记为回答。谢谢 @Akki,我按照你的建议做了,做了一些改动。但现在只显示用户数据。这些地址不显示。请看,我改变了我的问题。 两个原因:一个是getItemViewType
内部如果 position 是 1 则返回类型地址。你也有用户类里面的地址,所以你需要解释你想要实现什么的问题
【参考方案1】:
public class GenericAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
private static int TYPE_ADDRESS = 1;
private static int TYPE_USER = 2;
private LayoutInflater layoutInflater;
private List<User> users;
private Context context;
public GenericAdapter(Context context, List<User> users)
this.users = users;
this.layoutInflater = LayoutInflater.from(context);
this.context = context;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
if(viewType == TYPE_ADDRESS)
return AddressViewHolder(layoutInflater.inflate(R.layout.list_item, parent, false));
else if(viewType == TYPE_USER)
return UsersViewHolder(layoutInflater.inflate(R.layout.list_item, parent, false));
else return AddressViewHolder(layoutInflater.inflate(R.layout.list_item, parent, false));
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position)
if(holder instanceof AddressViewHolder)
/*Your binding */
else if(holder instanceof UsersViewHolder)
/*Your binding */
@Override
public int getItemCount()
return users.size();
@Override
public int getItemViewType(int position)
/*Please specify your logic here of selecting the type optionally you can use the position to query the data and decide on it*/
return (position % 2 == 0 )? TYPE_ADDRESS : TYPE_USER;
class AddressViewHolder extends RecyclerView.ViewHolder
public AddressViewHolder(@NonNull View itemView)
super(itemView);
class UsersViewHolder extends RecyclerView.ViewHolder
public UsersViewHolder(@NonNull View itemView)
super(itemView);
【讨论】:
以上是关于如何使用不同类型的项目制作 RecyclerView? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
如何使用不同类型的重力制作 LayerImageDrawable