Android recyclerView/listview的点击变色、点击换背景颜色(完整版)(最后附demo)

本文介绍了如何在Android中为RecyclerView和ListView实现点击变色及更换背景颜色的效果,提供了activity_main.xml、recview_item.xml的布局示例,以及RecViewAdapter和MainActivity的代码示例,附带了完整的Demo。

**

一言不合就上图

**
这里写图片描述

我的AS版本是:3.1.2
android版本是:19——27

首先依賴(僅供參考):

    //recyclerview
    implementation 'com.android.support:recyclerview-v7:27.1.1'

然後項目結構:
这里写图片描述

首先:activity_main.xml的佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="下面是recycler View點擊變色demo" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10px"
        android:orientation="horizontal"
        android:padding="20px">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recView"
            android:layout_width="300px"
            android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

        <TextView
            android:id="@+id/right_titel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="30px"
            android:background="@color/colorAccent"
            android:gravity="center"
            android:text="點擊左側 獲取值"
            android:textSize="33sp" />
    </LinearLayout>

</LinearLayout>

recview_item.xml的佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值