1

i want to use the Recyclerview with two different rows to show categories. The first row is a Parentcategory and should represented as Listheader. The Parentcategorys can contain zero, one or multiple childs which should represented other then the parent.

My data is stored in an ArrayAdapter that looks like this. (The Key Category is the ParentCategory while the Lists represents the children.

private ArrayMap<Category, List<Category>> categoryMap= new ArrayMap<>();

I've searched a lot and tryed a lot, like in this thread, or this one and this. But this all didn't work for me.

Edit: My problem is that the data is not displayed correctly. For example: I have 4 Parentcategories. The second parentcategory has 2 children.

My data looks like this.

  • Parent
  • Parent
    • Child
    • Child
  • Parent
  • Parent

But my list displays this:

  • Parent
  • Parent
    • Child
  • Parent

I also tried to use ExpandableListView and ListViews. But it was not what i want.

My adapter looks currently like this:

public class CategoryListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;

private ArrayMap<Category,List<Category>> categoryMap;
private Context context;
private ArrayList<Category> categoryArrayList;


public CategoryListAdapter(Context context, ArrayMap<Category, List<Category>> categoryMap) {
    this.context = context;
    this.categoryMap = categoryMap;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.d("VIEWTYPE",""+viewType);
    switch (viewType){
        case TYPE_HEADER:
            CategoryListParentBinding binding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.getContext()),
                    R.layout.category_list_parent, parent, false);
            return new HeaderViewHolding(binding);
        case TYPE_ITEM:
                CategoryListChildBinding binding1 = DataBindingUtil.inflate(
                        LayoutInflater.from(parent.getContext()),
                        R.layout.category_list_child, parent, false);
                return new ChildViewHolding(binding1);
    }
    throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if(holder instanceof HeaderViewHolding){
        CategoryListParentBinding parentBinding = ((HeaderViewHolding) holder).parentBinding;
        parentBinding.setParentCategory(categoryMap.keyAt(position));
    }
    if(holder instanceof ChildViewHolding){
        for(int i = 0; i<=categoryMap.valueAt(position).size()-1;i++){
            CategoryListChildBinding childBindingBinding = ((ChildViewHolding) holder).childBinding;
            childBindingBinding.setChildCategory(categoryMap.valueAt(position).get(i));
        }
    }
}

@Override
public int getItemViewType(int position) {
    if(isPositionHeader(position))
        return TYPE_HEADER;
    return TYPE_ITEM;
}

private boolean isPositionHeader(int position) {
    return categoryMap.valueAt(position).size() <= 0;
}


@Override
public int getItemCount() {
    int count = categoryMap.size();
    for(int i=0;i<=categoryMap.size()-1;i++){
      count = count + categoryMap.valueAt(i).size();
    }
    return count;
}

static class HeaderViewHolding extends RecyclerView.ViewHolder {
    CategoryListParentBinding parentBinding;
    public HeaderViewHolding(CategoryListParentBinding parentBinding) {
        super(parentBinding.categoryParentLayout);
        this.parentBinding = parentBinding;
    }
}

static class ChildViewHolding extends RecyclerView.ViewHolder {
    CategoryListChildBinding childBinding;
    public ChildViewHolding(CategoryListChildBinding childBinding) {
        super(childBinding.categoryChildLayout);
        this.childBinding = childBinding;
    }
}
}

Can somebody help me?

Community
  • 1
  • 1
KingOA
  • 51
  • 7
  • It is not clear what problem you are experiencing – Kuffs Jan 20 '17 at 15:30
  • "Didn't work for me" is not proper statement. What is the error you are getting, or what is the behavior you are seeing? – Gokhan Arik Jan 20 '17 at 15:31
  • ooh ok sry i'am new here, and don't know how to describe my problem. All this examples mentioned to overwrite the method getItemViewType, but i can't differentiate between the viewtypes because i've stored my data in an ArrayMap. I'am not getting any errors. But the list don't show the correct data. For example. I have a list with 4 parentCategorys. The second parent has 2 Children. The list show 3 parents and only one child. – KingOA Jan 20 '17 at 15:37
  • You have nested lists / maps, and `isPositionHeader` does not seem to take your setup into account. You will have to fix your lookup of view types and binding of the data. – David Medenjak Jan 21 '17 at 12:05
  • @DavidMedenjak yes this is where i'am struggling. I don't know how to look up for my view types. – KingOA Jan 21 '17 at 12:47

1 Answers1

0

Problem is ArrayMap, Use HashMap instead. ArrayMap is sorting data according their type :|

Farhad
  • 189
  • 4