4 回答

TA贡献1863条经验 获得超2个赞
公共类 GovtjobsAdapter 扩展 RecyclerView.Adapter {
private List<GovtjobBean> govtjobList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tv_govtjob_title,tv_govtjob_lastdatetoapply,tv_govtjob_location;
public MyViewHolder(View view) {
super(view);
tv_govtjob_title = (TextView) view.findViewById(R.id.tv_govtjobs_title);
tv_govtjob_lastdatetoapply = (TextView) view.findViewById(R.id.tv_govtjob_lastdatetoapply);
tv_govtjob_location = (TextView) view.findViewById(R.id.tv_govtjob_location);
}
}
public GovtjobsAdapter(List<GovtjobBean> govtjobList) {
this.govtjobList = govtjobList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.govtjob_lists, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
GovtjobBean govtjoblist = govtjobList.get(position);
holder.tv_govtjob_title.setText(govtjoblist.getGovt_title());
holder.tv_govtjob_lastdatetoapply.setText(govtjoblist.getGovt_lastdatetoapply());
holder.tv_govtjob_location.setText(govtjoblist.getGovt_location());
}
@Override
public int getItemCount() {
return govtjobList.size();
}
}

TA贡献1895条经验 获得超7个赞
您必须使用 recyclerview ViewTypes,检查此链接中的示例:
Android RecyclerView 示例——多个 ViewType
或者你可以使用像 FastAdapter 这样的库,它是处理适配器项类型的很棒的库:
https://github.com/mikepenz/FastAdapter

TA贡献1836条经验 获得超4个赞
您将需要创建一个自定义回收器视图类,您可以在其中重写 onCreateViewHolder(ViewGroup parent, Int viewType)。
这是我的例子:
(1) 在您的 recyclerview 类中创建自定义视图持有者类。例子:
class ViewHolderCurrent extends RecyclerView.ViewHolder {
TextView locationTV;
TextView conditionTV;
...
public ViewHolderCurrent(View listItemView) {
super(listItemView);
locationTV = listItemView.findViewById(R.id.location_main_4);
conditionTV = listItemView.findViewById(R.id.condition_main_4);
...
}
}
(2) 将 onCreateViewHolder 覆盖到您的自定义视图持有者:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 1:
View viewCurrent = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item4, parent, false);
return new ViewHolderCurrent(viewCurrent);
case 2:
...
case 3:
...
}
return null;
}
(3)重写 onBindViewHolder() 以填充您选择的查看器。例子:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case 1:
ViewHolderCurrent viewHolderCurrent = (ViewHolderCurrent) holder;
viewHolderCurrent.locationTV.setText(*text*);
viewHolderCurrent.conditionTV.setText(*text*);
break;
case 2:
...
}
}
我的完整源代码在这里

TA贡献1815条经验 获得超6个赞
您可以使用getItemViewType(int position)
适配器的方法来执行此操作。在此方法中,您可以检查当前位置的项目类型(图像、视频或文本),并为每个项目返回一个特定值。然后在您的中,onCreateViewHolder(ViewGroup parent, Int viewType)
您可以使用该viewType
参数来扩大您的观点。
添加回答
举报