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
versionCode 10
versionName "2.1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
flavorDimensions "default"
buildTypes {
@ -56,7 +56,7 @@ android {
dependencies {
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'
})
implementation 'androidx.appcompat:appcompat:1.2.0'

View File

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

View File

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

View File

@ -9,7 +9,7 @@ buildscript {
google()
}
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
// in the individual module build.gradle files
@ -22,6 +22,7 @@ allprojects {
maven {
url "https://maven.google.com"
}
google()
}
}