2015-09-04 25 views
14

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?

+0

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 –

+0

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

+0

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

Trả lời

17
@BindingMethods(@BindingMethod(type = SuperCustomView.class, attribute = "app:onToggle", method = "setOnToggleListener")) 
public class SuperCustomView extends FrameLayout { 
    private OnToggleListener mToggleListener; 

    public interface OnToggleListener { 
     void onToggle(boolean switchPosition); 
    } 

    public void setOnToggleListener(OnToggleListener listener) { 
     mToggleListener = listener; 
    } 
    .../... 
} 

Hack của tôi để kiểm tra mã là:

public void setOnToggleListener(final OnToggleListener listener) { 
    this.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      toggle = !toggle; 
      listener.onToggle(toggle); 
     } 
    }); 
} 

Và trên đối tượng điều khiển của tôi:

public class MyController { 

    private Context context; 

    public MyController(Context context) { 
     this.context = context; 
    } 

    public void toggleStrokeLimitation(boolean switchPosition) { 
     Toast.makeText(context, "Toggle" + switchPosition, Toast.LENGTH_SHORT).show(); 
    } 
} 

Yeah !! nó làm việc

Hoặc bạn có thể sử dụng xml như:

<com.androidbolts.databindingsample.model.SuperCustomView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:onToggleListener="@{controller.toggleStrokeLimitation}" /> 

Bây giờ không cần phải thêm @BindingMethods chú thích.

Documentation nói: "Some attributes have setters that don't match by name. For these methods, an attribute may be associated with the setter through BindingMethods annotation. This must be associated with a class and contains BindingMethod annotations, one for each renamed method. "

+0

Chính xác những gì tôi đang tìm :) – Shirane85

0

Bạn có thể không cầnBindingMethods cho nghe thính giả vào xem tùy chỉnh nếu bạn xác định tên phương pháp theo định dạng Java bean ĐÚNG. Dưới đây là một ví dụ

lớp CustomView

public class CustomView extends LinearLayout { 
    ... 
    private OnCustomViewListener onCustomViewListener; 

    ... 
    public void setOnCustomViewListener(OnCustomViewListener onCustomViewListener) { 
     this.onCustomViewListener = onCustomViewListener; 
    } 


    .... 
    public interface OnCustomViewListener { 
     void onCustomViewListenerMethod(int aNumber); 
    } 
} 

XML

<...CustomView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:onCustomViewListener="@{viewModel.viewModelListenerMethod}" // or use can use app:onCustomViewListener="@{viewModel::viewModelListenerMethod}" 
    /> 

ViewModel

public class ViewModel extends BaseObservable{ 

    public void viewModelListenerMethod(int aNumber){ 
     // handle listener here 
    } 
} 
Các vấn đề liên quan