Switched to the official SearchWidget.
This commit is contained in:
parent
14516092f5
commit
e80a1317f1
4 changed files with 65 additions and 106 deletions
|
|
@ -1,25 +1,19 @@
|
||||||
package com.kabouzeid.gramophone.ui.activities;
|
package com.kabouzeid.gramophone.ui.activities;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.graphics.PorterDuff;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.v4.view.MenuItemCompat;
|
||||||
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
import android.support.v7.widget.SearchView;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
import android.text.Editable;
|
import android.view.Menu;
|
||||||
import android.text.TextWatcher;
|
|
||||||
import android.view.KeyEvent;
|
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.inputmethod.EditorInfo;
|
|
||||||
import android.widget.EditText;
|
|
||||||
import android.widget.ImageView;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.kabouzeid.appthemehelper.ThemeStore;
|
import com.kabouzeid.appthemehelper.ThemeStore;
|
||||||
import com.kabouzeid.appthemehelper.util.ToolbarContentTintHelper;
|
|
||||||
import com.kabouzeid.gramophone.R;
|
import com.kabouzeid.gramophone.R;
|
||||||
import com.kabouzeid.gramophone.adapter.SearchAdapter;
|
import com.kabouzeid.gramophone.adapter.SearchAdapter;
|
||||||
import com.kabouzeid.gramophone.ui.activities.base.AbsMusicServiceActivity;
|
import com.kabouzeid.gramophone.ui.activities.base.AbsMusicServiceActivity;
|
||||||
|
|
@ -28,24 +22,22 @@ import com.kabouzeid.gramophone.util.Util;
|
||||||
import butterknife.Bind;
|
import butterknife.Bind;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
|
|
||||||
public class SearchActivity extends AbsMusicServiceActivity {
|
public class SearchActivity extends AbsMusicServiceActivity implements SearchView.OnQueryTextListener {
|
||||||
|
|
||||||
public static final String TAG = SearchActivity.class.getSimpleName();
|
public static final String TAG = SearchActivity.class.getSimpleName();
|
||||||
@Bind(R.id.recycler_view)
|
@Bind(R.id.recycler_view)
|
||||||
RecyclerView recyclerView;
|
RecyclerView recyclerView;
|
||||||
@Bind(R.id.toolbar)
|
@Bind(R.id.toolbar)
|
||||||
Toolbar toolbar;
|
Toolbar toolbar;
|
||||||
@Bind(R.id.search_src_text)
|
|
||||||
EditText searchSrcText;
|
|
||||||
@Bind(R.id.search_close_btn)
|
|
||||||
ImageView searchCloseBtn;
|
|
||||||
@SuppressWarnings("ButterKnifeNoViewWithId")
|
|
||||||
@Bind(android.R.id.empty)
|
@Bind(android.R.id.empty)
|
||||||
TextView empty;
|
TextView empty;
|
||||||
|
|
||||||
|
SearchView searchView;
|
||||||
|
|
||||||
private SearchAdapter searchAdapter;
|
private SearchAdapter searchAdapter;
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
private String lastQuery = "";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
@ -70,7 +62,6 @@ public class SearchActivity extends AbsMusicServiceActivity {
|
||||||
});
|
});
|
||||||
|
|
||||||
setUpToolBar();
|
setUpToolBar();
|
||||||
setUpSearchBar();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setUpToolBar() {
|
private void setUpToolBar() {
|
||||||
|
|
@ -80,54 +71,6 @@ public class SearchActivity extends AbsMusicServiceActivity {
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setUpSearchBar() {
|
|
||||||
searchCloseBtn.setColorFilter(ToolbarContentTintHelper.toolbarContentColor(this, ThemeStore.primaryColor(this)), PorterDuff.Mode.SRC_IN);
|
|
||||||
searchCloseBtn.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
searchSrcText.setText("");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
searchSrcText.setTextColor(ToolbarContentTintHelper.toolbarTitleColor(this, ThemeStore.primaryColor(this)));
|
|
||||||
searchSrcText.setHintTextColor(ToolbarContentTintHelper.toolbarSubtitleColor(this, ThemeStore.primaryColor(this)));
|
|
||||||
searchSrcText.setHint(R.string.search_hint);
|
|
||||||
|
|
||||||
searchSrcText.addTextChangedListener(new TextWatcher() {
|
|
||||||
@Override
|
|
||||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterTextChanged(Editable s) {
|
|
||||||
search(searchSrcText.getText().toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
searchSrcText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
|
||||||
@Override
|
|
||||||
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
||||||
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
|
||||||
search(searchSrcText.getText().toString());
|
|
||||||
hideSoftKeyboard();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void hideSoftKeyboard() {
|
|
||||||
Util.hideSoftKeyboard(SearchActivity.this);
|
|
||||||
searchSrcText.clearFocus();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void enableViews() {
|
public void enableViews() {
|
||||||
super.enableViews();
|
super.enableViews();
|
||||||
|
|
@ -140,15 +83,42 @@ public class SearchActivity extends AbsMusicServiceActivity {
|
||||||
recyclerView.setEnabled(false);
|
recyclerView.setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
getMenuInflater().inflate(R.menu.menu_search, menu);
|
||||||
|
|
||||||
|
final MenuItem searchItem = menu.findItem(R.id.search);
|
||||||
|
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
|
||||||
|
searchView.setQueryHint(getString(R.string.search_hint));
|
||||||
|
searchView.setOnQueryTextListener(this);
|
||||||
|
|
||||||
|
MenuItemCompat.expandActionView(searchItem);
|
||||||
|
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onMenuItemActionExpand(MenuItem item) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onMenuItemActionCollapse(MenuItem item) {
|
||||||
|
onBackPressed();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return super.onCreateOptionsMenu(menu);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
if (item.getItemId() == android.R.id.home) {
|
if (item.getItemId() == android.R.id.home) {
|
||||||
super.onBackPressed();
|
onBackPressed();
|
||||||
}
|
}
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void search(@NonNull String query) {
|
private void search(@NonNull String query) {
|
||||||
|
lastQuery = query;
|
||||||
if (searchAdapter != null) {
|
if (searchAdapter != null) {
|
||||||
searchAdapter.search(query);
|
searchAdapter.search(query);
|
||||||
empty.setVisibility(searchAdapter.getItemCount() < 1 ? View.VISIBLE : View.GONE);
|
empty.setVisibility(searchAdapter.getItemCount() < 1 ? View.VISIBLE : View.GONE);
|
||||||
|
|
@ -158,6 +128,25 @@ public class SearchActivity extends AbsMusicServiceActivity {
|
||||||
@Override
|
@Override
|
||||||
public void onMediaStoreChanged() {
|
public void onMediaStoreChanged() {
|
||||||
super.onMediaStoreChanged();
|
super.onMediaStoreChanged();
|
||||||
search(searchSrcText.getText().toString());
|
search(lastQuery);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onQueryTextSubmit(String query) {
|
||||||
|
hideSoftKeyboard();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onQueryTextChange(String newText) {
|
||||||
|
search(newText);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void hideSoftKeyboard() {
|
||||||
|
Util.hideSoftKeyboard(SearchActivity.this);
|
||||||
|
if (searchView != null) {
|
||||||
|
searchView.clearFocus();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,7 @@
|
||||||
|
|
||||||
<android.support.v7.widget.Toolbar
|
<android.support.v7.widget.Toolbar
|
||||||
android:id="@+id/toolbar"
|
android:id="@+id/toolbar"
|
||||||
style="@style/Toolbar">
|
style="@style/Toolbar" />
|
||||||
|
|
||||||
<include layout="@layout/search_bar" />
|
|
||||||
</android.support.v7.widget.Toolbar>
|
|
||||||
|
|
||||||
<FrameLayout
|
<FrameLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
|
|
||||||
|
|
@ -1,37 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_gravity="center_vertical"
|
|
||||||
android:orientation="horizontal">
|
|
||||||
|
|
||||||
<EditText
|
|
||||||
android:id="@+id/search_src_text"
|
|
||||||
android:layout_width="0dp"
|
|
||||||
android:layout_height="36dip"
|
|
||||||
android:layout_gravity="bottom"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:background="@null"
|
|
||||||
android:ellipsize="end"
|
|
||||||
android:imeOptions="actionSearch"
|
|
||||||
android:inputType="text|textAutoComplete|textNoSuggestions"
|
|
||||||
android:minWidth="@dimen/abc_search_view_text_min_width"
|
|
||||||
android:paddingLeft="@dimen/abc_dropdownitem_text_padding_left"
|
|
||||||
android:paddingRight="@dimen/abc_dropdownitem_text_padding_right"
|
|
||||||
android:singleLine="true"
|
|
||||||
android:textCursorDrawable="@null"
|
|
||||||
tools:ignore="RtlHardcoded" />
|
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:id="@+id/search_close_btn"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:layout_gravity="center_vertical"
|
|
||||||
android:background="?attr/selectableItemBackgroundBorderless"
|
|
||||||
android:contentDescription="@string/abc_searchview_description_clear"
|
|
||||||
android:focusable="true"
|
|
||||||
android:paddingLeft="8dip"
|
|
||||||
android:paddingRight="8dip"
|
|
||||||
android:src="@drawable/ic_close_white_24dp" />
|
|
||||||
</LinearLayout>
|
|
||||||
10
app/src/main/res/menu/menu_search.xml
Normal file
10
app/src/main/res/menu/menu_search.xml
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
<item
|
||||||
|
android:id="@+id/search"
|
||||||
|
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
|
||||||
|
android:title="@string/action_search"
|
||||||
|
app:showAsAction="ifRoom|collapseActionView"
|
||||||
|
app:actionViewClass="android.support.v7.widget.SearchView" />
|
||||||
|
</menu>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue