android新闻页面布局,android-具有多个布局的RecyclerView
我正在使用RecyclerView在CardView中显示文本,但我想在RecyclerView的顶部添加一个Header TextView,如下所示:因此,我仅使用TextView制作了另一个布局文件“ top_header.xml”,但是我不确定如何修改适配器以在其中获得两个布局.public class Adapter extends RecyclerView.Adapter {List m
我正在使用RecyclerView在CardView中显示文本,但我想在RecyclerView的顶部添加一个Header TextView,如下所示:
因此,我仅使用TextView制作了另一个布局文件“ top_header.xml”,但是我不确定如何修改适配器以在其中获得两个布局.
public class Adapter extends RecyclerView.Adapter {
List mItems;
public Adapter() {
super();
mItems = new ArrayList();
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);
}
}
}
更多推荐
所有评论(0)