问题描述
不知道它是如何工作的,但根据这篇文章可以通过使用不同的命名空间来绑定特定的监听器。
我想用一个searchview做同样的事情,并将QueryTextListener绑定到它,但是我收到以下错误:
找不到属性bind:setOnQueryTextListener
的参数类型为android.widget.SearchView.OnQueryTextListener的setter。
我在ViewModel中所做的:
public class MembersFragmentViewModel extends BaseObservable {
private Context context;
private MembersAdapter适配器;
private RecyclerView recyclelerView;
public MemberFragmentViewModel(Context context,MembersAdapter adapter,RecyclerView recyclelerView){
this.context = context;
this.adapter = adapter;
this.recyclerView = recyclelerView;
}
public SearchView.OnQueryTextListener getQueryTextListener(){
返回新的SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query){
列表< Contact> filteredModelList = filter(adapter.getContacts(),query);
adapter.animateTo(filteredModelList);
if(recyclelerView!= null)
recyclelerView.scrollToPosition(0);
返回true;
}
};
}
//代码...
而xml: p>
我的命名空间在布局标签中声明如下:
xmlns: bind =http://schemas.android.com/apk/res-auto
而我的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>
这是我的数据标签:
< data>
< variable
name =viewModel
type =mypackagename.viewmodel.members.MembersFragmentViewModel/>
< / data>
非常感谢任何线索!
在我的情况下,答案只是一个错误的包名称。
我的SearchView在xml中被声明为具有以下包名称: / p>
android.support.v7.widget.SearchView
我在ViewModel中使用的包是
android.widget.SearchView
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
这篇关于Android数据绑定:找不到属性的setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!