recyclerview 中的多个 Headers 不起作用 - 为什么会这样?
Multiple Headers in recycleview not working - why so?
我想用多个 header 创建回收视图,第一个 header 工作得很好,但其余 header 没有按预期工作。这是我第一次使用 recycleview headers 。我也想知道这样做是否正确。
这是我的适配器。
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = RecyclerViewAdapter.class.getSimpleName();
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private List<ItemObject> itemObjects;
private Context context;
public RecyclerViewAdapter( Context context , List<ItemObject> itemObjects) {
this.context = context;
this.itemObjects = itemObjects;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_layout, parent, false);
return new HeaderViewHolder(layoutView);
} else if (viewType == TYPE_ITEM) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new ItemViewHolder(layoutView , context);
}
throw new RuntimeException("No match for " + viewType + ".");
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
ItemObject mObject = itemObjects.get(position);
if(holder instanceof HeaderViewHolder){
((HeaderViewHolder) holder).headerTitle.setText(mObject.getContents());
}else if(holder instanceof ItemViewHolder){
((ItemViewHolder) holder).itemContent.setText(mObject.getContents());
}
}
private ItemObject getItem(int position) {
return itemObjects.get(position);
}
@Override
public int getItemCount() {
return itemObjects.size();
}
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
}
这里是我的主要方法activity。
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this , getDataSource());
recyclerView.setAdapter(adapter);
private List <ItemObject> getDataSource(){
List<ItemObject> list1 = new ArrayList <ItemObject>();
list1.add(new ItemObject("First Header",true));
list1.add(new ItemObject("This is the item content in the first position"));
list1.add(new ItemObject("This is the item content in the second position"));
List <ItemObject> list2 = new ArrayList <ItemObject>();
list2.add(new ItemObject("Second Header",true));
list2.add(new ItemObject("This is the item content in the first position"));
list2.add(new ItemObject("This is the item content in the second position"));
List <ItemObject> list3 = new ArrayList <ItemObject>();
list3.add(new ItemObject("Third Header",true));
list3.add(new ItemObject("This is the item content in the first position"));
list3.add(new ItemObject("This is the item content in the second position"));
List <ItemObject> finalList = new ArrayList <ItemObject>(list1);
finalList.addAll(list1);
finalList.addAll(list2);
finalList.addAll(list3);
return finalList;
[![I get first header 2 times][1]][1]
}
这是我的商品 object class.
public class ItemObject {
private String contents;
boolean isHeader ;
public ItemObject(String contents, boolean isHeader) {
this.contents = contents;
this.isHeader = isHeader;
}
public ItemObject(String contents) {
this.contents = contents;
}
public String getContents() {
return contents;
}
public boolean isHeader() {
return isHeader;
}
}
我第一个header 2次这样https://imgur.com/a/2YZ8DxM
List.addAll()
只是连接作为参数传递给 finalList
的 List
的项目。也就是说,您最终得到一个包含 9 个项目的 List
。这就是为什么你的条件 position == 0
只适用于你的第一个 header.
一个可能的(简单)解决方案是修改您的 ItemObject
以具有一个标志,指示给定项目是 header(注意这是构造函数的第二个参数,true
是 header,否则 false
)。
private List <ItemObject> getDataSource(){
List<ItemObject> list1 = new ArrayList <ItemObject>();
list1.add(new ItemObject("First Header", true));
list1.add(new ItemObject("This is the item content in the first position", false));
list1.add(new ItemObject("This is the item content in the second position", false));
List <ItemObject> list2 = new ArrayList <ItemObject>();
list2.add(new ItemObject("Second Header", true));
list2.add(new ItemObject("This is the item content in the first position", false));
list2.add(new ItemObject("This is the item content in the second position", false));
List <ItemObject> list3 = new ArrayList <ItemObject>();
list3.add(new ItemObject("Third Header", true));
list3.add(new ItemObject("This is the item content in the first position", false));
list3.add(new ItemObject("This is the item content in the second position", false));
List <ItemObject> finalList = new ArrayList <ItemObject>(list1);
finalList.addAll(list1);
finalList.addAll(list2);
finalList.addAll(list3);
return finalList;
}
然后您在适配器中的条件将类似于此:
private boolean isPositionHeader(int position) {
ItemObject mObject = itemObjects.get(position);
return mObject.isHeader();
}
替换List <ItemObject> finalList = new ArrayList <ItemObject>(list1);
finalList.addAll(list1);
finalList.addAll(list2);
finalList.addAll(list3);
和List <ItemObject> finalList = new ArrayList <ItemObject>();
finalList.addAll(list1);
finalList.addAll(list2);
finalList.addAll(list3)
您在此处添加第一个 header 两次
List <ItemObject> finalList = new ArrayList <ItemObject>(list1);
我想用多个 header 创建回收视图,第一个 header 工作得很好,但其余 header 没有按预期工作。这是我第一次使用 recycleview headers 。我也想知道这样做是否正确。
这是我的适配器。
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = RecyclerViewAdapter.class.getSimpleName();
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private List<ItemObject> itemObjects;
private Context context;
public RecyclerViewAdapter( Context context , List<ItemObject> itemObjects) {
this.context = context;
this.itemObjects = itemObjects;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_layout, parent, false);
return new HeaderViewHolder(layoutView);
} else if (viewType == TYPE_ITEM) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new ItemViewHolder(layoutView , context);
}
throw new RuntimeException("No match for " + viewType + ".");
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
ItemObject mObject = itemObjects.get(position);
if(holder instanceof HeaderViewHolder){
((HeaderViewHolder) holder).headerTitle.setText(mObject.getContents());
}else if(holder instanceof ItemViewHolder){
((ItemViewHolder) holder).itemContent.setText(mObject.getContents());
}
}
private ItemObject getItem(int position) {
return itemObjects.get(position);
}
@Override
public int getItemCount() {
return itemObjects.size();
}
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
}
这里是我的主要方法activity。
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this , getDataSource());
recyclerView.setAdapter(adapter);
private List <ItemObject> getDataSource(){
List<ItemObject> list1 = new ArrayList <ItemObject>();
list1.add(new ItemObject("First Header",true));
list1.add(new ItemObject("This is the item content in the first position"));
list1.add(new ItemObject("This is the item content in the second position"));
List <ItemObject> list2 = new ArrayList <ItemObject>();
list2.add(new ItemObject("Second Header",true));
list2.add(new ItemObject("This is the item content in the first position"));
list2.add(new ItemObject("This is the item content in the second position"));
List <ItemObject> list3 = new ArrayList <ItemObject>();
list3.add(new ItemObject("Third Header",true));
list3.add(new ItemObject("This is the item content in the first position"));
list3.add(new ItemObject("This is the item content in the second position"));
List <ItemObject> finalList = new ArrayList <ItemObject>(list1);
finalList.addAll(list1);
finalList.addAll(list2);
finalList.addAll(list3);
return finalList;
[![I get first header 2 times][1]][1]
}
这是我的商品 object class.
public class ItemObject {
private String contents;
boolean isHeader ;
public ItemObject(String contents, boolean isHeader) {
this.contents = contents;
this.isHeader = isHeader;
}
public ItemObject(String contents) {
this.contents = contents;
}
public String getContents() {
return contents;
}
public boolean isHeader() {
return isHeader;
}
}
我第一个header 2次这样https://imgur.com/a/2YZ8DxM
List.addAll()
只是连接作为参数传递给 finalList
的 List
的项目。也就是说,您最终得到一个包含 9 个项目的 List
。这就是为什么你的条件 position == 0
只适用于你的第一个 header.
一个可能的(简单)解决方案是修改您的 ItemObject
以具有一个标志,指示给定项目是 header(注意这是构造函数的第二个参数,true
是 header,否则 false
)。
private List <ItemObject> getDataSource(){
List<ItemObject> list1 = new ArrayList <ItemObject>();
list1.add(new ItemObject("First Header", true));
list1.add(new ItemObject("This is the item content in the first position", false));
list1.add(new ItemObject("This is the item content in the second position", false));
List <ItemObject> list2 = new ArrayList <ItemObject>();
list2.add(new ItemObject("Second Header", true));
list2.add(new ItemObject("This is the item content in the first position", false));
list2.add(new ItemObject("This is the item content in the second position", false));
List <ItemObject> list3 = new ArrayList <ItemObject>();
list3.add(new ItemObject("Third Header", true));
list3.add(new ItemObject("This is the item content in the first position", false));
list3.add(new ItemObject("This is the item content in the second position", false));
List <ItemObject> finalList = new ArrayList <ItemObject>(list1);
finalList.addAll(list1);
finalList.addAll(list2);
finalList.addAll(list3);
return finalList;
}
然后您在适配器中的条件将类似于此:
private boolean isPositionHeader(int position) {
ItemObject mObject = itemObjects.get(position);
return mObject.isHeader();
}
替换List <ItemObject> finalList = new ArrayList <ItemObject>(list1);
finalList.addAll(list1);
finalList.addAll(list2);
finalList.addAll(list3);
和List <ItemObject> finalList = new ArrayList <ItemObject>();
finalList.addAll(list1);
finalList.addAll(list2);
finalList.addAll(list3)
您在此处添加第一个 header 两次
List <ItemObject> finalList = new ArrayList <ItemObject>(list1);