SectionedRecyclerView over Write第一项

时间:2016-04-24 21:06:13

标签: java android listview android-recyclerview

我尝试使用:SimpleSectionedRecyclerViewAdapter,我有arrayList,其中包含30个带字符串的元素,我将使用标题Title对每10个元素进行分组,  第一项应该是" Item0"标题名称"前10个元素开始" 但是当我这样做时,我得到了:

"前10个元素开始" //标题标题

"项目1" < ---!注意:它应该" Item0"

那么索引编号0去了哪里?

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

 }

1 个答案:

答案 0 :(得分: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);