I have both searchView
and recyclerView
in same layout.
recyclerView
is using filter and it's working properly.
The problem occurred when i entered query on searchView
. Instead of let me finish typing, searchView
will lose focus after i entered first character of my query. Let's say i want show all items which contain "1234" in its name, searchView
will lose focus after i entered "1" then display all items which contain "1". I have to click that searchView
again if i want to continue.
Here is my layout (in fragment):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.SearchView
android:id="@+id/sv_test"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
android:inputType="number"
app:iconifiedByDefault="false"
app:queryBackground="@android:color/transparent"
app:queryHint="Hint"
app:theme="@style/AppSearchView">
</android.support.v7.widget.SearchView>
<View
android:id="@+id/divider50"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/light_grey"
android:visibility="visible" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_test"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Here is my filter command:
private void setSearchview() {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String query) {
adapter.getFilter().filter(query);
return false;
}
});
}
I have similar layout in custom alert dialog and it's working fine without any issues. Weird right?
If you have a focusable item (like an EditText) inside your RecyclerView, it might be stealing the focus on the first update.
To disable the RecyclerView gaining any focus, you can add the descendantFocusability
parameter to the XML like this:
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_test"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content" />
If you need to focus inside the RecyclerView, you might work out a solution which sets this parameter dynamically (so it turns focus off before the update, and back on after it).