Android 数据绑定 - 如何将 ViewStub 与数据绑定一起使用 [英] Android Data Binding - how to use ViewStub with data binding

查看:42
本文介绍了Android 数据绑定 - 如何将 ViewStub 与数据绑定一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何可以将 viewStubs 与 dataBinding 一起使用吗?ViewStubProxy 能帮上忙吗?

我的存根电流看起来像这样:

 

但是当我给 viewStub 充气时这个布局会被替换,那么 ViewStubs 如何与 android dataBinding 一起使用?

这是我从文档中看到的:

<块引用><块引用>

视图存根

ViewStubs 与普通的 Views 有点不同.他们开始不可见,并且当它们可见或被明确告知时充气,它们通过充气另一个来替换自己在布局中布局.

因为 ViewStub 基本上从 View 层次结构中消失了,绑定对象中的 View 也必须消失以允许收藏.因为视图是最终的,一个 ViewStubProxy 对象需要ViewStub 的位置,让开发者可以访问 ViewStub当它存在并且还可以访问膨胀的视图层次结构时ViewStub 已膨胀.

当膨胀另一个布局时,必须为该布局建立一个绑定新版面.因此,ViewStubProxy 必须监听 ViewStub 的ViewStub.OnInflateListener 并在那时建立绑定.由于只能存在一个,因此 ViewStubProxy 允许开发人员在其上设置一个 OnInflateListener ,它将在建立后调用绑定.

解决方案

只需按照文档的说明设置监听器:

mBinding.viewStub.setOnInflateListener(new ViewStub.OnInflateListener() {@覆盖公共无效 onInflate(ViewStub 存根,视图膨胀){ViewStubBinding 绑定 = DataBindingUtil.bind(inflated);binding.setModel(模型);}});公共无效inflateViewStub(查看视图){如果 (!mBinding.viewStub.isInflated()) {mBinding.viewStub.getViewStub().inflate();}}

Is there anyway to use viewStubs with dataBinding ? can ViewStubProxy help ?

My stub current looks like this:

    <ViewStub
  android:id="@+id/stub_import"
  android:inflatedId="@+id/panel_import"

  android:layout="@layout/progress_overlay"

  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="@{myobject.bottom ? bottom:top}" />

But this layout will be replaced when i inflate the viewStub so how can ViewStubs be used with android dataBinding ?

this is what i see from the docs:

ViewStubs

ViewStubs are a little different from normal Views. They start off invisible and when they either are made visible or are explicitly told to inflate, they replace themselves in the layout by inflating another layout.

Because the ViewStub essentially disappears from the View hierarchy, the View in the binding object must also disappear to allow collection. Because the Views are final, a ViewStubProxy object takes the place of the ViewStub, giving the developer access to the ViewStub when it exists and also access to the inflated View hierarchy when the ViewStub has been inflated.

When inflating another layout, a binding must be established for the new layout. Therefore, the ViewStubProxy must listen to the ViewStub's ViewStub.OnInflateListener and establish the binding at that time. Since only one can exist, the ViewStubProxy allows the developer to set an OnInflateListener on it that it will call after establishing the binding.

解决方案

Just set the listener as the doc says :

mBinding.viewStub.setOnInflateListener(new ViewStub.OnInflateListener() {
    @Override
    public void onInflate(ViewStub stub, View inflated) {
        ViewStubBinding binding = DataBindingUtil.bind(inflated);
        binding.setModel(model);
    }
});



public void inflateViewStub(View view) {
    if (!mBinding.viewStub.isInflated()) {
        mBinding.viewStub.getViewStub().inflate();
    }
}

这篇关于Android 数据绑定 - 如何将 ViewStub 与数据绑定一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆