Android DataBinding Custom Binding Adapter Warning

后端 未结 3 815
感动是毒
感动是毒 2020-12-07 22:46

I followed the data binding documentation for Custom Binding Adapter for image loading from official Android Developers site: http://develo

3条回答
  •  生来不讨喜
    2020-12-07 23:01

    Try this, work for me!. I hope this can help you. Simple way to change image resource without binding adapter.

    
    

    and View Model Class :

    public ObservableField imageButton;
    private Context context;
    
    //Constructor
    public MainVM(Context context) {
        this.context = context;
        imageButton = new ObservableField<>();
        setImageButton(R.mipmap.image_default); //set image default
    }
    
    public void onClickImageButton(View view) {
        setImageButton(R.mipmap.image_change); //change image
    }
    
    private void setImageButton(@DrawableRes int resId){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            imageButton.set(context.getDrawable(resId));
        }else{
            imageButton.set(context.getResources().getDrawable(resId));
        }
    }
    

提交回复
热议问题