Monday, May 20, 2013

Re: [android-developers] ListView with "selected" state does not work when list item view has a background

That is actually more along the lines of what I was talking about, now that I think about it...  Changing the background color or drawable for a selected item rather than inflating a completely different view.

I didn't use a selector to do that, but I like that approach better.  I was simply storing the selected position in the adapter, and then when getView gets called I would either set the background color or set the background drawable to what I wanted in the case of the position matching the selected position.


Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware


On Mon, May 20, 2013 at 9:01 AM, Kostya Vasilyev <kmansoft@gmail.com> wrote:
Maybe it's just me, but...

On Monday, May 20, 2013 6:39:14 PM UTC+4, MagouyaWare wrote:

On Thu, May 16, 2013 at 3:14 AM, Miha <miha.v...@gmail.com> wrote:
One possible solution I see is modifying the backing adapter implementation and providing a different view based on the state of the item, but that seems like wrong approach -- I would have to update the adapter with information on the selected item and call notifyDataSetChanged, which would (I suppose) result in an unnecessary re-drawing of the whole list.

Note though, that it doesn't redraw the ENTIRE list... just the items that are visible on the screen.  So, even if you have 500 items in your list, you are only going to be redrawing the 10-20 items that are actually visible to the user.  I've used this approach before and it works quite well.

Implementing highlight through getView, and with a separate view type seems awfully backwards, since the framework already has mechanisms for doing almost all of it.

A custom list item background should let the standard ListView item background show in certain states, e.g. first two states here:

<?xml version="1.0" encoding="utf-8"?>

    <item android:drawable="@android:color/transparent" android:state_pressed="true"/>
    <item android:drawable="@android:color/transparent" android:state_selected="true"/>
    <item android:drawable="@color/theme_light_message_list_checked" android:state_checked="true"/>
    <item android:drawable="@color/theme_light_message_list_read_background"/>

</selector>

Using ListView.drawSelectorOnTop=true would be even easier, but it can have some visual side effects (or not... depends on list item view... mine don't like this).

The third state is what I use to implement "currently selected items" highlight on Android 2.1 - 4.2, by using setChecked on my item layouts. I chose this rather than state_activated, because the latter is API 11 and higher.

public class AbsMessageListItemLayout extends RelativeLayout {

  public void setChecked(boolean isChecked) {
     if (mIsChecked != isChecked) {
mIsChecked = isChecked;
refreshDrawableState();
    }
  }
 
private static final int[] STATE_CHECKED = new int[] { android.R.attr.state_checked };

@Override
protected int[] onCreateDrawableState(int extraSpace) {
int[] baseState = super.onCreateDrawableState(extraSpace + 1);
if (mIsChecked) {
mergeDrawableStates(baseState, STATE_CHECKED);
}
return baseState;
}
}

The final state in the above drawable is application specific (read/unread message indication), can be ignored...

-- K



Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware

--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment