繁体   English   中英

设置列表项已选择

[英]Set list item selected

我已经尝试了很多示例,但是结果是相同的,我的要求是单击列表项时,然后我要显示该项由color.selected。并且必须显示该项,直到单击其他列表项。

尝试了很多例子,但结果是-当我单击列表项时,其颜色变化,同时在释放其颜色变化为默认颜色后按下它。

我想实现类似此处所示的功能,但是此代码现在也对我有用。

因此,我如何实现这种类型的视图。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            for(int i=0;i<parent.getChildCount();i++)
                {
                    parent.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
                }
            v.setBackgroundColor(Color.BLUE);
            }
        });

在您想要的可绘制文件夹名称中创建一个新的xml。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
    android:state_pressed="true"
    android:drawable="@color/white" />
<item
    android:state_selected="true"
    android:drawable="@drawable/list_item_bg_selected" />
<item 
    android:drawable="@color/list_bg" />
</selector>

并将其设置为列表视图的背景。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM