2017-11-01 3 views
0

내 앱에서 이상한 문제가 있습니다. 단추의 onClick 이벤트를 메서드에 바인딩 할 수 없습니다. 여기에 내 코드입니다 :버튼을 바인딩 할 수 없습니다. onClick

public class BookingViewModel extends BaseObservable { 
    public void onAddInvite(View view) { 
     invitationList.add(new Person(name, number, email)); 
     notifyPropertyChanged(BR.invitationsText); 
    } 
} 

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> 

     <variable 
      name="bookingViewModel" 
      type="it.jgrassi.roombooking.viewmodel.BookingViewModel" /> 
    </data> 
    <Button 
      android:id="@+id/button_book" 
      android:layout_width="89dp" 
      android:layout_height="40dp" 
      android:layout_marginBottom="8dp" 
      android:layout_marginEnd="8dp" 
      android:layout_marginStart="8dp" 
      android:text="Book" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintEnd_toEndOf="parent" 
      app:layout_constraintStart_toStartOf="parent" 
      android:enabled="@{bookingViewModel.bookingEnabled}" 
      android:onClick="@{bookingViewModel::onAddInvite}"/> 
</layout> 

I 찾으 이상한 같은 응용 프로그램에서 나는 목록 항목에 온 클릭을 처리하는 비슷한 상황이 있기 때문에 :

public class ItemRoomViewModel extends BaseObservable{ 
public void onItemClick(View view) { 
     //do stuff 
    } 
} 

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> 

     <variable 
      name="roomViewModel" 
      type="it.jgrassi.roombooking.viewmodel.ItemRoomViewModel" /> 
    </data> 

    <android.support.constraint.ConstraintLayout 
     android:id="@+id/item_room" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?android:attr/selectableItemBackground" 
     android:clickable="true" 
     android:onClick="@{roomViewModel::onItemClick}" 
     android:paddingBottom="8dp" 
     android:paddingTop="8dp"> 
</layout> 

기타 바인딩은 완벽하게 작동합니다. 또한 안드로이드 스튜디오는 사용중인 것처럼 보이는 메소드를 만듭니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

2

작동 케이스에서와 마찬가지로 버튼에 android:clickable="true"을 설정하십시오. 대신 클릭 리스너를 사용하여 click 이벤트를 포착 할 수 있습니다. 또한 Butter Knife (자세한 내용을 보려면 클릭) 필드 및 메서드 바인딩을 사용할 수 있습니다 (그게 지금 사용하고 있습니다).

+0

감사합니다 :) 버튼이 필요하다는 것을 기억하지 못했습니다. –

+0

도움이 되었기 때문에 기쁩니다! – gbruscatto

관련 문제