所以显然我被引导出了错误的轨道,因为我应该寻找突出显示“选中”的项目而不是“选定”的项目。因此,许多答案告诉我使用selector
与我的 ListView 布局一起使用android:listSelector="@drawable/myselector
,但我真正需要的是使用selector
与我的行布局。其实解决办法很简单,我将其发布在下面:
可绘制/rowbackgroundselector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true"
android:drawable="@android:color/holo_green_light"/>
</selector>
- 请注意如何使用“state_activated”来检测某个项目是否被“选中”...
可绘制/mylistrow.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:background="@drawable/rowbackgroundselector"
android:padding="10sp"
/>
MainActivity.onListItemClick()
public void onListItemClick(ListView l, View v, int position, long id) {
getListView().setItemChecked(pos, true);
}
最后,确保您的适配器使用自定义行布局
mAdapter = new ArrayAdapter<FileTag>(this.getActivity(),
R.layout.mylistrow, mList);