如何为列表项制作特定的背景?

How to make specific background for list item?

如何让列表项在点击时有这样的蓝色背景?

这是我单击项目时来自片段的代码:

override fun onListItemClick(itemIndex: Int, itemCode: String, itemViewId: Int) {
        val connectionGuid = (adapter.getItem(itemIndex) as ConnectionViewModel).guid
        proceedView.isEnabled = true
        proceedView.setOnClickListener { proceedConnection(connectionGuid) }
    }

这是我的适配器代码:

class ConnectionItemHolder(parent: ViewGroup, private val listener: ListItemClickListener?) :
    RecyclerView.ViewHolder(parent.inflateListItemView(R.layout.view_item_connection)) {

    private val logoImageView = itemView.findViewById<ImageView>(R.id.logoImageView)
    private val titleView = itemView.findViewById<TextView>(R.id.titleView)
    private val subTitleView = itemView.findViewById<TextView>(R.id.subTitleView)

    init {
        itemView.setOnClickListener {
            if (adapterPosition > RecyclerView.NO_POSITION)
                listener?.onListItemClick(itemIndex = adapterPosition)
        }
    }

    fun bind(item: ConnectionViewModel) {
        logoImageView.loadRoundedImage(
            imageUrl = item.logoUrl,
            placeholderId = R.drawable.ic_logo_bank_placeholder,
            cornerRadius = itemView.resources.getDimension(R.dimen.dp_12)
        )
        titleView.text = item.name
        subTitleView.text = item.statusDescription
        subTitleView.setTextColorResId(item.statusColorResId)
    }
}

这是我的布局 view_item_connection:

<androidx.cardview.widget.CardView 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"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/dp_80"
    android:layout_marginStart="@dimen/dp_16"
    android:layout_marginEnd="@dimen/dp_16"
    android:clipToPadding="false"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardCornerRadius="@dimen/dp_6"
    app:cardElevation="@dimen/dp_20">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/app_logo_background">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginEnd="@dimen/dp_15"
            android:layout_toEndOf="@+id/logoImageView"
            android:orientation="vertical">

            <TextView
                android:id="@+id/titleView"
                style="@style/Text18Primary"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="bottom"
                android:maxLines="1"
                tools:text="Demobank" />

            <TextView
                android:id="@+id/subTitleView"
                style="@style/Text14Secondary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_3"
                android:maxLines="1"
                tools:text="Connected on 12 sept. 2019, 18:02" />
        </LinearLayout>

        <ImageView
            android:id="@+id/logoImageView"
            style="@style/ConnectionLogoImage"
            android:layout_centerVertical="true"
            android:contentDescription="@null" />
    </RelativeLayout>
</androidx.cardview.widget.CardView>

像这样在 drawable 文件夹中创建一个 xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/white" />
    <stroke
        android:width="1dp"
        android:color="@color/your_line_color" />
    <corners
        android:radius="10dp"
        />
</shape>

用法

bindview()中这样使用

viewHolder.rootLayout.setBackgroundResource(R.drawable.stroke_background)

首先收集当前选中的项目位置:

 OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
                mSelectedItem = position;
                mAdapter.notifyDataSetChanged();
        }
    };

第二次覆盖适配器的 getView 方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View view = View.inflate(context, R.layout.item_list, null);

    if (position == mSelectedItem) {
        // set your color here
    }

    return view;
}

如果你使用来自GoogleMaterial的MaterialCardView组件,你可以这样做:

<com.google.android.material.card.MaterialCardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:strokeColor="@color/card_stroke_color"
    app:strokeWidth="2dp"
    />

color/card_stroke_color.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="?colorPrimary" android:alpha="0.5"/>
    <item android:color="@android:color/transparent" />
</selector>

绑定 ViewHolder 时:

cardView.isChecked = item.checked