Android ListView item selection issue

和自甴很熟 提交于 2019-12-04 02:00:30

问题


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.

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


回答1:


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>


来源:https://stackoverflow.com/questions/9279626/android-listview-item-selection-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!