【发布时间】:2015-01-26 18:32:53
【问题描述】:
我正在尝试使用selector 突出显示我的ListView 中的选定行。在我发布新闻后,我需要它保持突出显示。符号化突出显示的行是选定的行。
目前文本默认更改颜色。我需要更改背景颜色。
我在DrawerLayout 内部的片段中有ListView。当我选择一个设备时,DrawerLayout 关闭。当我再次打开DrawerLayout 时,我选择的设备的背景没有改变。所选设备的文本是不同的颜色,但不是背景。
我在这里尝试了很多关于 SO 的建议,但没有发现任何可行的方法。我的目标是 API-17,minsdk 是 API-17。
这是我尝试过的一些帖子,但没有成功。
- ListView Item Selected State not working
- Change background color of selected item on a ListView
- Android ListView selected item stay highlighted
如果您需要更多详细信息,请告诉我,谢谢。
这是我的代码(我尝试的最后一件事):
device_list_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/device_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:background="@drawable/device_selector" >
</ListView>
device_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_activated="true"
android:drawable="@drawable/device_selected"/>
<item
android:drawable="@drawable/device_not_selected"/>
</selector>
strings.xml
<drawable name="device_selected">#777777</drawable>
<drawable name="device_not_selected">#555555</drawable>
DeviceListFragment
public class DeviceListFragment extends Fragment implements OnItemClickListener {
public View onCreateView(...) {
View view = inflater.inflate(R.layout.device_list_fragment, container, false);
this.devicesArrayAdapter = new DevicesArrayAdapter(this.getActivity());
this.deviceListView = (ListView) view.findViewById(R.id.device_list_view);
this.deviceListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
this.deviceListView.setAdapter(this.devicesArrayAdapter);
this.deviceListView.setOnItemClickListener(this);
return view;
}
public void onItemClick(...) {
this.deviceListView.setItemChecked(position, true);
}
}
DevicesArrayAdapter
public class DevicesArrayAdapter extends ArrayAdapter<Device> {
public View getView(...) {
View rowView = layoutInflater.inflate(R.layout.device_element, parent, false);
return rowView;
}
}
【问题讨论】:
标签: android android-listview android-selector