Android DataBinding - BindingAdapter

less than 1 minute read

BindingAdapter

  • BindingAdapters Class 생성

    예를 들어 RecyclerView의 Adapter를 BindingAdapter를 통해 xml파일에서 적용 가능

    • Java
      public class BindingAdapters {
          @BindingAdapter("bind:setAdapter")
          public static void setAdapter(RecyclerView recyclerView, ListRecyclerViewAdapter adapter) {
              recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
              recyclerView.setHasFixedSize(true);
              recyclerView.setAdapter(adapter);
          }
      }
    
    • Kotlin
      class BindingAdapter {
          companion object {
              @JvmStatic
              @BindingAdapter("bind:setAdapter")
              fun setAdapter(
                  recyclerView: RecyclerView,
                  adapter: ListRecyclerViewAdapter
              ) {
                  recyclerView.layoutManager = LinearLayoutManager(recyclerView.context)
                  recyclerView.setHasFixedSize(true)
                  recyclerView.adapter = adapter
              }
          }
      }
    
    • xml
      <android.support.v7.widget.RecyclerView
          android:id="@+id/recyclerView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          bind:setAdapter="@{viewModel.adapter}"
      />
    

Categories:

Updated:

Leave a comment