2017-04-21 7 views
0

저는 recyclerview를 가지고 있으며, 데이터 바인딩을 사용하여 recyclerview의 각 항목의 스타일을 동적으로 변경하고 싶습니다.데이터 바인딩을 사용하여 Recyclerview의 아이템 스타일을 변경하십시오.

이 내가 레이아웃 등 어디해야 데이터 바인딩의 텍스트 뷰 배경의 텍스트 색상처럼,이 레이아웃에 각 항목의 스타일을 변경하려면 내 Recyclerview 항목 레이아웃

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/channel_priority_item_height" 
     android:background="@drawable/channel_item_bg" 
     android:paddingStart="@dimen/channel_priority_header_left_pad" 
     android:paddingEnd="@dimen/channel_priority_item_right_pad"> 
     <ImageView 
      android:id="@+id/preference_channel_image" 
      android:src="@drawable/empty_channel_drawable" 
      android:layout_width="@dimen/channels_item_width" 
      android:layout_height="@dimen/channels_item_height" 
      android:layout_centerVertical="true" 
      android:layout_alignParentStart="true" 
      android:layout_marginEnd="@dimen/channel_item_margin_end"/> 
     <RelativeLayout 
      android:id="@+id/preference_channel_switch_holder" 
      android:layout_width="48dp" 
      android:layout_height="48dp" 
      android:layout_centerVertical="true" 
      android:layout_alignParentEnd="true"> 
      <ImageView 
       android:id="@+id/preference_channel_switch" 
       android:layout_width="@dimen/channels_preference_switch_size" 
       android:layout_height="@dimen/channels_preference_switch_size" 
       android:layout_centerInParent="true" 
       android:padding="@dimen/switch_padding" 
       android:src="@drawable/switch_selected_bg" 
       android:background="@drawable/circle_button_bg" /> 
     </RelativeLayout> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_toEndOf="@+id/preference_channel_image" 
      android:layout_toStartOf="@+id/preference_channel_switch_holder" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true"> 
      <TextView 
       android:text="Some Header" 
       android:id="@+id/channel_name" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textSize="@dimen/preference_channel_primary" 
       android:textColor="@color/white" 
       android:fontFamily="sans-serif"/> 
      <TextView 
       android:text="Some Description" 
       android:id="@+id/channel_description" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textSize="@dimen/preference_channel_secondary" 
       android:textColor="@color/white_50" 
       android:fontFamily="sans-serif"/> 
     </LinearLayout> 
    </RelativeLayout> 

입니다 끝내라? 어댑터 또는 상위 단편에서 모든 예제는 매우 유용합니다. 감사합니다.

답변

1

모델 클래스 :

public class ColorObject extends BaseObservable { 

    int color = Color.BLACK; 

    public void setColor(int color) { 
     if (this.color==color) return; 
     this.color = color; 
     notifyPropertyChanged(BR.color); 
    } 

    @Bindable 
    public int getColor() { 
     return color; 
    } 
} 

그러나, 나는 단지 색상 모델 클래스를 만들 것을 권장하지 않습니다. 이 Color 개체를 활동보기 모델 클래스에 추가 할 수 있습니다.

어댑터 :

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> { 

    private ColorObject colorObject; 

    public ListAdapter(Context context){ 
     colorObject = new ColorObject(); 

    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     ItemListBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_list, parent, false); 
     return new ViewHolder(binding); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     holder.bind(colorObject); 
    } 

    @Override 
    public int getItemCount() { 
     return 5; 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder { 
     public ItemListBinding itemListBinding; 
     public ViewHolder(ItemListBinding itemBinding) { 
      super(itemBinding.getRoot()); 
      itemListBinding = itemBinding; 
     } 

     public void bind(ColorObject item) { 
      itemListBinding.setTextColor(item); 
      itemListBinding.executePendingBindings(); 
     } 
    } 
} 

item_list.xml :

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

    <data> 
     <variable 
      name="textColor" 
      type="com.example.ColorObject"/> 
    </data> 

    <android.support.constraint.ConstraintLayout xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/root" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:padding="@dimen/activity_horizontal_margin" 
      android:id="@+id/text" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:textColor="@{textColor.color}" 
      android:background="@color/colorPrimaryDark" 
      android:text="sjhfjsgjfhgjfhs" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toTopOf="parent" /> 

    </android.support.constraint.ConstraintLayout> 
</layout> 
관련 문제