Tôi đang cố gắng ràng buộc sự kiện trên chế độ xem tùy chỉnh với thư viện ràng buộc dữ liệu Android mới nhưng gặp sự cố.Kết nối dữ liệu với trình nghe tùy chỉnh trên chế độ xem tùy chỉnh
ở đây là phần có liên quan của giao diện tùy chỉnh của tôi:
public class SuperCustomView extends FrameLayout {
private OnToggleListener mToggleListener;
public interface OnToggleListener {
void onToggle(boolean switchPosition);
}
public void setOnToggleListener(OnToggleListener listener) {
mToggleListener = listener;
}
.../...
}
Tôi cố gắng sử dụng Custom View này và ràng buộc sự kiện onToggle
như sau:
<data>
<variable
name="controller"
type="com.xxx.BlopController"/>
</data>
<com.company.views.SuperCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:onToggle="@{controller.toggleStrokeLimitation}"
app:custom_title="Blah"
app:custom_summary="Bloh"
app:custom_widget="toggle"/>
đâu toggleStrokeLimitation
là một phương pháp trên bộ điều khiển:
public void toggleStrokeLimitation(boolean switchPosition) {
maxStrokeEnabled.set(switchPosition);
}
Tôi hiểu lỗi này hoặc khi biên dịch:
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:onToggle' with parameter type java.lang.Object. file:/path/to/androidapp/app/src/main/res/layout/fragment_stroke.xml loc:36:35 - 36:67 ****\ data binding error ****
Tôi đã cố gắng sử dụng android:onToggle
thay vì app:onToggle
nhưng gặp phải lỗi tương tự.
Khi đọc binding events section of the doc, tôi cảm thấy mình có thể cắt bất kỳ phương pháp nào khỏi bộ điều khiển thành sự kiện onToggle
.
Khung khuôn có bọc các phương pháp controller.toggleStrokeLimitation
vào một SuperCustomView.OnToggleListener
không? Bất kỳ gợi ý nào về loại ma thuật đằng sau onClick
hiện có do khung làm việc cung cấp?
Cố gắng triển khai setter tùy chỉnh cho thuộc tính onToggle http://developer.android.com/tools/data-binding/guide.html#custom_setters và đảm bảo rằng controller.toggleStrokeLimitation là loại OnToggleListener –
Loại điều khiển 'là gì. toggleStrokeLimitation'?. Có vẻ như nó là một 'Object' và setter của bạn mong đợi một' OnToggleListener' – yigit
Bạn nói đúng, tôi có thể làm điều đó nhưng tôi muốn ánh xạ sự kiện đó đến một phương thức tùy chỉnh. Như được giải thích trong https://developer.android.com/tools/data-binding/guide.html#binding_events tôi sẽ chỉnh sửa câu hỏi bằng lý do của tôi. – fstephany