SwitchCompat通过绑定,而不是处理点击

时间:2018-06-04 09:34:11

标签: android-databinding

这是我的xml布局文件:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <import type="android.view.View" />    
        <variable
            name="handler"
            type="com.myproject.AdSettingsFragment" />    
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 <android.support.v7.widget.SwitchCompat
                android:id="@+id/pushSwitchCompat"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:checked="true"
                android:onCheckedChanged="@{(switch, checked) -> handler.onCheckedChanged(checked)}"
                android:theme="@style/SwitchCompatTheme"/>

    </android.support.constraint.ConstraintLayout>
</layout>

这里我要处理的java代码点击SwitchCompat组件:

public class AdSettingsFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        AdvertisingSettingsBinding  binding = DataBindingUtil.inflate(inflater, R.layout.advertising_settings, container, false);
        return binding.getRoot();
    }    

    public void onCheckedChanged(boolean isChecked) {
    }
}

但是当我点击组件SwitchCompat时,我的片段onCheckedChanged中的方法AdSettingsFragment.java不会被调用。 为什么不工作?

0 个答案:

没有答案