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?