我正在使用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);

}

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐