dan*_*ela 24 data-binding android android-layout swiperefreshlayout
我正在使用android数据绑定库.如果我想让视图可见,我可以写这样的东西:
<TextView
android:id="@+id/label_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@{habitListViewModel.message}"
app:visibility="@{habitListViewModel.hasError ? View.VISIBLE : View.GONE}" />
Run Code Online (Sandbox Code Playgroud)
是否有以类似(xml)方式绑定到swipeRefreshLayout的刷新属性的选项?
目前我通过调用setRefreshing(true/false)在代码中设置它,但是希望在xml中使它保持一致.
Ent*_*eco 62
不需要破解.关键是在SwipeRefreshLayout文档中查找公共方法.通常,数据绑定将查找没有该set部分的相应名称.你会在那里找到:
OnRefreshListener
由于OnRefreshListener是一个公共接口,只需一个方法,您可以在绑定中直接使用它,如下所示:
app:onRefreshListener="@{() -> viewModel.onRefresh()}"
Run Code Online (Sandbox Code Playgroud)
更新状态
对于这个,您使用另一个公共方法,转换为:
app:refreshing="@{viewModel.isLoading}"
Run Code Online (Sandbox Code Playgroud)
总之,它看起来像这样:
<data>
<variable name="viewModel" type="ViewModel" />
</data>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:refreshing="@{viewModel.isLoading}"
app:onRefreshListener="@{() -> viewModel.onRefresh()}">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)
视图模型:
public class ViewModel implements DataProvider.Callback {
public ObservableBoolean isLoading = new ObservableBoolean();
private DataProvider provider;
MasterViewModel(@NonNull final DataProvider provider) {
this.provider = provider;
}
/* Needs to be public for Databinding */
public void onRefresh() {
isLoading.set(true);
provider.fetch(this);
}
public void onReady(List results){
isLoading.set(false);
}
public void onError(Exception oops){
isLoading.set(false);
Log.e("Stack", oops);
}
}
Run Code Online (Sandbox Code Playgroud)
rom*_*4ek 22
更新:
作为数据绑定映射从xml属性名称set{AttributeName},您可以使用app:refreshing,因为数据绑定将成功提供setRefreshing方法的值SwipeRefreshLayout(幸运的是我们存在并且是公共的):
<android.support.v4.widget.SwipeRefreshLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:refreshing="@{habitListViewModel.isLoading}">
...
//ListView,RecyclerView or something
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)
而已!请注意,您可以简单地使用@{true}或@{false}代替@{habitListViewModel.isLoading}.希望有所帮助.
| 归档时间: |
|
| 查看次数: |
10659 次 |
| 最近记录: |