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 <[email protected] 
> <javascript:>>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"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android";>

    <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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to