MvxRecycleView 选中项背景

MvxRecycleView selected item background

如何在 MvxRecycleView 中设置选中的项目背景?

我想更改所选项目的背景。

selector_category_item.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_pressed="true"  android:state_enabled="true"  >
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary"/>
        </shape>
            </item>

    <item android:state_checked="false" android:state_enabled="true"  >
        <shape android:shape="rectangle">
            <solid android:color="@color/grayBackground"/>
        </shape>
    </item>
</selector>

我的 MvxRecycleView

<MvxRecyclerView
            android:overScrollMode="never"
            android:scrollbars="vertical"
            android:background="#e2e2e2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            local:MvxItemTemplate="@layout/category_item"
            local:MvxBind="ItemsSource Categories; ItemClick SelectCategoryCommand; SelectedItem SelectedCategory"
            android:id="@+id/categoryRecyclerView" />

和category_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:clickable="true"
    android:background="@drawable/selector_category_item"
    android:layout_marginBottom="1dp"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ffimageloading.cross.MvxCachedImageView
            android:id="@+id/categoryImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            local:MvxBind="DrawableName CategoryImage" />
        <ImageView
            android:id="@+id/favIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentEnd="true"
            android:src="@drawable/no_fav_icon" />
    </RelativeLayout>
    <TextView
        android:clickable="true"
    android:focusableInTouchMode="true"
    android:focusable="true"
        android:layout_marginTop="8dp"
        android:id="@+id/categoryText"
        style="@style/TextStyleBlack"
        android:fontFamily="@font/roboto_regular"
        android:letterSpacing="-0.04"
        android:lineSpacingExtra="0sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        local:MvxBind="Text Name"
        android:textSize="14sp" />
</LinearLayout>

我需要在选中项目时将所选项目的背景更改为其他背景。 我该如何解决?

  1. 制作布尔标志以保存 Category model 上每个类别项目的选定状态。
  2. 在您的适配器上 viewHolder 为回收器项目根视图创建实例 mBackground
  3. onBindViewHolder 更新 mBackground 选择状态。

    class MyDataHolder extends RecyclerView.ViewHolder
        implements View
        .OnClickListener {
    LinearLayout mBackground;
    MyDataHolder(View itemView) {
        super(itemView);
        mBackground = (LinearLayout) itemView.findViewById(R.id.my_item_root_view);
        itemView.setOnClickListener(this);
    }
    
    
    
    @Override
    public void onBindViewHolder(MyCategoryAdapter.MyDataHolder holder, int position) {
        final Category category = getItem(position);
        holder.mBackground.setSelected(category.isSelected());
    }
    

您可以制作自定义 MvxRecyclerView 适配器来处理选择:

public class MyRecyclerAdapter : MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerAdapter
{
    public MyRecyclerAdapter(IMvxAndroidBindingContext bindingContext) : base(bindingContext)
    {
    }

    public MyRecyclerAdapter(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        base.OnBindViewHolder(holder, position);
        holder.ItemView.Selected = _selectedPosition == position;
        holder.ItemView.Click += (s, e) => SelectIndex(holder.AdapterPosition);
    }

    private void SelectIndex(int index)
    {
        NotifyItemChanged(_selectedPosition);
        _selectedPosition = index;
        NotifyItemChanged(_selectedPosition);
    }

    private int _selectedPosition = RecyclerView.NoPosition;
}

然后您想在视图中的某个位置设置 MvxRecyclerView.Adapter 属性。理想的位置通常是 OnCreate 用于 activity,或 OnCreateView 用于片段:

recyclerView.Adapter = new MyRecyclerAdapter((IMvxAndroidBindingContext)BindingContext);

此时你有一个 RecyclerView,它将你点击的项目设置为选中。现在您可以使用 ColorStateList 根据选择状态更改颜色。将文件添加到 Resources\drawable\selector_category_item.xml:

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item 
    android:drawable="@color/colorAccent"
    android:state_pressed="true" />
  <item 
    android:drawable="@color/colorPrimary"
    android:state_selected="true" />
</selector>

最后将 category_item.xml LinearLayout 的背景设置为:

android:background="@drawable/selector_category_item"

值得指出的是,您还可以根据选择状态使用不同的 ColorStateList 来根据需要更改 android:textColor。您只需要将这些 ColorStateList XML 文件放在 Resources\color 文件夹中,而不是 Resources\drawable.

我在 GitHub 上发布了一个工作示例。