Android数据绑定和场景转换

时间:2017-10-06 07:32:45

标签: android data-binding android-transitions

在新场景中,第一个场景中没有按钮。

当我更改为新场景时,数据绑定不起作用 - 我无法将onClick从新按钮委托给ViewModel。

我目前的逻辑是:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    TestViewModel testViewModel = new TestViewModel();

    ViewDataBinding binding = DataBindingUtil.inflate(inflater, R.layout.test_fragment, container, false);
    binding.setVariable(BR.vm, viewModel);
    binding.executePendingBindings();

    return mainView.getRoot();
}


public void changeToSceneB() {
        ViewGroup sceneRoot = (ViewGroup) getView().findViewById(R.id.scene_root);
        Scene loadedScene = Scene.getSceneForLayout(sceneRoot, R.layout.test_scene_b, getContext());
        Transition transition = TransitionInflater.from(context).inflateTransition(R.transition.test_transition);
        TransitionManager.go(loadedScene, transition);
    }
}

这是test_fragment XML的一部分:

   <FrameLayout
        android:id="@+id/scene_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appBar"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="@dimen/activity_margin"
        android:layout_marginRight="@dimen/activity_margin"
        android:layout_marginTop="70dp">

        <include
            layout="@layout/test_scene_a"
            binding:vm="@{vm}"/>

    </FrameLayout>

这就是test_scene_b的样子(代码改变了,我离开了最有趣的部分):

<?xml version="1.0" encoding="utf-8"?>

<data>

    <variable
        name="vm"
        type="com.test.test.TestViewModel"/>
</data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/dialog_margin"
        android:gravity="bottom"
        android:orientation="vertical">

        <Button
            style="@style/RoundedButtonGrey"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dialog_margin"
            android:onClick="@{() -> vm.testFirst()}"
            android:text="Test 1"/>

        <Button
            style="@style/RoundedButtonRed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_margin"
            android:onClick="@{() -> vm.testSecond()}"
            android:text="Test 2"/>
    </LinearLayout>

你能告诉我为什么数据绑定不起作用吗?

1 个答案:

答案 0 :(得分:0)

原因是:当您为第二个场景设置xml时,没有为该场景提供绑定变量,因此在输入该场景时,该变量不包含您的视图模型。

解决方法是:

  1. 膨胀TestSceneBBinding
  2. 在此绑定中设置视图模型
  3. 使用Scene(viewGroup1,viewGroup2)构造函数创建Scene
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    TestViewModel testViewModel = new TestViewModel();

    ViewDataBinding binding = DataBindingUtil.inflate(inflater, R.layout.test_fragment, container, false);
    binding.setVariable(BR.vm, viewModel);
    binding.executePendingBindings();

    return mainView.getRoot();
}


public void changeToSceneB() {
    ViewGroup sceneRoot = (ViewGroup) getView().findViewById(R.id.scene_root);
    TestSceneBBinding testSceneBBinding = TestSceneBBinding.inflate(layoutInflater, this.root_group, false) 
    newScreenBinding.vm = viewModel

    Scene loadedScene = Scene(sceneRoot, testSceneBBinding.rootGroup);
    Transition transition = TransitionInflater.from(context).inflateTransition(R.transition.test_transition);
    TransitionManager.go(loadedScene, transition);    
}

希望这会有所帮助:)