How DataBindingComponents works on per-layout basis? [duplicate]

孤街浪徒 提交于 2019-12-23 03:51:30

问题


Android DataBinding Library is a charming lib for me to learn MVVM. Now there's a problem, how to play an animation before update the text to the UI, on a per-layout basis. (Not a solution for global layouts using BindingAdapter , using a static binding adapter.)

From the IO16 video I know perhaps I can use DataBindingComponent to achieve this effect, just like the setImageUrl example, but I can not find any sample codes on how exactly DataBindingComponents and BindingAdapter annotated instance methods work, can any one provide some detail on this?

==update 2016-07-06==

I know I can using a static binding adapter with a custom tag, but that's not what I want.

==update 2017-08-04== I don't know why this question is marked as duplicate, the other question is totally different if you know about android data binding. Just don't know how to remove the duplication mark, so making an edit here.


回答1:


After watching the io16 session again I finally found the solution:

Firstly, create a class with any method annotated as a BindingAdapter.

public class OptionBindingAdapter {
    private boolean mOptionsShowing;

    @BindingAdapter("android:text")
    public void setOption(TextView button, String text) {
        if (text == null) {
            return;
        }

        if (button.getTranslationY() > 0) {
            button.setText(text);
            button.setVisibility(View.VISIBLE);
            button.animate()
                    .translationY(0)
                    .start();
        } else {
            button.animate()
                    .translationY(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, button.getResources().getDisplayMetrics()))
                    .start();
        }
    }
}

Then create a class that implement DataBindingComponent, there you just create a getter method which returns a instance of the class above.

public class OptionBindingComponent implements DataBindingComponent {
    private OptionBindingAdapter mOptionBindingAdapter = new OptionBindingAdapter();

    public OptionBindingAdapter getOptionBindingAdapter() {
        return mOptionBindingAdapter;
    }
}

Now you can create any instance of this component and use it when binding, for example in Activity#onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBinding  = DataBindingUtil.setContentView(this, R.layout.activity_main, new OptionBindingComponent());
    mViewModel = new OptionsViewModel(new OptionsRepository(), this);
    mBinding.setVariable(me.zhanghailin.androiddatabindingwithanimations.BR.options, mViewModel);
}

Done! that's it, the effect is that the binding adapter will be used for this very binding, instead of the default component.



来源:https://stackoverflow.com/questions/38204239/how-databindingcomponents-works-on-per-layout-basis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!