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"

相关内容

热门资讯

WPK下载!红龙PoKer软件... WPK下载!红龙PoKer软件辅助ai,德普之星私人局辅助免费,切实真的是有ai辅助举措德普之星私人...
WPK透视版!德州扑克攻略开挂... WPK透视版!德州扑克攻略开挂ai,aapoker脚本怎么用,本来是真的有ai开挂教程书进入游戏-大...
WPK辅助!AApoker插件... WPK辅助!AApoker插件透视挂ai,wpk有作弊吗,好像真的有ai透视挂绝活儿1、完成wpk有...
WPK俱乐部!微扑克脚本透视a... WPK俱乐部!微扑克脚本透视ai,拱趴大菠萝万能辅助器,总是存在有ai透视方式1、任何拱趴大菠萝万能...
WPK链接!红龙PoKer脚本... WPK链接!红龙PoKer脚本开挂ai,wpk辅助器,真是是真的有ai开挂方式wpk辅助器辅助器是一...
WPK脚本!WePoKer攻略... WPK脚本!WePoKer攻略透视挂ai,智星德州辅助译码插件靠谱吗,真是是真的有ai透视挂技法1、...
WPK透视版!AApoker攻... WPK透视版!AApoker攻略辅助器ai,AApoker中牌率,果然有ai辅助器讲义1、首先打开A...
WPK四人!智星菠萝德州技巧开... WPK四人!智星菠萝德州技巧开挂ai,aapoker ai插件,好像真的是有ai开挂绝活1、这是跨平...
WPK安卓!wpk软件透视挂a... WPK安卓!wpk软件透视挂ai,wpk系统是否存在作弊行为,确实是有ai透视挂指南1、该软件可以轻...
WPK工具!aapoker脚本... WPK工具!aapoker脚本透视挂ai,拱趴大菠萝十三水作弊,切实存在有ai透视挂窍要1)拱趴大菠...