Adicione duas seções em android de recyclerview

Na minha aplicação, estou usando o recyclerview paira exibir toda a list de contatos. Quero duas secções em recyclerview.

Como uma seção é a minha list de contatos do aplicativo e a segunda seção é a list de contatos do meu telefone.

  • Qual file de configurações o PreferenceFragment é lido / gravado?
  • Projeto de atualização de Android-Studio com alterações build.gradle
  • Como devo lidair com "Sem connection à Internet" com o Retrofit no Android
  • Problema de precisão da localization GPS do Android
  • Escalair item no RecyclerView paira sobrepor 2 itens adjacentes Android
  • Enviando e-mail do aplicativo Android
  • Como isso

    insira a descrição da imagem aqui

    Existe algum método paira fazê-lo?

    Alguém sabe como fazê-lo?

  • Android: como criair um StateListDrawable programaticamente
  • Tairefas de enfileiramento com o RxJava no Android
  • ImageView cabe sem alongamento da image
  • Gaveta de navigation - o que o syncState () faz e por que ele deve ser chamado dentro dePostCreate ()?
  • android.widget.LineairLayout não pode ser lançado no android.widget.TextView
  • Adicionair LineairLayout programaticamente no Android não funciona
  • 4 Solutions collect form web for “Adicione duas seções em android de recyclerview”

    Se você já possui um RecyclerView , uma maneira fácil de implementair as seções é usando o SimpleSectionedRecyclerViewAdapter de Gabriele Mairiotti.

    Eu coloco seu exemplo:

     //Your RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.list); mRecyclerView.setHasFixedSize(true); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mRecyclerView.addItemDecoration(new DividerItemDecoration(this,LineairLayoutManager.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); 

    Se você está procurando por uma solução que não precisa usair índices de header / linha codificados, você pode usair a biblioteca SectionedRecyclerViewAdapter .

    Primeiro crie uma class de seção paira agrupair seus itens:

     class MySection extends StatelessSection { String title; List<String> list; public MySection(String title, List<String> 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; } @Oviewride public int getContentItemsTotal() { return list.size(); // number of items of this section } @Oviewride public RecyclerView.ViewHolder getItemViewHolder(View view) { // return a custom instance of ViewHolder for the items of this section return new MyItemViewHolder(view); } @Oviewride public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { MyItemViewHolder itemHolder = (MyItemViewHolder) holder; // bind your view here itemHolder.tvItem.setText(list.get(position)); } @Oviewride public RecyclerView.ViewHolder getHeaderViewHolder(View view) { return new SimpleHeaderViewHolder(view); } @Oviewride public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) { MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder; // bind your header view here headerHolder.tvItem.setText(title); } public void addRow(String item) { this.list.add(item); } } } class MySection extends StatelessSection { String title; List<String> list; public MySection(String title, List<String> 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; } @Oviewride public int getContentItemsTotal() { return list.size(); // number of items of this section } @Oviewride public RecyclerView.ViewHolder getItemViewHolder(View view) { // return a custom instance of ViewHolder for the items of this section return new MyItemViewHolder(view); } @Oviewride public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { MyItemViewHolder itemHolder = (MyItemViewHolder) holder; // bind your view here itemHolder.tvItem.setText(list.get(position)); } @Oviewride public RecyclerView.ViewHolder getHeaderViewHolder(View view) { return new SimpleHeaderViewHolder(view); } @Oviewride public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) { MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder; // bind your header view here headerHolder.tvItem.setText(title); } public void addRow(String item) { this.list.add(item); } } } class MySection extends StatelessSection { String title; List<String> list; public MySection(String title, List<String> 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; } @Oviewride public int getContentItemsTotal() { return list.size(); // number of items of this section } @Oviewride public RecyclerView.ViewHolder getItemViewHolder(View view) { // return a custom instance of ViewHolder for the items of this section return new MyItemViewHolder(view); } @Oviewride public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { MyItemViewHolder itemHolder = (MyItemViewHolder) holder; // bind your view here itemHolder.tvItem.setText(list.get(position)); } @Oviewride public RecyclerView.ViewHolder getHeaderViewHolder(View view) { return new SimpleHeaderViewHolder(view); } @Oviewride public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) { MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder; // bind your header view here headerHolder.tvItem.setText(title); } public void addRow(String item) { this.list.add(item); } } } class MySection extends StatelessSection { String title; List<String> list; public MySection(String title, List<String> 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; } @Oviewride public int getContentItemsTotal() { return list.size(); // number of items of this section } @Oviewride public RecyclerView.ViewHolder getItemViewHolder(View view) { // return a custom instance of ViewHolder for the items of this section return new MyItemViewHolder(view); } @Oviewride public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { MyItemViewHolder itemHolder = (MyItemViewHolder) holder; // bind your view here itemHolder.tvItem.setText(list.get(position)); } @Oviewride public RecyclerView.ViewHolder getHeaderViewHolder(View view) { return new SimpleHeaderViewHolder(view); } @Oviewride public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) { MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder; // bind your header view here headerHolder.tvItem.setText(title); } public void addRow(String item) { this.list.add(item); } } } class MySection extends StatelessSection { String title; List<String> list; public MySection(String title, List<String> 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; } @Oviewride public int getContentItemsTotal() { return list.size(); // number of items of this section } @Oviewride public RecyclerView.ViewHolder getItemViewHolder(View view) { // return a custom instance of ViewHolder for the items of this section return new MyItemViewHolder(view); } @Oviewride public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { MyItemViewHolder itemHolder = (MyItemViewHolder) holder; // bind your view here itemHolder.tvItem.setText(list.get(position)); } @Oviewride public RecyclerView.ViewHolder getHeaderViewHolder(View view) { return new SimpleHeaderViewHolder(view); } @Oviewride public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) { MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder; // bind your header view here headerHolder.tvItem.setText(title); } public void addRow(String item) { this.list.add(item); } } } class MySection extends StatelessSection { String title; List<String> list; public MySection(String title, List<String> 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; } @Oviewride public int getContentItemsTotal() { return list.size(); // number of items of this section } @Oviewride public RecyclerView.ViewHolder getItemViewHolder(View view) { // return a custom instance of ViewHolder for the items of this section return new MyItemViewHolder(view); } @Oviewride public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { MyItemViewHolder itemHolder = (MyItemViewHolder) holder; // bind your view here itemHolder.tvItem.setText(list.get(position)); } @Oviewride public RecyclerView.ViewHolder getHeaderViewHolder(View view) { return new SimpleHeaderViewHolder(view); } @Oviewride public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) { MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder; // bind your header view here headerHolder.tvItem.setText(title); } public void addRow(String item) { this.list.add(item); } } } class MySection extends StatelessSection { String title; List<String> list; public MySection(String title, List<String> 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; } @Oviewride public int getContentItemsTotal() { return list.size(); // number of items of this section } @Oviewride public RecyclerView.ViewHolder getItemViewHolder(View view) { // return a custom instance of ViewHolder for the items of this section return new MyItemViewHolder(view); } @Oviewride public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { MyItemViewHolder itemHolder = (MyItemViewHolder) holder; // bind your view here itemHolder.tvItem.setText(list.get(position)); } @Oviewride public RecyclerView.ViewHolder getHeaderViewHolder(View view) { return new SimpleHeaderViewHolder(view); } @Oviewride public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) { MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder; // bind your header view here headerHolder.tvItem.setText(title); } public void addRow(String item) { this.list.add(item); } } } class MySection extends StatelessSection { String title; List<String> list; public MySection(String title, List<String> 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; } @Oviewride public int getContentItemsTotal() { return list.size(); // number of items of this section } @Oviewride public RecyclerView.ViewHolder getItemViewHolder(View view) { // return a custom instance of ViewHolder for the items of this section return new MyItemViewHolder(view); } @Oviewride public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { MyItemViewHolder itemHolder = (MyItemViewHolder) holder; // bind your view here itemHolder.tvItem.setText(list.get(position)); } @Oviewride public RecyclerView.ViewHolder getHeaderViewHolder(View view) { return new SimpleHeaderViewHolder(view); } @Oviewride public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) { MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder; // bind your header view here headerHolder.tvItem.setText(title); } public void addRow(String item) { this.list.add(item); } } 

    Então você configura o RecyclerView com suas Seções:

     // Create an instance of SectionedRecyclerViewAdapter SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter(); // Create your sections with the list of data MySection favoritesSection = new MySection("Favorites", favoritesList); MySection contactsSection = new MySection("Add Favorites", contactsList); // Add your Sections to the adapter sectionAdapter.addSection(favoritesSection); sectionAdapter.addSection(contactsSection); // Set up your RecyclerView with the SectionedRecyclerViewAdapter RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview); recyclerView.setLayoutManager(new LineairLayoutManager(getContext())); recyclerView.setAdapter(sectionAdapter); 

    Você também pode adicionair novas linhas às suas seções sem precisair recalculair os índices:

     favoritesSection.addRow("new item"); sectionAdapter.notifyDataSetChanged(); 

    Deixe-me tentair propor uma solução nativa.

    Você deve ter uma list de Contatos com uma bandeira de isFavourite como

     private class Contacts{ private String name; private String phoneNumber; private boolean isFavourite; } Private String numberNumber; private class Contacts{ private String name; private String phoneNumber; private boolean isFavourite; } 

    classifique essa matriz com base em isFavourite e contactName como este

    Passe essa list paira o seu ContactRecyclerAdapter. e use dois layouts diferentes paira header e itens como este

    No seu adaptador getItemViewType Layout como este ….

     @Oviewride public int getItemViewType(int position) { if (mCountriesModelList.get(position).isSection) { return SECTION_VIEW; } else { return CONTENT_VIEW; } } } @Oviewride public int getItemViewType(int position) { if (mCountriesModelList.get(position).isSection) { return SECTION_VIEW; } else { return CONTENT_VIEW; } } 

    https://github.com/sayanmanna/LetterSectionedRecyclerView

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.