tags:

views:

565

answers:

2

Hi All,

I apologize for the following long question...

I have a LinearLayout which contains a ListView and some other items. As for the ListView, each on of its rows is a LinearLayout that contains 3 views - Checkbox, ImageView and TextView (from left to right - horizontal). Since I wanted the whole row to be selected when using the trackball (to be highlighted with a background color), I set the all three views inside the LinearLayout row as not focusable, and it worked.

Now I'm having 2 problems regarding this ListView. First, I want that whenever I touch a row in the ListView (with my finger), to get the same behavior as when using the trackball - means that I want the row to be selected (highlighted). What's happening right now is that when I touch the row it really becomes selected, but when I release my finger the selection is gone (much like happens in device's contact list).

Second - from a Menu, I can display a new LinearLayout instead the one that contains the ListView (different application's screen). When this happens, I still stores the object of the LinearLayout that contains the ListView, because I want to be able to re-display it later without creating it from scratch. The problem is that when I re-disaply the LinearLayout with the ListView, none of the ListView's rows are selected, even if a ceratin row was selected when the the LinearLayout with the ListView "left" the screen.

Sorry again for the long post.

Thanks!

+2  A: 
  1. This is he default and expected behavior. Tampering with this is strongly suggested against.
  2. This follows from 1. to some extent. The selected-state is not persistent. Rather, assign an OnItemClickListener, and have it store away the id of the selected item into some variable. If you need to re-select the item when you come back, you can use setSelection()
David Hedlund
I already tried to use setSelection() when coming back, but it is no good - first of all the row is not highlighted at all (no background color), and second the row is located at the top of the screen although it was located somewhere in the middle when I left.Thanks.
WhiteTigerK
Another thing - in device's contact list when using the trackball to select a contact and then press the trackball to view his details, the state of the contact list is saved, so when going back to the contact list the contact is still selected and its location in the screen is the same. Therefore I assume that this behavior is possible to achieve.Thanks.
WhiteTigerK
yes, the select state will be saved, but if at any one time you touch the screen you will toggle *touch mode* and when in touch mode, as you have already noticed, there is no visual feedback for an item being selected (only brief feedback when the item is being touched, which is not happening when you return to the listview). this is the default behavior. do not change it.
David Hedlund
A: 

This should help:

private void setListviewSelection(final ListView list, final int pos) {
    list.post(new Runnable() {
        @Override
        public void run() {
            list.setSelection(pos);
            View v = list.getChildAt(pos);
            if (v != null) {
                v.requestFocus();
            }
        }
    });
}
vovkab