2017-08-09 1 views
0

Ich benutze Android SearchView, um eine Suchschaltfläche in meiner Aktionsleiste zu haben. Ich folgte dem Tutorial this von Google Developers auf YouTube und fügte ein paar Verbesserungen basierend auf meinen eigenen Bedürfnissen hinzu. Wenn ich jedoch das Suchfeld verlasse, das beim Klicken auf die Schaltfläche erscheint, verschwindet die Suchschaltfläche!Der Menüeintrag Android Search wird ausgeblendet, wenn Sie die Zurück-Taste drücken/das Suchfeld kollabiert

Hier ist meine onCreateOptionsMenu und onOptionsItemSelected Funktionen

onCreateOptionsMenu:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 


    return true; 
} 

onOptionsItemSelected:

@Override 
public boolean onOptionsItemSelected(final MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    switch (id) { 
     case R.id.action_settings: 
      // TODO: settings activity 
      break; 
     case R.id.action_logout: 
      FirebaseAuth.getInstance().signOut(); 
      break; 
     case R.id.action_search: 
      // Initialize everything here, because it produces a NullPointerException if initialized in onCreateOptionsMenu 
      SearchView searchView = (SearchView) MenuItemCompat.getActionView(item); 
      SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE); 
      ComponentName componentName = new ComponentName(this, SearchableActivity.class); 
      searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName)); 

      searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
       @Override 
       public boolean onQueryTextSubmit(String query) { 

        return false; 
       } 

       @Override 
       public boolean onQueryTextChange(String newText) { 

        return false; 
       } 
      }); 
      break; 
    } 

    return super.onOptionsItemSelected(item); 
} 

Und hier ist mein menu_main.xml Layout-Datei

<menu 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" 
tools:context="com.magnus.inline.MainActivity"> 
<item 
    android:id="@+id/action_search" 
    android:icon="@drawable/ic_action_search" 
    android:title="Search" 
    app:actionViewClass="android.support.v7.widget.SearchView" 
    app:showAsAction="ifRoom|collapseActionView"/> 
<item 
    android:id="@+id/action_settings" 
    android:orderInCategory="100" 
    android:title="@string/action_settings" 
    app:showAsAction="never" /> 
<item 
    android:id="@+id/action_logout" 
    android:orderInCategory="200" 
    android:title="@string/action_logout" 
    app:showAsAction="never" /> 

Und searchable.xml Layout-Datei

<?xml version="1.0" encoding="utf-8"?> 
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
    android:label="@string/app_name" 
    android:hint="Search" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
</searchable> 

Images

EDIT:

Ich bemerkte, dass die einzige Zeit, die Suchtaste verschwindet, wenn ich die Optionen gedrückt haben im "Suchmodus" (siehe das letzte Bild im Link). Ich habe das Gefühl, dass wenn das Optionsmenü angezeigt wird, die "action_search" irgendwie zu einem Optionsmenü-Element anstelle eines Aktionsleistenelements (?) Umgewandelt wird. Wenn ich versuche, MenuItemCompat.getActionView() eines searchview MenuItem in der onCreateOptionsMenu -Funktion aufzurufen, stürzt meine App einfach ab und sagt, dass sie versucht, sie an einer Null-Objektreferenz auszuführen.

Hier ist meine activity_main.xml Layout-Datei

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.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" 
    tools:context="com.magnus.inline.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/main_appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/main_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

     </android.support.v7.widget.Toolbar> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AlertDialog.AppCompat.Light" 
      android:background="@color/colorPrimaryDark" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</android.support.design.widget.CoordinatorLayout> 
+0

Können Sie ein Bild Problem hinzu? –

+0

Ich habe Ihren Code in Android Studio kopiert und konnte das Problem nicht reproduzieren. Sind Sie sicher, dass Sie keinen wichtigen Code weggelassen haben? –

+0

Ich bin auch neugierig auf diesen Kommentar: "Initialisieren Sie alles hier, weil es eine NullPointerException erzeugt, wenn in OnCreateOptionsMenu initialisiert". Vielleicht ist diese NPE das, was dir Probleme bereitet. FWIW, ich hatte noch nie NPE, die 'SearchView' in' onCreateOptionsMenu() ' –

Antwort

3

Statt

app:showAsAction="ifRoom|collapseActionView" 

Wechsel zu

app:showAsAction="always|collapseActionView 
+0

Es hat funktioniert! Vielen Dank für die Lösung! –

Verwandte Themen