Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android databinding and Scene transition

In new scene there are buttons that are not present in first scene.

When I change to the new scene databinding does not work - I am not able to delegate onClick from new buttons to the ViewModel.

My current logic is:

@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);
    }
}

This is part of 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>

and this is how does test_scene_b looks like (code changed, I left most intresting part):

<?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>

Could you tell me why databinding does not work?

like image 791
Krystian Bersztolc Avatar asked Nov 18 '22 07:11

Krystian Bersztolc


1 Answers

The reason is: when you setup xml for the second Scene, you not provide binding variable for this scene, so when it is entered it is not contain your view model.

Workaround is:

  1. Inflate TestSceneBBinding
  2. Set view model in this binding
  3. Use Scene(viewGroup1, viewGroup2) constructor to create 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);    
}

Hope it'll help :)

like image 66
Peter Staranchuk Avatar answered Dec 16 '22 04:12

Peter Staranchuk