分段回收视图 [英] Sectioned recyclerview

查看:22
本文介绍了分段回收视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用部分和子标题来实现一个 recyclerview.我在 Github 上通过 Gabriele Mariotti 在 Github 上找到了一个不错的课程,但我在实现它时遇到了一些问题.基本上,当我使用我的常规 recyclerview 适配器而不是示例时,每个部分标题替换我的 recyclerview 的一个元素.

I'm trying to implement a recyclerview with sections and subheaders. I found a nice class by Gabriele Mariotti on Github but I'm having some issues implementing it. Basically when I use my regular recyclerview adapter instead of the example, each section header replaces one element of my recyclerview.

例如.假设我的 recyclerview 中有 5 张卡片(5 种成分,因为它是一个烹饪应用程序),但是在添加 2 个标题之后,现在我剩下 3 张卡片,其中第一张和第四张被副标题替换.我希望这是有道理的.此外,我的卡片位置都搞砸了,当我滚动到 recyclerview 的底部时,它会使应用程序崩溃.我附上了几行代码,希望有人能指出我正确的方向.与 Mariotti 的示例相比,我基本上只是使用我自己的适配器而不是simpladapter.java"类:

For example. Let's say I have 5 cards (5 ingredients as it is a cooking app) inside my recyclerview but after adding 2 headers now I am left with 3 cards where the first and fourth are replaced by the subheaders. I hope this makes sense. Also my card position is all messed up and when I scroll to the bottom of the recyclerview it crashes the app. I'm attaching a few lines of code so hopefully someone could point me toward the right direction. Compared to Mariotti's example, I'm basically just using my own adapter instead of "simpleadapter.java" class:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolders> {

private List<Ingredient> itemList;
private final String TAG = OrderFragment.class.getSimpleName();
private Context context;

public RecyclerViewAdapter(Context context,  List<Ingredient> itemList) {
    this.itemList = itemList;
    this.context = context;
}

public class RecyclerViewHolders extends RecyclerView.ViewHolder{
    public RecyclerViewHolders(View itemView) {
        super(itemView);
    }
}

public class ingredientViewHolder extends RecyclerViewHolders{

    public TextView ingredientName;
    public ImageView ingredientPhoto;
    public ImageButton infoBtn;
    public Button regularBtn;
    public Button extraBtn;
    public ImageView soldOutPhoto;
    public ImageView grayOutPhoto;
    private Context context;

    public ingredientViewHolder(View itemView) {
        super(itemView);
        ingredientName = (TextView)itemView.findViewById(R.id.ingredient_name);
        ingredientPhoto = (ImageView)itemView.findViewById(R.id.ingredient_photo);
        infoBtn = (ImageButton) itemView.findViewById(R.id.info);
        regularBtn = (Button) itemView.findViewById(R.id.regular);
        extraBtn = (Button) itemView.findViewById(R.id.extra);
        soldOutPhoto = (ImageView) itemView.findViewById(R.id.sold_out);
        grayOutPhoto = (ImageView) itemView.findViewById(R.id.gray_out);
    }
}

@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.ingredient, null);
    return new ingredientViewHolder(fragment, layoutView);
}

@Override
public void onBindViewHolder(RecyclerViewHolders viewHolder, final int pos) {
    ingredientViewHolder holder = (ingredientViewHolder) viewHolder;

    final int position = holder.getAdapterPosition();
    final String name = itemList.get(position).getName();

    if (MainActivity.order.get(position ) == 1) {
        holder.regularBtn.setSelected(true);
        holder.extraBtn.setSelected(false);
    } else if (MainActivity.order.get(position ) == 2) {
        holder.regularBtn.setSelected(false);
        holder.extraBtn.setSelected(true);
    } else {
        holder.regularBtn.setSelected(false);
        holder.extraBtn.setSelected(false);
    }

    holder.ingredientName.setText(itemList.get(position).getName());

}

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

这里是我如何在我的片段中调用这个 recyclerview:

And here is how I am calling this recyclerview inside my fragment:

        final RecyclerView rView = (RecyclerView) v.findViewById(R.id.recycler_view);
    rView.setHasFixedSize(true);
    rView.setLayoutManager(new GridLayoutManager(getActivity(), 3));

    //Your RecyclerView.Adapter
    RecyclerView.Adapter rcAdapter = new RecyclerViewAdapter(getActivity(),  visibleIngredients);
    //This is the code to provide a sectioned grid
    List<SectionedGridRecyclerViewAdapter.Section> sections =
            new ArrayList<SectionedGridRecyclerViewAdapter.Section>();

    //Sections
    sections.add(new SectionedGridRecyclerViewAdapter.Section(0,"Spreads"));
    sections.add(new SectionedGridRecyclerViewAdapter.Section(2,"Liquid"));

    //Add your adapter to the sectionAdapter
    SectionedGridRecyclerViewAdapter.Section[] dummy = new SectionedGridRecyclerViewAdapter.Section[sections.size()];
    SectionedGridRecyclerViewAdapter mSectionedAdapter = new
            SectionedGridRecyclerViewAdapter(getActivity(),R.layout.section,R.id.section_text,rView,rcAdapter);
    mSectionedAdapter.setSections(sections.toArray(dummy));

    //Apply this adapter to the RecyclerView
    rView.setAdapter(mSectionedAdapter);

推荐答案

找出问题所在.看起来位置已关闭,因为在 onBindViewHolder 中我使用了以下行:

Figured out the problem. Looks like the position was off because inside the onBindViewHolder I was using the following line:

final int position = holder.getAdapterPosition();

一旦我忽略了这一点并使用函数返回的位置,一切都很好.

Once I ignored that and use the position that function returns instead it was all good.

这篇关于分段回收视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆