2016-04-24 9 views
0

我正在嘗試使用:SimpleSectionedRecyclerViewAdapter,我有30個元素與字符串arrayList,我要將每10個元素與標題組標題, 第一項應該是「Item0」與標題命名 「前10個元素開始」 但這樣做的時候,我得到:SectionedRecyclerView over寫第一項

「前10個元素開始」 //頭titile

「項目1」 < ---!注意:它應該是「Item0」

so where index number 0 gone?

//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); 

我使用的是完全一樣適配器: https://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然後item1等,但它重疊item0並打印標題然後item1 dirclty沒有item0 –

回答

0

與庫SectionedRecyclerViewAdapter你可以在部分組的項,並添加標題,每個部分無需擔心的「位置」標題:

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類中的「字符串」替換「類別」,並創建您的部分: new MySection(「Header 0」,Arrays.asList(new String [] {「Item0」,「Item1」,「Item2 「}); 你應該看到」Item0「作爲第一個元素。 – Gustavo

+0

謝謝你它工作正常 –