标题使用多种视图类型替换RecyclerView中的项目

时间:2016-06-25 22:57:22

标签: android android-recyclerview

我有RecyclerView具有多种视图类型的适配器,其中已初始化:

  1. 0位置的第一个标题
  2. 1个位置的ArrayList项目
  3. 8位置的第二个标题
  4. 我的问题 - 标题会替换指定位置上的项目,但无论项目如何都不会找到。

    适配器:

    public MainAdapter(ArrayList<Model> items, Context context) {
        this.items = items;
            this.context = context;
    }
    
    @Override
    public int getItemViewType (int position) {
        switch (position) {
            case TYPE_HEADER:
                return TYPE_HEADER;
            case TYPE_SECOND_HEADER:
                return TYPE_SECOND_HEADER;
            default:
                return TYPE_ITEMS;
        }
    }
    
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        switch (viewType) {
            case TYPE_HEADER:
                ViewGroup baseVHV = (ViewGroup) inflater.inflate(R.layout.utils_recyclerview_header_base, viewGroup, false);
                BaseHeaderViewHolder baseVH = new BaseHeaderViewHolder(this.context, baseVHV);
                return baseVH;
            case TYPE_ITEMS:
                ViewGroup baseVHV1 = (ViewGroup) inflater.inflate(R.layout.utils_viewholder, viewGroup, false);
                BaseViewHolder baseVH1 = new BaseViewHolder(this.context, baseVHV1);
                return baseVH1;
            case TYPE_SECOND_HEADER:
                ViewGroup baseSVHV = (ViewGroup) inflater.inflate(R.layout.utils_recyclerview_second_header_base, viewGroup, false);
                BaseSecondHeaderViewHolder baseSVH = new BaseSecondHeaderViewHolder(this.context, baseSVHV);
                return baseSVH;
            default:
                ViewGroup baseVHV2 = (ViewGroup) inflater.inflate(R.layout.utils_viewholder, viewGroup, false);
                BaseViewHolder baseVH2 = new BaseViewHolder(this.context, baseVHV2);
                return baseVH2;
        }
    }   
    
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        switch (viewHolder.getItemViewType()) {
            case TYPE_HEADER:
                BaseHeaderViewHolder header = (BaseHeaderViewHolder) viewHolder;
                header.initView("Test");
                break;
            case TYPE_ITEMS:
                BaseViewHolder baseViewHolder = (BaseViewHolder) viewHolder;
                baseViewHolder.initView(this.items.get(i));
                break;
            case TYPE_SECOND_HEADER:
                BaseSecondHeaderViewHolder header2 = (BaseSecondHeaderViewHolder) viewHolder;
                header2.initView("Test");
                break;
    
        }
    }
    
    @Override
    public int getItemCount() {
        return items.size();
    }
    
    
    }
    

    我不能出现什么错误。我希望你,非常感谢!

2 个答案:

答案 0 :(得分:0)

  

我的问题 - 标题会替换指定位置上的项目,但是   无论物品是什么,都无法定位。

这种情况正在发生,因为您要将标题添加为单独的项目。 您想要做的是在视图中为标题提供单独的视图。这样,您可以使用itemViewType和position的组合来显示onBindViewHolder中位置的标题和右视图。你可以做类似下面的事情

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
    BaseViewHolder baseViewHolder = (BaseViewHolder) viewHolder;
    switch (viewHolder.getItemViewType()) {
        case TYPE_HEADER:
            //Make your header view visible
            baseViewHolder.initHeader();
            break;        

        case TYPE_SECOND_HEADER:
            //Make your second header view visible
            baseViewHolder.initSecondHeader();
            break;

        case default:
            //hide both headers since the type is not header
            baseViewHolder.hideHeader();
            baseViewHolder.hideSecondHeader();
            break;

    }
    baseViewHolder.initView(this.items.get(i));
}

答案 1 :(得分:0)

在getItemViewType()中,您应该检查案例中所需的位置0,1和8,而不是视图类型。

@Override 
public int getItemViewType (int position) 
{ 
    switch (position) 
    {  
    case 0:
        return TYPE_HEADER; 

    case 8:
        return TYPE_SECOND_HEADER;

    default:
        return TYPE_ITEMS; 
     }
}