Android Data binding - Error:(119, 29) Identifiers must have user defined types from the XML file. main_radio_subscribe is missing it

前端 未结 3 1603
轻奢々
轻奢々 2021-01-07 21:16

I been trying to control the visibility of a view using the Implicit Attribute Listeners(reference) in android data binding which allows to access views by id and access at

3条回答
  •  走了就别回头了
    2021-01-07 21:33

    Step 1: create BindingAdapter:

    @BindingAdapter("android:visibility")
    public static void setVisibility(final View view, @IdRes int layourId) {
        SwitchCompat switcher = (SwitchCompat)view.getRootView().findViewById(layourId)
        switcher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                view.setVisibility(isChecked ? View.VISIBLE : View.GONE);
            }
        }
    }
    

    Step 2: import R class in databinding data section at you layout.xml:

    
         
    
    

    Step 3: bind custom view to your switcher like this:

    
    
    
    

提交回复
热议问题