Can I databind a ProgressBar in Android?

前端 未结 4 1773
一向
一向 2021-02-13 15:37

Is it possible to use data-binding for a SeekBar or ProgressBar in Android? I have this data element:


  

        
4条回答
  •  深忆病人
    2021-02-13 15:59

    Well you may need to use two-way binding like below.

    Add in binding utils class

    private static final String ANDROID_PROGRESS = "android:progress";
    
    @BindingAdapter(ANDROID_PROGRESS)
    public static void setSeekbarProgress(SeekBar seekBar, int progress) {
        try {
            seekBar.setProgress(progress);
        } catch (Resources.NotFoundException nfe) {
            nfe.printStackTrace();
        }
    }
    
    @InverseBindingAdapter(attribute = ANDROID_PROGRESS)
    public static int getSeekbarProgress(SeekBar seekBar) {
        try {
            return seekBar.getProgress();
        } catch (Resources.NotFoundException nfe) {
            nfe.printStackTrace();
            return 0;
        }
    }
    

    Add the observable in view model

    var child1Age = ObservableField(1)
    

    In the layout

     
    
            
    

提交回复
热议问题