Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add two sections in recyclerview android

In my application i am using recyclerview to display all contact list. I want two section in recyclerview.

Like one section is my application contact list and second section is my phone contact list.

Like this

enter image description here

Is there any method to do it?

Does anybody know how to do it?

like image 963
Kinjal Avatar asked Jun 10 '15 09:06

Kinjal


People also ask

What is getItemViewType in RecyclerView?

getItemViewType(int position) will be called for the viewType ; onBindViewHolder(ViewHolder holder, int position) is always called when recycling the view (new data is brought in and try to display with that ViewHolder ).

Which method must be overridden to implement a RecyclerView adapter?

Adapter. RecyclerView includes a new kind of adapter. It's a similar approach to the ones you already used, but with some peculiarities, such as a required ViewHolder . You will have to override two main methods: one to inflate the view and its view holder, and another one to bind data to the view.


1 Answers

If you already have a RecyclerView, an easy way to implement the sections is using Gabriele Mariotti's SimpleSectionedRecyclerViewAdapter.

I paste you his example:

//Your RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.list); mRecyclerView.setHasFixedSize(true); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL));  //Your RecyclerView.Adapter mAdapter = new SimpleAdapter(this,sCheeseStrings);   //This is the code to provide a sectioned list List<SimpleSectionedRecyclerViewAdapter.Section> sections =         new ArrayList<SimpleSectionedRecyclerViewAdapter.Section>();  //Sections sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0,"Section 1")); sections.add(new SimpleSectionedRecyclerViewAdapter.Section(5,"Section 2")); sections.add(new SimpleSectionedRecyclerViewAdapter.Section(12,"Section 3")); sections.add(new SimpleSectionedRecyclerViewAdapter.Section(14,"Section 4")); sections.add(new SimpleSectionedRecyclerViewAdapter.Section(20,"Section 5"));  //Add your adapter to the sectionAdapter SimpleSectionedRecyclerViewAdapter.Section[] dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()]; SimpleSectionedRecyclerViewAdapter mSectionedAdapter = new           SimpleSectionedRecyclerViewAdapter(this,R.layout.section,R.id.section_text,mAdapter); mSectionedAdapter.setSections(sections.toArray(dummy));  //Apply this adapter to the RecyclerView mRecyclerView.setAdapter(mSectionedAdapter); 
like image 197
marc_aragones Avatar answered Oct 05 '22 17:10

marc_aragones