2016-04-24 16 views
0

私は使用しようとしています:SimpleSectionedRecyclerViewAdapter、私は文字列で30要素を持つarrayListを持っています、私はヘッダータイトル の最初の項目はヘッダーSectionedRecyclerViewオーバーライド最初の項目

//ヘッダtitile

"アイテム1" < --- "まず10個の要素は開始": "まず10個の要素が起動し、" しかし、私が得ることをやったときに名前を付けます!注: "Item0"にする必要があります

インデックス番号はどこですか?

//adapter.addItem3(CategoriesList.get(0)); 
       List<SimpleSectionedRecyclerViewAdapter.Section> sections = 
         new ArrayList<SimpleSectionedRecyclerViewAdapter.Section>(); 

       sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, "First 10 elements start")); 


       //Add your adapter to the sectionAdapter 
       SimpleSectionedRecyclerViewAdapter.Section[] dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()]; 
       SimpleSectionedRecyclerViewAdapter mSectionedAdapter = new 
         SimpleSectionedRecyclerViewAdapter(activity, R.layout.drawer_header_book, R.id.headerName, adapter); 
       mSectionedAdapter.setSections(sections.toArray(dummy)); 
       mDrawerList.setLayoutManager(new LinearLayoutManager(activity)); 
       mDrawerList.setAdapter(mSectionedAdapter); 

私は、アダプタとまったく使用しています:ライブラリSectionedRecyclerViewAdapterhttps://gist.github.com/gabrielemariotti/4c189fb1124df4556058

public class DrawerItemCustomAdapterForAllBooks extends RecyclerView.Adapter<DrawerItemCustomAdapterForAllBooks.SimpleViewHolder> { 

private final Context context; 
Typeface custom_font; 
ArrayList<Categories> mData; 
private static final int TYPE_HEADER = 0; 
private static final int TYPE_ITEM = 0; 
private static final int TYPE_SEPARATOR = 1; 
int BookID; 

public void add(Categories s,int position) { 
    position = position == -1 ? getItemCount() : position; 
    mData.add(position,s); 
    notifyItemInserted(position); 
} 

public void remove(int position){ 
    if (position < getItemCount() ) { 
     mData.remove(position); 
     notifyItemRemoved(position); 
    } 
} 

public DrawerItemCustomAdapterForAllBooks(Context context, ArrayList<Categories> Categories2, int BookID) { 
    this.mData = Categories2; 
    this.context = context; 
    this.BookID = BookID; 
    custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/HelveticaNeueLTArabic-Light.ttf"); 

} 

public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    final View view = LayoutInflater.from(context).inflate(R.layout.listview_item_row, parent, false); 
    return new SimpleViewHolder(view); 
} 

@Override 
public void onBindViewHolder(SimpleViewHolder holder, final int position) { 



    if (holder != null) { 


     final Categories currentItem = getItem(holder.getAdapterPosition()); 
     SimpleViewHolder genericViewHolder = (SimpleViewHolder) holder; 
     genericViewHolder.position = holder.getAdapterPosition(); 
     genericViewHolder.CategoryName.setText(currentItem.getName()); 
     genericViewHolder.CategoryName.setTypeface(custom_font); 
     genericViewHolder.CategoryName.setTag(holder.getAdapterPosition()); 
     genericViewHolder.itemView.setTag(holder.getAdapterPosition()); 


     if (currentItem.getBookID() == 1) { 
      genericViewHolder.CategoryName.setTextColor(context.getResources().getColor(R.color.nokhba_white)); 
      genericViewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.nokhba_darkrose)); 

      Picasso.with(context).load(R.drawable.list_icon_e02) 
        .error(R.drawable.no_internet) 
        .tag(context) 
        .placeholder(R.drawable.no_spic) 
        .into(genericViewHolder.CategoryImage); 
     } else if (currentItem.getBookID() == 2) { 
      genericViewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.nokhba_openrose)); 
      genericViewHolder.CategoryName.setTextColor(context.getResources().getColor(R.color.nokhba_darkrose)); 
      Picasso.with(context).load(R.drawable.list_icon_08) 
        .error(R.drawable.no_internet) 
        .tag(context) 
        .placeholder(R.drawable.no_spic) 
        .into(genericViewHolder.CategoryImage); 
     } else if (currentItem.getBookID() == 3) { 
      genericViewHolder.CategoryName.setTextColor(context.getResources().getColor(R.color.nokhba_darkrose)); 
      genericViewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.nokhba_white)); 
      Picasso.with(context).load(R.drawable.list_icon_08) 
        .error(R.drawable.no_internet) 
        .tag(context) 
        .placeholder(R.drawable.no_spic) 
        .into(genericViewHolder.CategoryImage); 
     } 
    } 
    } 

@Override 
public int getItemCount() { 
    return mData.size()-2; 
} 

public Categories getItem(int position) { 
    return mData.get(position-1); 
} 

public static class SimpleViewHolder extends RecyclerView.ViewHolder { 
    protected TextView CategoryName; 
    protected ImageView CategoryImage; 
    protected View itemView; 
    int position = -1; 

    public SimpleViewHolder(View itemView) { 
     super(itemView); 
     this.CategoryName = (TextView) itemView.findViewById(R.id.CategoryName); 
     this.CategoryImage = (ImageView) itemView.findViewById(R.id.CategoryImage); 
     // CategoryName.setTypeface(custom_font); 
     this.itemView = itemView; 

     getAdapterPosition(); 

     this.CategoryName = (TextView) itemView.findViewById(R.id.CategoryName); 
     this.CategoryImage = (ImageView) itemView.findViewById(R.id.CategoryImage);  } 
} 

} 
+0

5行目は 'item1 "の置換を考慮していないようです。 drawer_header_bookレイアウトファイルのコードスニペットを追加できますか? –

+0

Item1はCatagoriesのarrayListの文字列オブジェクトであり、 "最初の10要素の開始"はヘッダの太さであるため、item0とitem0の間にitem0と重複しますが、item0と重複してitem1のdircltyを出力します。 –

答えて

0

グループ化することができ、あなたのセクション内の項目と「位置」の心配をせずに、各セクションにヘッダを追加ヘッダ:

class MySection extends StatelessSection { 

    String title; 
    List<Categories> list; 

    public MySection(String title, List<Categories> list) { 
     // call constructor with layout resources for this Section header, footer and items 
     super(R.layout.section_header, R.layout.section_item); 

     this.title = title; 
     this.list = list; 
    } 

    @Override 
    public int getContentItemsTotal() { 
     return list.size(); // number of items of this section 
    } 

    @Override 
    public RecyclerView.ViewHolder getItemViewHolder(View view) { 
     // return a custom instance of ViewHolder for the items of this section 
     return new MyItemViewHolder(view); 
    } 

    @Override 
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { 
     MyItemViewHolder itemHolder = (MyItemViewHolder) holder; 

     // bind your view here 
     itemHolder.tvItem.setText(list.get(position).getName()); 
    } 

    @Override 
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) { 
     return new SimpleHeaderViewHolder(view); 
    } 

    @Override 
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) { 
     MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder; 

     // bind your header view here 
     headerHolder.tvItem.setText(title); 
    } 
} 

は、次に、あなたのセクションでRecyclerViewを設定します。

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter(); 

// Create your sections with a sub list of data from mData 
MySection data1Section = new MySection("First 10 elements start", categories1List); 
MySection data2Section = new MySection("Elements from 10 to 20 start", categories2List); 
MySection data3Section = new MySection("Elements from 20 to 30 start", categories3List); 

// Add your Sections to the adapter 
sectionAdapter.addSection(data1Section); 
sectionAdapter.addSection(data2Section); 
sectionAdapter.addSection(data3Section); 

// Set up your RecyclerView with the SectionedRecyclerViewAdapter 
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview); 
recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 
recyclerView.setAdapter(sectionAdapter); 
+0

あなたが言ったが、ヘッダーは私の質問で言及するようにそれを超えて配列の最初の位置に来るので、私の最初の項目は "Item0"ですが、ヘッダーが作成された後、それは0の位置を取るアイテムを取得する "Item1" "item0"と "item1"というように出力されるはずですので、セクションのヘッダーは実際に配列アイテムの実際の位置をグループ化していません –

+0

カテゴリリストに "item0"がありますか? 上記のMySectionクラスの "Categories"を "String"で置き換え、次のようにセクションを作成してください: new MySection( "Header 0"、Arrays.asList(new String [] {"Item0"、 "Item1"、 "Item2 "}); " Item0 "が最初の要素として表示されます。 – Gustavo

+0

ありがとうございます –

関連する問題