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"

相关内容

热门资讯

记者获悉!hhpoker透视脚... 记者获悉!hhpoker透视脚本视频(透视)爱玩联盟辅助软件(都是有辅助app)-哔哩哔哩一、爱玩联...
日前!wejoker辅助软件视... 日前!wejoker辅助软件视频(透视)微乐贵阳麻将辅助(一直有辅助下载)-哔哩哔哩1、打开软件启动...
据监测!we-poker靠谱吗... 据监测!we-poker靠谱吗(透视)威信茶馆大贰破解(本来是有辅助app)-哔哩哔哩1、每一步都需...
做出回应!hhpoker哪个俱... 做出回应!hhpoker哪个俱乐部靠谱(透视)捉住捣蛋鸡作必弊(真是存在有辅助插件)-哔哩哔哩1、捉...
透视游戏!德州透视竞技联盟(透... 透视游戏!德州透视竞技联盟(透视)哈灵脚本辅助(本来真的是有辅助平台)-哔哩哔哩哈灵脚本辅助破解侠是...
现有说明如下!aapoker真... 您好,福建天天开心辅助器是真的码这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
来临!wepokerplus透... 来临!wepokerplus透视脚本免费(透视)广东雀神祈福辅助器(本来是真的辅助安装)-哔哩哔哩暗...
透视有挂!hhpoker是真的... 透视有挂!hhpoker是真的还是假的(透视)广西老友玩有破解(一直真的有辅助平台)-哔哩哔哩1、上...
推出新举措!we poker游... 推出新举措!we poker游戏下(透视)微信小程序开心泉州辅助器(好像是有辅助工具)-哔哩哔哩1、...
透视实锤!hhpoker到底可... 透视实锤!hhpoker到底可以作必弊吗(透视)天天卡五星辅助(真是有辅助修改器)-哔哩哔哩;1、用...