material-components / material-components/material-components-android

[Search] Unable to apply search string to RecyclerView

Aperta
#4,348 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug Widget: Search
Lingua principale
Java
Stelle
17.4k
Fork
3.2k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Description: I have below structure in my activity_main.xml, incl. SearchBar and SearchView, similar to the documentation. I'm using a ViewPager2 with fragments (one for each tab of the TabLayout, including each a RecyclerView list). Basically, the GUI looks like the Catalog app RecyclerView Demo.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:layoutDirection="locale"
    android:textDirection="locale"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stateListAnimator="@null"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:layout_scrollEffect="compress" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:contentDescription="@string/app_name"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabMode="scrollable" />

        <com.google.android.material.search.SearchBar
            android:id="@+id/search_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="searchbar_hint" />

    </com.google.android.material.appbar.AppBarLayout>

    <com.google.android.material.search.SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="searchview_hint"
        app:layout_anchor="@id/search_bar">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/search_results_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />

    </com.google.android.material.search.SearchView>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Expected behavior: This is how I understand the Material Design search functionality for my use case:

  • I click on the SearchBar, the SearchView opens. I enter a search string, the original RecyclerView of the currently selected tab will be searched for the string and the results are shown in the RecyclerView with id search_results_recycler_view (i.e. the one within SearchView).
  • With setOnEditorActionListener / SearchView.getText, I'm listening for the search string.

Setup:

  • SearchBar and SearchView are defined in MainActivity
  • RecyclerViews and adapters are defined in the fragments
  • Custom adapter for RecyclerView has a getFilter() method implemented

The problem is that I'm not sure on how to...
a) transfer the search string from SearchView to the original RecyclerView list of the currently selected tab and then...
b) transfer the filtered list back to the RecyclerView with id search_results_recycler_view within SearchView.

Shall I use a ViewModel for each transfer part? Easy for a) where I can use String, but for b) it tricky as ViewModel doesn't allow an ArrayList<CustomAdapter>?

The Catalog RecyclerView Demo also doesn't show how to do that as all search results are hard-coded and have no association with the original text, so entering a search string has no effect.

Can you please share some insights (e.g. in the documentation) on how to implement this (best practice) or would you be able to amend the Catalog demo accordingly?

Source code: See above XML code.

Android API version: Tested on Android API 30-35

Material Library version: 1.13.0-alpha07

Device: Pixel 8 emulators, Nokia 7.2

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da docs/components/Search.md e catalog/java/io/material/catalog/search/SearchRecyclerDemoActivity.java, quindi segui activity_main.xml, i fragment ViewPager2 e i relativi adapter RecyclerView. Determina come devono essere collegati la scheda corrente, il testo di ricerca e i risultati filtrati. Il lavoro è completato quando il pattern documentato o l’esempio del catalogo esegue una ricerca reale e visualizza i risultati corrispondenti in search_results_recycler_view.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
android, java, xml
Ambito
documentation, mobile
Tipo di issue
Documentazione
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.