我使用RecyclerView顯示在CardView文字,但我想在那個看起來像這樣的RecyclerView的頂部添加頁眉的TextView:RecyclerView提供多種版面
所以我做了另一個佈局文件「top_header.xml」只有一個TextView,但我不確定如何修改適配器以在其中獲取兩個佈局。
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
List<AdapterData> mItems;
public Adapter() {
super();
mItems = new ArrayList<AdapterData>();
AdapterData data = new AdapterData();
data.setCode("dummytext");
data.setResult("dummytext");
mItems.add(data);
data = new AdapterData();
data.setCode("dummytext");
data.setResult("dummytext");
mItems.add(data);
data = new AdapterData();
data.setCode("dummytext");
data.setResult("dummytext");
mItems.add(data);
data = new AdapterData();
data.setCode("dummytext");
data.setResult("dummytext");
mItems.add(data);
data = new AdapterData();
data.setCode("dummytext");
data.setResult("dummytext");
mItems.add(data);
}
@Override
public int getItemViewType(int position) {
int viewType = 0;
return viewType;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 0) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.top_textview, parent, false));
}
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_card_item, parent, false));
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
AdapterData data = mItems.get(position);
viewHolder.code.setText(data.getCode());
viewHolder.result.setText(data.getResult());
}
@Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder1 extends RecyclerView.ViewHolder{
public TextView header;
public ViewHolder1(View itemView) {
super(itemView);
header = (TextView)itemView.findViewById(R.id.header_textview);
}
}
class ViewHolder2 extends RecyclerView.ViewHolder{
public TextView code;
public TextView result;
public ViewHolder2(View itemView) {
super(itemView);
code = (TextView)itemView.findViewById(R.id.sims_code);
result = (TextView)itemView.findViewById(R.id.sims_result);
}
}
}
ok我已經到了這一點,我認爲它的權利,但不知道當前onBindViewHolder代碼必須創建另一個BindHolder *代碼更新* –