I have posted the same problem a couple of times but it hasn't yet been resolved. I have a ListFragment
and I want to highlight the selected item in the list. I have been given suggestions to use a "selector". I don't understand how to use this selector. My ListFragment
class is:
// Create an adapter with list of stores and populate the list with
// values
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, StoreList);
setListAdapter(adapter);
mDbHelper.close();
}
/*
* (non-Javadoc)
*
* Handles the event when an item is clicked on left pane, performs action
* based on the selection in left pane
*
* @see android.app.ListFragment#onListItemClick(android.widget.ListView,
* android.view.View, int, long)
*/
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String selectedStore = (String) getListAdapter().getItem(position);
DetailFragment fragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
v.setBackgroundColor(getResources().getColor(R.color.BLUE));
// passes selectedStore to detail fragment
fragment.setText(selectedStore);
// getItemList(selectedStore);
}
Using setBackground sets the color permanently, but I want it to go away when another item is selected.
I understand how to use a selector in a ListView
but in my case if I haven't defined any xml for the Listview
, then how would I use a "selector"? I am using android.R.layout.simple_list_item_1
which is predefined.
ListView
is for use with D-pads, trackballs, arrow keys, and other pointing devices. On tablets, there is a related concept of an "activated" row, designed to highlight the last-tapped-upon item from the touchscreen, to provide context for something adjacent to it (e.g., master-detail pattern). – Scouring