Can I databind a ProgressBar in Android?

前端 未结 4 1775
一向
一向 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 16:01

    To get the TextView to update when the SeekBar changes, bind to progress change through the android:onProgressChanged attribute. This expects a public method in the Model with the signature of SeekBarBindingAdapter.OnProgressChanged:

    View

    
    

    Model

    public class Model {
        public ObservableField seekBarValue = new ObservableField<>("");
    
        public void onValueChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
            seekBarValue.set(progresValue + "");
        }
    }
    

    In general I advice you to look at the source code for all the Adapter classes made for Data Binding. If you for instance navigate to the file SeekBarBindingAdapter.java in Android Studio (menu Navigate>File) you'll learn all the event methods you can bind to through the adapters there. This particular feature is available because Google have implemented the following adapter:

    @BindingAdapter("android:onProgressChanged")
    public static void setListener(SeekBar view, OnProgressChanged listener) {
        setListener(view, null, null, listener);
    }
    

提交回复
热议问题