我想选择一行ListView。我已经尝试了一切,但似乎没有任何效果。
- 我在
onCreate方法中 - 我填充我的
ListView - 我
setSelection通过position - 我设置了 setSelected(true)
该方法setSelection(index)有效,但我无法选择该项目。怎么做?我也尝试过使用requestFocus(),但它不起作用。
我想选择一行ListView。我已经尝试了一切,但似乎没有任何效果。
onCreate方法中ListViewsetSelection通过position该方法setSelection(index)有效,但我无法选择该项目。怎么做?我也尝试过使用requestFocus(),但它不起作用。
您ListView需要一个带有可绘制对象的选择器。否则,项目在选择时不会改变颜色。StateListDrawablestate_selected
您需要为此创建选择器项,您必须使用 setSelector() 调用将此项目设置为您的列表视图。
创建一个新的可绘制文件并使用类似的代码,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"><shape>
<gradient android:angle="270" android:endColor="#BB00D5FF" android:startColor="#BB00D5FF" />
<corners android:bottomLeftRadius="9dp" android:topRightRadius="9dp" />
</shape></item>
<item android:state_pressed="true"><shape>
<gradient android:angle="270" android:endColor="#BB0987CD" android:startColor="#BB0987CD" />
<corners android:bottomLeftRadius="9dp" android:topRightRadius="9dp" />
</shape></item>
</selector>
并使用 setSelector(R.drawable.bg) 将上述可绘制对象设置为列表视图