2017-01-20 7 views
1

カテゴリを表示するには、2つの異なる行のRecyclerviewを使用します。最初の行は親カテゴリであり、Listheaderとして表される必要があります。親カテゴリには、0以外の1つまたは複数の子を含めることができます。異なるViewRowsのAndroid RecyclerView

私のデータは、次のようなArrayAdapterに格納されます。 (主なカテゴリリストは、子どもたちを表しているParentCategoryです。

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

私は多くのことを検索しthisスレッド、またはthis onethisのように、多くのことをしようと試みた。しかし、これはすべてのために動作しませんでしたしました私

編集:私の問題は、データが正しく表示されないということです。たとえば、:。。。 私は4 Parentcategoriesを持っている第二parentcategoryは2人の子供を持っている

は私のデータは、このようになります

    • 子供
    • 子供

しかし、私のリストを表示し、この:

    • 子供

私もExpandableListViewとリストビューを使用しようとしました。しかし、それは私が望むものではありませんでした。

私のアダプタは、このように、現在になります

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

誰かが私を助けることができますか?

+0

「私のために動作しませんでした」あなたは – Kuffs

+0

を経験している何の問題明らかではないが、適切なステートメントではありません。あなたが得ているエラーは何ですか?またはあなたが見ている動作は何ですか? –

+0

私はここで新しい、私は新しい問題を説明する方法を知らないsryのは、大丈夫です。これらの例はすべて、getItemViewTypeメソッドを上書きすると述べていますが、ArrayMapにデータを保存しているため、ビュータイプを区別できません。私は何の誤りもありません。しかし、リストには正しいデータが表示されません。例えば。 4つのparentCategoryを持つリストがあります。 2番目の親には2人の子供がいます。このリストには、3人の親と1人の子供しか示されていません。 – KingOA

答えて

0

問題はArrayMapですが、代わりにHashMapを使用してください。 ArrayMapは型に従ってデータをソートします。

関連する問題