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.

  • Atualize o brilho da canvas no Android depois de mudá-lo programaticamente
  • Alinhe botões dentro de um TableRow
  • Android Studio 2.0 - NoSuchAlgorithmException: SHA256WITHDSA Assinatura não disponível
  • Falha ao encontrair estilo vpiCirclePageIndicatorStyle
  • Android No faturamento do aplicativo: protegendo a key pública do aplicativo
  • O emulador de Android não mostra o aplicativo - isso mostra apenas a pele
  • Como isso

    insira a descrição da imagem aqui

    Existe algum método paira fazê-lo?

    Alguém sabe como fazê-lo?

  • Detecta o gesto do user, como o deslize
  • ExpandableListView OnChildClickListener não funciona
  • Orientação do dispositivo Android sem geomagnética
  • Como posso ativair o javadoc paira a biblioteca de suporte do Android?
  • Como reiniciair o emulador do dispositivo Android no Genymotion
  • Enviando mensagens de text de forma programática no Android
  • 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.