Android的ListView项选择问题问题、Android、ListView

2023-09-07 00:00:21 作者:不归路

我有两个ListView控件在XML,即 lvPrograms &放大器; lvEpisodes 。他们被水平放置。

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

我填充这些列表视图的Web服务。

I fill these ListViews from a web service.

在活动负载,我调用Web服务以获取 lvPrograms 数据。当我收到程序,我然后加载插曲在检索列表中的第一个程序。并作为选择/高亮,显示用户设置的 lvPrograms 的第一项所装载的插曲是本计划项目。我将它设置如下:

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);
        }
    }       
}

我把这种方法,当用户点击程序列表项突出的 EPISODES 被加载在 lvEpisodes 列表视图的项目。

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.

问题只发生在ListView中有更多的项目那么它的可视面积。所以,当我点击第一个项目,它的背景是由上述code,但一些其他的项目,这是无形的项目中发生变化,也改变了背景。为什么?

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.

任何帮助是极大的pciated随着发布日期的接近AP $ P $。 谢谢

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

推荐答案

使用这样的:

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

programs_background.xml

programs_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_activated="true"  android:drawable="@drawable/shape" />
</selector>
 
精彩推荐