recycleview中的多个标头不起作用-为什么如此?

时间:2019-06-17 15:34:47

标签: android android-recyclerview recycler-adapter stickyrecycleview

我想用多个标头创建recycleview,第一个标头工作正常,但其余标头未按预期工作。这是我第一次使用recycleview标头。而且我也想知道这是正确的做法。

这是我的适配器。

    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;
    }
}

这是我主要活动中的方法。

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]
}

这是我的物品对象类。

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;
    }
}

我像这样{{3}}两次获得第一个标题

2 个答案:

答案 0 :(得分:0)

List.addAll()仅连接作为参数传递到List的{​​{1}}的项。也就是说,您最终得到一个包含9个项目的finalList。这就是为什么条件List仅适用于第一个标头的原因。

一种可能(简单)的解决方案是将position == 0修改为带有一个标志,该标志指示给定的项是标题(注意,这是构造函数的第二个参数,ItemObject是标题,否则为true

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;
}

答案 1 :(得分:0)

替换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)

您要在此处两次添加第一个标头

List <ItemObject> finalList = new ArrayList <ItemObject>(list1);