I also do a similar thing: select the selected item in the list of items (change it to red) and set the text color inside the item to white.
I can come up with a β simple but not effective β way: save the selected position of the position in the user adapter and change it in the implementation of the ListView OnItemClickListener :
// The OnItemClickListener implementation @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mListViewAdapter.setSelectedItem(position); }
Then update the selected background and text color in the getView() method.
// The custom Adapter @Override public View getView(int position, View convertView, ViewGroup parent) { ... if (position == mSelectedPosition) { // customize the selected item background and sub views convertView.setBackgroundColor(YOUR_HIGHLIGHT_COLOR); textView.setTextColor(TEXT_COLOR); } else { ... } }
After searching for a while, I found that many people mentioned setting android:listSelector="YOUR_SELECTOR" . After some time, I found the easiest way to select the selected ListView item, which can be done with only two lines set to the ListView layout resource:
android:choiceMode="singleChoice" android:listSelector="YOUR_COLOR"
There is another way to make it work, for example, customizing the activatedBackgroundIndicator theme. But I think it will be a much more general solution, as it will affect the whole topic.
Ethan Zhong Dec 15 '15 at 6:51 2015-12-15 06:51
source share