我在我的项目中实现了数据绑定但是我可以看到什么奇怪的行为,它没有为我在xml中定义的一些变量生成方法。即使我已多次清理和重建我的项目。 但它正在为我的模型类生成方法。
mBinding.setModel(mProfileViewModel);
我昨天创建的。我真的很困惑发生了什么。如果您有任何想法,请提供帮助。
下面是我的xml。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:chip="http://schemas.android.com/apk/res-auto">
<data>
<import
name="view"
type="android.view.View" />
<variable
name="model"
type="com.android.ui.profile.ProfileViewModel" />
<variable
name="logoutCallBack"
type="com.android.dialog.LogoutDialog.ILogoutListener"/>
<variable
name="logoutDialog"
type="com.android.dialog.LogoutDialog"/>
<variable
name="activity"
type="com.android.ui.profile.ProfileActivity" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/icl_action_bar"
layout="@layout/actionbar_white"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tv_logout"
android:layout_below="@+id/icl_action_bar"
android:background="@color/colorWhite">
<LinearLayout
android:id="@+id/ll_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/profile_picture" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:letterSpacing="0.03"
android:text="Herry"
android:textColor="@color/colorBlack"
android:textSize="26sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:letterSpacing="0.03"
android:lineSpacingMultiplier="1.14"
android:text="Classrooms"
android:textColor="@color/colorBlack"
android:textSize="14sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_normal"
android:layout_marginLeft="@dimen/spacing_normal"
android:layout_marginRight="@dimen/spacing_normal"
android:layout_marginTop="@dimen/spacing_small">
<com.plumillonforge.android.chipview.ChipView
android:id="@+id/chipview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
chip:chip_padding="5dp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorWhite"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/width_divider_subject_activity"
android:background="@color/colorStroke" />
</LinearLayout>
</RelativeLayout>
<TextView
android:id="@+id/tv_logout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal|center_vertical"
android:onClick="@{() -> logoutDialog.showDialog(activity,logoutCallBack)}"
android:paddingBottom="@dimen/spacing_xlarge"
android:paddingTop="@dimen/spacing_xlarge"
android:text="@string/lbl_logout"
android:textColor="@color/colorOnBoardingPageIndicator"
android:textSize="16sp" />
</RelativeLayout>
以下是我的活动
public class ProfileActivity extends BaseActivity implements LogoutDialog.ILogoutListener {
private ActivityProfileBinding mBinding;
private ProfileViewModel mProfileViewModel;
@Override
protected void onCreatingBase(Bundle savedInstanceState, Intent intent) {
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_profile);
mProfileViewModel = ViewModelProviders.of(this, new ProfileViewModelFactory(getApplication())).get(ProfileViewModel.class);
mProfileViewModel.getLogoutResponse().observe(this, mLogoutObserver);
mBinding.setModel(mProfileViewModel);
registerObservers(mProfileViewModel);
mProfileViewModel.fetchClassRooms();
}
/**
* Observer listening to the response of Logout API
*/
private Observer<ApiResponse> mLogoutObserver = new Observer<ApiResponse>() {
@Override
public void onChanged(ApiResponse classRoomResponses) {
}
};
@Override
public void onClick(View view) {
}
@Override
public void onYesClicked() {
mProfileViewModel.doLogout();
}
}
LogoutDialog类
public class LogoutDialog {
public static void showDialog(Context context,final ILogoutListener logoutListner ){
new AlertDialog.Builder(context)
.setMessage(context.getResources().getString(R.string.logout_desc))
.setCancelable(false)
.setPositiveButton(context.getResources().getString(R.string.yes_text), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
logoutListner.onYesClicked();
}
})
.setNegativeButton(context.getResources().getString(R.string.no_text), null)
.show();
}
public interface ILogoutListener {
void onYesClicked();
}
}