Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have had a similar issue, and solved it via code. It might not be the brightest nor the most efficient solution, but works around ListView drawing issues and so far seems to be working well.</p> <p>The activity file:</p> <pre><code>private ListView listView; private View selectedView; private int selectedPosition; private MyAdapter arrayAdapter; ... // set a state machine for when a list item is clicked listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { if (selectedPosition == -1) { // no item is selected, mark as selected selectedView = view; view.setBackgroundResource(R.drawable.bg_selected); selectedPosition = position; } else if (selectedPosition == position) { // the same item is selected as the previous one, deselect selectedView = null; view.setBackgroundResource(R.drawable.bg_unselected); selectedPosition = -1; } else { // another item is selected, deselect the previous and select the new one selectedView.setBackgroundResource(R.drawable.bg_unselected); selectedView = view; view.setBackgroundResource(R.drawable.bg_selected); selectedPosition = position; } arrayAdapter.setSelectedPosition(selectedPosition); } }); </code></pre> <p>The adapter code:</p> <pre><code>public class MyAdapter extends ArrayAdapter&lt;MyObject&gt; { private int selectedPosition; private static class ViewHolder { RelativeLayout layout; ... } public void setSelectedPosition(int selectedPosition) { this.selectedPosition = selectedPosition; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.list_row, null); holder = new ViewHolder(); holder.layout = (RelativeLayout) convertView.findViewById(R.id.rl_item); ... convertView.setTag(holder); } else holder = (ViewHolder) convertView.getTag(); ... if (position == selectedPosition) holder.layout.setBackgroundResource(R.drawable.bg_selected); else holder.layout.setBackgroundResource(R.drawable.bg_unselected); ... return convertView; } ... } </code></pre> <p>Hope it helps!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload