gpt4 book ai didi

android - header TextView 与 RecyclerView

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:10 26 4
gpt4 key购买 nike

我正在使用 RecyclerView + CardView现在要显示文本列表,我正在尝试添加一个普通的 TextView标题到 RecyclerView 的顶部所以我在适配器中有两种布局,但现在它在应用程序中显示如下:

enter image description here

它必须看起来像这样:

enter image description here

所以问题可能出在 getItemCount 上和 getItemViewType我必须更改什么,以便标题仅显示一次,然后是 CardViews .

public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static final int LAYOUT_ONE = 0;
private static final int LAYOUT_TWO = 1;

List<AdapterData> 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) {
return position % 2;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch ( viewType ) {

case LAYOUT_ONE: return new ViewHolderFirst(LayoutInflater.from(parent.getContext()).inflate(R.layout.top_textview, parent, false));

case LAYOUT_TWO: return new ViewHolderSecond(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_card_item, parent, false));
}

return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)) {

case LAYOUT_ONE:
ViewHolderFirst viewHolderFirst = (ViewHolderFirst)holder;

break;

case LAYOUT_TWO:
ViewHolderSecond viewHolderSecond = (ViewHolderSecond)holder;
AdapterData data = mItems.get(position);
viewHolderSecond.code.setText(data.getCode());
viewHolderSecond.result.setText(data.getResult());

break;
}
}

@Override
public int getItemCount() {
return mItems.size();
}



class ViewHolderFirst extends RecyclerView.ViewHolder {
public TextView header;

public ViewHolderFirst(View itemView) {
super(itemView);
header = (TextView)itemView.findViewById(R.id.header_textview);

}
}

class ViewHolderSecond extends RecyclerView.ViewHolder{
public TextView code;
public TextView result;

public ViewHolderSecond(View itemView) {
super(itemView);
code = (TextView)itemView.findViewById(R.id.sims_code);
result = (TextView)itemView.findViewById(R.id.sims_result);

}
}


}

最佳答案

在 getItemViewType() 中试试这个

@Override
public int getItemViewType(int position) {
return (position==0) ? 0 : 1; //try this line
}

关于android - header TextView 与 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30909001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com