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"

相关内容

热门资讯

科普!wepoker辅助器安装... 1、科普!wepoker辅助器安装包定制,hhpoker德州作弊,2025新版教程(有挂透视)(UU...
总算了解!aapoker脚本,... 总算了解!aapoker脚本,aapoker怎么提高中牌率,切实教程(有挂神器);支持2-10人实时...
揭秘攻略!德普之星透视,hhp... 揭秘攻略!德普之星透视,hhpoker有没有作弊辅助,黑科技教程(有挂技巧);1分钟了解详细教程(微...
玩家必备科技!werplan怎... 玩家必备科技!werplan怎么透视,hhpoker脚本下载,玩家教程(有挂神器);原来确实真的有挂...
研究成果!德州来玩辅助器,hh... 研究成果!德州来玩辅助器,hhpkoer辅助器,透明教程(有挂透视);科技安装教程;13670430...
大家学习交流!werplan怎... 大家学习交流!werplan怎么透视,hhpoker脚本下载,2025版教程(有挂神器)科技教程也叫...
2024教程!we-poker... 1、2024教程!we-poker靠谱吗,pokemomo辅助工具,玩家教程(有挂攻略)2、进入游戏...
记者揭秘!aapoker怎么开... 记者揭秘!aapoker怎么开辅助器,竞技联盟透视,微扑克教程(有挂辅助)准备好在aapoker怎么...
总算清楚!德扑圈有透视吗,hh... 1、总算清楚!德扑圈有透视吗,hhpoker的辅助是真的吗,教你攻略(有挂攻略)2、进入游戏-大厅左...
技巧知识分享!aapoker ... 技巧知识分享!aapoker ai插件,wepoker如何设置透视,曝光教程(有挂透明),支持语音通...