칩 의 닫기 아이콘을 클릭 할 때 사용자 지정 BindingAdapter 를 작성했습니다 .
@BindingAdapter("onCloseClicked")
fun Chip.onCloseClicked(onCloseClicked: () -> Unit) {
setOnCloseIconClickListener { onCloseClicked() }
}
다음과 같이 내 레이아웃에 바인딩합니다.
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="viewModel"
type="com.example.playground.MyViewModel" />
</data>
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chip 1"
app:closeIconEnabled="true"
app:onCloseClicked="@{() -> viewModel.chip1CloseClicked()}" />
<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chip 2"
app:closeIconEnabled="true" />
</LinearLayout>
</layout>
위와 같이 1 개의 칩만 바인딩하면 모두 정상적으로 작동합니다. 두 번째 칩도 다음과 같이 바인딩 할 때 :
<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chip 2"
app:closeIconEnabled="true"
app:onCloseClicked="@{() -> viewModel.chip2CloseClicked()}" />
데이터 바인딩 오류로 인해 앱이 더 이상 빌드되지 않습니다.
com / example / playground / databinding / ActivityMainBindingImpl.java
오류 : 반환 문 누락
이것이 발생하는 이유와 해결 방법에 대한 아이디어가 있습니까?
추신 : https://github.com/fmweigl/ChipDatabinding 에서 직접 시도해 볼 수 있습니다.
그 문제는 이미 그 질문 에서 논의되었습니다 -데이터 바인딩 컴파일러는 유효하지 않은 자바 코드를 생성합니다 :
// Listener Stub Implementations
// callback impls
public final kotlin.Unit _internalCallbackInvoke(int sourceId ) {
switch(sourceId) {
case 1: {
// localize variables for thread safety
// viewModel
com.example.playground.MyViewModel viewModel = mViewModel;
// viewModel != null
boolean viewModelJavaLangObjectNull = false;
viewModelJavaLangObjectNull = (viewModel) != (null);
if (viewModelJavaLangObjectNull) {
viewModel.chip1CloseClicked();
}
return null;
}
case 2: {
// localize variables for thread safety
// viewModel
com.example.playground.MyViewModel viewModel = mViewModel;
// viewModel != null
boolean viewModelJavaLangObjectNull = false;
viewModelJavaLangObjectNull = (viewModel) != (null);
if (viewModelJavaLangObjectNull) {
viewModel.chip2CloseClicked();
}
return null;
}
}
}
여기 default
에 return
문이있는 분기 가 없습니다.
이를 해결하기 위해 귀하의 경우 @BindingAdapter
주석으로 표시된 기능의 서명 을 다음으로 변경할 수 있습니다.
@BindingAdapter("onCloseClicked")
fun Chip.onCloseClicked(clickListener: View.OnClickListener?) {
setOnCloseIconClickListener(clickListener)
}
변경 사항이 적용되면 프로젝트가 성공적으로 컴파일됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다