2016-06-15 1 views
3

Android 개발자 Binding Events의 예를 따라 단계별로 구현했습니다. 그 일 벌금. 하지만 어댑터에서 처리기로 매개 변수를 보내고 싶습니다. 어떻게 데이터 바인딩 처리기를 사용하여이 작업을 수행 할 수 있습니까?android 데이터 바인딩 라이브러리 사용하기 바인딩 이벤트에 매개 변수를 전달하는 방법

+1

지금까지 해보신 것은 무엇입니까? 코드를 게시하십시오. 어떤 종류의 매개 변수를 전달하려고합니까? –

답변

5

답변을 얻었습니다.

layout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<data> 

    <variable 
     name="movie" 
     type="embitel.com.databindingexample.helper.Movie" /> 

    <variable 
     name="handler" 
     type="embitel.com.databindingexample.helper.MyHandlers" /> 

</data> 

<android.support.v7.widget.CardView 
    android:id="@+id/cardview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginTop="4dp" 
android:onClick="@{(view)->handler.onItemClicked(view,movie)}" 
app:cardBackgroundColor="@android:color/white" 
    app:cardCornerRadius="4dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="8dp"> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="128dp" 
      android:scaleType="centerCrop" 
      app:error="@{@drawable/ic_launcher}" 
      app:imageUrl="@{movie.imageUrl}" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@{movie.title}" /> 

    </LinearLayout> 

</android.support.v7.widget.CardView> 

다음과 같은 핸들러 클래스를 생성 onclick을 사용 람다 식 용 XML의 ,

public class MyHandlers { 

public void onItemClicked(View v, Movie movie) { 
    Context context = v.getContext(); 
    context.startActivity(DetailActivity.buildIntent(context, movie)); 
} 

}

,

는 해당 XML은 다음과 같이 iflated되는 핸들러, 당신은 또한 모든 클래스에서 핸들러 메소드 넣을 수 있습니다

binding.setHandler(new MyHandlers()); 

를 설정해야합니다. 이 경우 해당 클래스 이름을 핸들러로 설정해야합니다.

+0

람다를 사용하지 않고 어떻게 할 수 있습니까? –