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

2013-05-21 Thread Miha Valencic
Hi Kostya!

On Tue, May 21, 2013 at 8:37 PM, Kostya Vasilyev  wrote:
> I call setChecked directly, because my "checkbox" tracking is in the same
> layout (although it's not a checkbox)
>
> To track selected items (for getView to do the right thing during scrolling,
> and to know what items user actions apply to), I'm using a
> LongSparseArray... Don't think it matters much (except ListView tracks
> checked items by position, whereas I prefer "stable" database IDs).

In the end, I went with CheckableLinearLayout, implemented
state_checked for it and wired it with an adapter. It works well so
far.

Regards,
 Miha.

-- 
-- 
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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




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

2013-05-21 Thread Kostya Vasilyev
Miha,

On Tuesday, May 21, 2013 12:57:38 PM UTC+4, Miha wrote:
>
> Kostya, how do you handle "checked" states then? I see two possible 
> solutions: 
>
> 1) in OnItemClickListener, call adapter.setChecked(position), which 
> sets the checked item position and then calls notifyDataSetChanged() 
> 2) in OnItemClickListener call setChecked on the view itself plus call 
> adapter.setChecked(position) and define the checked state in the 
> getView() 
>

I call setChecked directly, because my "checkbox" tracking is in the same 
layout (although it's not a checkbox)

To track selected items (for getView to do the right thing during 
scrolling, and to know what items user actions apply to), I'm using a 
LongSparseArray... Don't think it matters much (except ListView tracks 
checked items by position, whereas I prefer "stable" database IDs).

I'm not using any choice modes inside ListView, because I also have an 
ActionMode UI implementation for Android 2.*, and it's abstracted away from 
the actual list... I guess the usefulness of sharing this is starting to 
rapidly diminish :)

 

>
> I see option 2 as a minor performance improvement over option1. How 
> did you handle it? 
>
> @Romain, is there a way to use state_activated in API level 10 via 
> some sort of compatibility package? Could I create such a 
> compatibility lib myself perhaps? 
>

In my experience the resource system will filter out attributes that aren't 
present in the actual runtime platform... So for example, you can use 
android:actionBarStyle=... when running on 2.*... and I expect 
android:attr_activated is the same way.

-- K

 

>
> Regards, 
>  Miha. 
>

-- 
-- 
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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




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

2013-05-21 Thread Miha Valencic
On Tue, May 21, 2013 at 12:11 AM, Miha Valencic  wrote:
> In this case, which is the correct state? I now see that
> "state_activated" works as expected, though as Kostya mentions, it is
> API Level 11 and beyond. Is there an alternatvive for API level 10?

So... I've implemented a couple of verisions (I pushed to github,
previously mentioned): using selector with state_activated, using
custom view for selected item via "getView" and using custom
"Checkable" layout, which uses state_checked approach. The
state_activated is what I would go for if it would be at least API
level 10, so between two other options, Checkable layout seems like a
good way to go.

Kostya, how do you handle "checked" states then? I see two possible solutions:

1) in OnItemClickListener, call adapter.setChecked(position), which
sets the checked item position and then calls notifyDataSetChanged()
2) in OnItemClickListener call setChecked on the view itself plus call
adapter.setChecked(position) and define the checked state in the
getView()

I see option 2 as a minor performance improvement over option1. How
did you handle it?

@Romain, is there a way to use state_activated in API level 10 via
some sort of compatibility package? Could I create such a
compatibility lib myself perhaps?

Regards,
 Miha.

-- 
-- 
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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




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

2013-05-20 Thread Miha Valencic
On Mon, May 20, 2013 at 10:15 PM, Romain Guy  wrote:
> The selected state is used when the item is "focused" using a keyboard. It
> has nothing to do with the choice mode.

In this case, which is the correct state? I now see that
"state_activated" works as expected, though as Kostya mentions, it is
API Level 11 and beyond. Is there an alternatvive for API level 10?

Regards,
 Miha.

-- 
-- 
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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




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

2013-05-20 Thread Romain Guy
The selected state is used when the item is "focused" using a keyboard. It
has nothing to do with the choice mode.
On May 20, 2013 12:32 PM, "Miha Valencic"  wrote:

> On Mon, May 20, 2013 at 9:23 PM, Miha Valencic 
> wrote:
> > Am I misunderstanding this statement: "...or use a background on your
> > list items with a selected state."?
>
> Also, one more thing: does it even makes sense to use listselector
> (setSelector) if the background is a state drawable already?
>
> Miha
>
> --
> --
> 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+unsubscr...@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+unsubscr...@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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




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

2013-05-20 Thread Miha Valencic
On Mon, May 20, 2013 at 9:23 PM, Miha Valencic  wrote:
> Am I misunderstanding this statement: "...or use a background on your
> list items with a selected state."?

Also, one more thing: does it even makes sense to use listselector
(setSelector) if the background is a state drawable already?

Miha

-- 
-- 
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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




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

2013-05-20 Thread Miha Valencic
Hi everybody!

On Mon, May 20, 2013 at 8:46 PM, Romain Guy  wrote:
>
> The list selector is drawn by default under list items. You can either change 
> it to draw on top (but you'll have to use custom drawables so the items are 
> visible underneath) or use a background on your list items with a selected 
> state.

I've read that before, only I seem to be doing something wrong. I've
create an ugly sample project
(https://github.com/skyflyer/android_list_selection), where I
demonstrate that when there is no background, list selector is
working, but when there is, list selector is not working, even though
the "list item background" is set to a drawable which is a selector.

To be more clear, item layout (row.xml) has:

http://schemas.android.com/apk/res/android";
xmlns:tools="http://schemas.android.com/tools";
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="@drawable/row_background">

where row_background is defined like this:


http://schemas.android.com/apk/res/android"; >






The way I understand this is: row should be white by default. When
pressed, it should be blue (and it is!), and when selected, it should
be red (when list choiceMode is set appropriately). But that is not
the case. The row background stays the same.

Am I misunderstanding this statement: "...or use a background on your
list items with a selected state."?

Romain, if I understand correctly: list selector is drawn "under the
item view". So when there is no background (it is transparent), the
list selector "shows through". But when list item has a background,
this selector is not visible. Correct? But if I set the background of
the list item to a "selector", this selector should match
"state_selected" that should come into effect, when list item is
selected? (it does not in my case).

Regards,
 Miha.

-- 
-- 
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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




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

2013-05-20 Thread Romain Guy
The list selector is drawn by default under list items. You can either
change it to draw on top (but you'll have to use custom drawables so the
items are visible underneath) or use a background on your list items with a
selected state.


On Mon, May 20, 2013 at 11:19 AM, Miha Valencic wrote:

> Hi Kostja et al!
>
> On Mon, May 20, 2013 at 5:01 PM, Kostya Vasilyev wrote:
>
>> Maybe it's just me, but...
>
>
> No, it's not just you. I also expect that framework should handle this.
> And it does, as long as list item layout does not have a background set.
>
> Let me prepare a demo, and I'll post it.
>
> Regards,
>  Miha.
>
> --
> --
> 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+unsubscr...@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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>



-- 
Romain Guy
Android framework engineer
romain...@android.com

-- 
-- 
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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




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

2013-05-20 Thread Miha Valencic
Hi Kostja et al!

On Mon, May 20, 2013 at 5:01 PM, Kostya Vasilyev  wrote:

> Maybe it's just me, but...


No, it's not just you. I also expect that framework should handle this. And
it does, as long as list item layout does not have a background set.

Let me prepare a demo, and I'll post it.

Regards,
 Miha.

-- 
-- 
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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




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

2013-05-20 Thread Justin Anderson
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  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  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:
>
> 
> http://schemas.android.com/apk/res/android";>
>
>  android:state_pressed="true"/>
>  android:state_selected="true"/>
>  android:state_checked="true"/>
>  android:drawable="@color/theme_light_message_list_read_background"/>
>
> 
>
> 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+unsubscr...@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+unsubscr...@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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




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

2013-05-20 Thread Kostya Vasilyev
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  >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:


http://schemas.android.com/apk/res/android";>








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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




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

2013-05-20 Thread Justin Anderson
On Thu, May 16, 2013 at 3:14 AM, Miha  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.

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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.