Not sure how it works but according to this post it's possible to bind specific listener by using different namespaces.
I wanted to do the same thing with a searchview and bind a QueryTextListener to it but I get the following error :
Cannot find the setter for attribute 'bind:setOnQueryTextListener'
with parameter type android.widget.SearchView.OnQueryTextListener.
What I did in my ViewModel :
public class MembersFragmentViewModel extends BaseObservable {
private Context context;
private MembersAdapter adapter;
private RecyclerView recyclerView;
public MembersFragmentViewModel(Context context, MembersAdapter adapter, RecyclerView recyclerView) {
this.context = context;
this.adapter = adapter;
this.recyclerView = recyclerView;
}
public SearchView.OnQueryTextListener getQueryTextListener(){
return new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
List<Contact> filteredModelList = filter(adapter.getContacts(), query);
adapter.animateTo(filteredModelList);
if(recyclerView != null)
recyclerView.scrollToPosition(0);
return true;
}
};
}
//Code ...
And the xml :
My namespace is declared in the layout tag like this :
xmlns:bind="http://schemas.android.com/apk/res-auto"
And my SearchView :
<android.support.v7.widget.SearchView android:id="@+id/searchview"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/rounded_search_view_background"
bind:setOnQueryTextListener="@{viewModel.QueryTextListener}">
</android.support.v7.widget.SearchView>
This is my data tag :
<data>
<variable
name="viewModel"
type="mypackagename.viewmodel.members.MembersFragmentViewModel"/>
</data>
Many thanks for any clue !
In my case the answer was just a wrong package name.
My SearchView was declared in xml with the following package name :
android.support.v7.widget.SearchView
And the package I used in the ViewModel was
android.widget.SearchView
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With