Improve searchview

This commit is contained in:
Thomas 2020-12-07 15:17:47 +01:00
parent 5ec97f3f81
commit 891198b71f
4 changed files with 15 additions and 8 deletions

View File

@ -8,7 +8,7 @@ android {
targetSdkVersion 30 targetSdkVersion 30
versionCode 10 versionCode 10
versionName "2.1.1" versionName "2.1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
} }
flavorDimensions "default" flavorDimensions "default"
buildTypes { buildTypes {
@ -56,7 +56,7 @@ android {
dependencies { dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs') implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations' exclude group: 'com.android.support', module: 'support-annotations'
}) })
implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.appcompat:appcompat:1.2.0'

View File

@ -29,9 +29,9 @@ import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.SearchView;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.databinding.DataBindingUtil; import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
@ -174,7 +174,7 @@ public class MainActivity extends AppCompatActivity {
} }
} }
private String previousQuery = "";
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
toolbarMenu = menu; toolbarMenu = menu;
@ -182,15 +182,21 @@ public class MainActivity extends AppCompatActivity {
inflater.inflate(R.menu.main, menu); inflater.inflate(R.menu.main, menu);
MenuItem actionFilterItem = menu.findItem(R.id.action_filter); MenuItem actionFilterItem = menu.findItem(R.id.action_filter);
searchView = (SearchView) actionFilterItem.getActionView(); searchView = (SearchView) actionFilterItem.getActionView();
searchView.setOnQueryTextFocusChangeListener((v, hasFocus) -> {
if (hasFocus) {
searchView.setQuery(previousQuery, false);
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
HomeFragment home = (HomeFragment) fragments.get(0); previousQuery = query.trim();
home.filter(query);
if (!searchView.isIconified()) { if (!searchView.isIconified()) {
searchView.setIconified(true); searchView.setIconified(true);
} }
menu.findItem(R.id.action_filter).collapseActionView(); menu.findItem(R.id.action_filter).collapseActionView();
HomeFragment home = (HomeFragment) fragments.get(0);
home.filter(query);
return false; return false;
} }

View File

@ -5,7 +5,7 @@
android:id="@+id/action_filter" android:id="@+id/action_filter"
android:icon="@drawable/ic_search" android:icon="@drawable/ic_search"
android:title="@string/menu_action_filter" android:title="@string/menu_action_filter"
app:actionViewClass="android.widget.SearchView" app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always|collapseActionView" /> app:showAsAction="always|collapseActionView" />
<item <item
android:id="@+id/action_settings" android:id="@+id/action_settings"

View File

@ -9,7 +9,7 @@ buildscript {
google() google()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:4.1.0' classpath 'com.android.tools.build:gradle:4.1.1'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
@ -22,6 +22,7 @@ allprojects {
maven { maven {
url "https://maven.google.com" url "https://maven.google.com"
} }
google()
} }
} }