Recyclerview 点击效果,可以使用

第一步: 新建drawble recycler_touch_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/white_fa" android:state_pressed="true"/>
    <item android:drawable="@color/white_fa" android:state_focused="true"/>
    <item android:drawable="@color/white"/>
</selector>
#FAFAFA   #FFFFFF

第二步:新建drawble-21 recycler_touch_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:color="@color/gray_d7"
    tools:targetApi="lollipop">
    <item android:drawable="@color/white" />
</ripple>
#D7D7D7 

第三步: 设置你想要的控件作为背景

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="@drawable/recycler_touch_bg"
   >
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="您好,我是一条标题"
        android:textColor="@color/yellow_e8"
        android:textSize="@dimen/sp_21" />

</LinearLayout>

这一步有个重要的地方,如果想要在五大布局中设置点击效果, android:clickable=“true”,记得加上要不然会点击无效。

就这么简单,对了,如果有好用的圆形点击效果,请评论一下,我会联系你,感谢

RecyclerView 点击折叠效果的实现可以通过自定义 RecyclerView.Adapter 实现。 1. 在 RecyclerView.Adapter 中定义一个 boolean 类型的变量用于记录当前 item 的展开状态。 2. 在 onBindViewHolder 方法中判断当前 item 是否展开,根据展开状态设置 item 的高度。如果当前 item 是展开状态,则高度为全部内容的高度;否则,高度为折叠后的高度。 3. 在 item 的点击事件中,改变展开状态并调用 notifyDataSetChanged() 方法刷新数据。 以下是示例代码: ```java public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private boolean isExpanded = false; // 记录当前 item 是否展开 @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // 创建 ViewHolder View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { // 绑定数据 if (isExpanded) { // 如果当前 item 是展开状态,则设置 item 的高度为全部内容的高度 holder.itemView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; } else { // 否则,高度为折叠后的高度 holder.itemView.getLayoutParams().height = 200; } } @Override public int getItemCount() { // 返回数据集大小 return 10; } class MyViewHolder extends RecyclerView.ViewHolder { public MyViewHolder(View itemView) { super(itemView); // 设置 item 的点击事件 itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 改变展开状态 isExpanded = !isExpanded; // 刷新数据 notifyDataSetChanged(); } }); } } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值