AndroidrecyclerView:如何保留selectableItemBackground以保持行高亮?
创始人
2024-10-09 18:01:42
0

要在RecyclerView的每一行上保留selectableItemBackground,需要在RecyclerView.Adapter的ViewHolder类中设置一个selector作为绑定的背景。

在ViewHolder中,您可以使用以下代码来设置selector:

public class MyViewHolder extends RecyclerView.ViewHolder { private final Context context; private final TextView myTextView;

public MyViewHolder(Context context, View view) {
    super(view);
    this.context = context;
    myTextView = (TextView) view.findViewById(R.id.myTextView);

    int[] attrs = new int[] { android.R.attr.selectableItemBackground /* index 0 */};
    TypedArray ta = context.obtainStyledAttributes(attrs);
    Drawable drawableFromTheme = ta.getDrawable(0 /* index */);
    myTextView.setBackground(drawableFromTheme);
    ta.recycle();
}

}

在上面的代码中,我们首先获取TypedArray,它包含在Android中默认的selectableItemBackground属性的背景drawable。然后,我们从typedArray中获取drawable并将其设置为row的背景,这样就可以保留高亮效果。

注意:要使此方法起作用,需要在XML布局文件中正确设置每个行的根布局属性:

android:clickable="true" android:focusable="true"

如果您的行布局不是根布局,则需要在布局中包含属性:

android:background="?android:attr/selectableItemBackground"

相关内容

热门资讯

免费测试版!aapoker透视... 此外,数据分析德州(约局吧德州真的有透视挂吗)辅助神器app还具备辅助透视行为开挂功能,通过对客户约...
必知教程!wepoker破解器... 1、必知教程!wepoker破解器,hhpoker必备开挂,玩家教程(有挂透明)(UU poker、...
玩家必知教程!wepoker透... 玩家必知教程!wepoker透视版下载,hardrock作弊,介绍教程(有挂技巧);致您一封信;亲爱...
攻略讲解!红龙poker辅助工... 攻略讲解!红龙poker辅助工具,pokernow辅助工具,黑科技教程(有挂技巧);玩家必备必赢加哟...
推荐几款新版!aa poker... 推荐几款新版!aa poker辅助,wepoker私人定制透视,解密教程(有挂技巧)关于wepoke...
一分钟秒懂!wepoker辅助... 一分钟秒懂!wepoker辅助是真的假的,wepoker透视脚本免费下载,力荐教程(有挂神器)关于w...
必看攻略!wepoker有透视... 必看攻略!wepoker有透视吗,竞技联盟透视,爆料教程(有挂教程)1、不需要AI权限,帮助你快速的...
盘点一款!wepoker私人局... 1、盘点一款!wepoker私人局辅助挂,agpoker辅助,解密教程(有挂教程);详细教程。2、w...
实测揭晓!线上德州的辅助器是什... 实测揭晓!线上德州的辅助器是什么,wepoker辅助器软件下载,高科技教程(有挂神器)1、不需要AI...
教学盘点!hhpoker有没有... 教学盘点!hhpoker有没有作弊挂,agpoker辅助,规律教程(有挂软件)关于hhpoker有没...