1

I have two ListView in an XML, namely lvPrograms & lvEpisodes. They are placed horizontally.

I fill these ListViews from a web service.

When the Activity loads, i call the web service to get data for lvPrograms. when i receive Programs, i then load the Episodes for the first program in the retrieved list. and set the lvPrograms's first item as selected/highlighted, to show user that the loaded Episodes are for this Program item. I set it as follows:

private void highlightSelectedProgram(int _previousSelectedProgramIndex, int _currentSelectedProgramIndex) {
    ListView allProgramsList = (ListView) findViewById(R.id.allProgramsList);

    //Get the last selected List Item
    View selectedChild = allProgramsList
            .getChildAt(_currentSelectedProgramIndex);

    //_previousSelectedProgramIndex & _currentSelectedProgramIndex are to keep track 
    //of currently/previously selected PROGRAM index


    if (selectedChild != null) {

        // get selected shape
        Drawable shape = getResources().getDrawable(
                R.drawable.selected_item_selector);

        //change selected item background to be highlighted
        selectedChild.setBackgroundDrawable(shape);

        //change previous item, if any (is not -1), to normal state
        if (_previousSelectedProgramIndex != ._currentSelectedProgramIndex && _previousSelectedProgramIndex != -1) {
            TextView previousChild = (TextView) allProgramsList
                    .getChildAt(_previousSelectedProgramIndex);

            previousChild.setBackgroundResource(R.drawable.item_selector);
        }
    }       
}

I call this method when user clicks on PROGRAMS list item to highlight the item whose EPISODES are being loaded in lvEpisodes listview.

It looks like the following image.

enter image description here

Issues occur ONLY when the ListView has more items then its visible area. So when i click the first item, it background is changed by the above code BUT some other item , which is among the invisible items, also changes the background. WHY??

I think i have missed some thing OR handling the initially invisible list items is different.

OR You can guide me to a way where i can declare a background selector for that item which is CLICKED ... and only the clicked item remain highlighted .. So if user clicks on some other item in the list which is among the hidden items, then that item becomes highlighted... so there must be a single HIGHLIGHTED item at any time in the list ... This will be great If its possible.

any help is greatly appreciated as the release date is close. Thanks

Aamir
  • 1,747
  • 5
  • 26
  • 50

1 Answers1

2

use something like:

 lvPrograms.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
 lvPrograms.setSelector(R.drawable.programs_background);

programs_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_activated="true"  android:drawable="@drawable/shape" />
</selector>
  • thanks Megha Joshi ... i will check it as soon as i reach my system – Aamir Feb 15 '12 at 20:37
  • Thank you very much Megha... this is working great as expected... there just one other thing u can tell me ... i want the first item in the list to have a separate drawable set when the list loads for the first time,as shown in the image above... is there a way to tell the list to mark first item in the list as "activated"?? thanks – Aamir Feb 16 '12 at 08:17
  • 1
    ok ... got it answer on SO as lvPrograms.setItemChecked(0, true); ... its done now ... thanks – Aamir Feb 16 '12 at 16:12