4 回答

TA貢獻1863條經(jīng)驗 獲得超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條經(jīng)驗 獲得超7個贊
您必須使用 recyclerview ViewTypes,檢查此鏈接中的示例:
Android RecyclerView 示例——多個 ViewType
或者你可以使用像 FastAdapter 這樣的庫,它是處理適配器項類型的很棒的庫:
https://github.com/mikepenz/FastAdapter

TA貢獻1836條經(jīng)驗 獲得超4個贊
您將需要創(chuàng)建一個自定義回收器視圖類,您可以在其中重寫 onCreateViewHolder(ViewGroup parent, Int viewType)。
這是我的例子:
(1) 在您的 recyclerview 類中創(chuàng)建自定義視圖持有者類。例子:
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條經(jīng)驗 獲得超6個贊
您可以使用getItemViewType(int position)
適配器的方法來執(zhí)行此操作。在此方法中,您可以檢查當前位置的項目類型(圖像、視頻或文本),并為每個項目返回一個特定值。然后在您的中,onCreateViewHolder(ViewGroup parent, Int viewType)
您可以使用該viewType
參數(shù)來擴大您的觀點。
添加回答
舉報