Android ListView 项目选择问题 [英] Android ListView item selection issue

查看:33
本文介绍了Android ListView 项目选择问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 XML 中有两个 ListView,即 lvPrograms &lvEpisodes.它们水平放置.

我从网络服务填充这些 ListViews.

当活动加载时,我调用网络服务来获取lvPrograms 的数据.当我收到节目时,我会为检索到的列表中的第一个节目加载剧集.并将lvPrograms 的第一个项目设置为选中/突出显示,以向用户显示加载的剧集 是针对该节目项目的.我是这样设置的:

private void highlightSelectedProgram(int _previousSelectedProgramIndex, int _currentSelectedProgramIndex) {ListView allProgramsList = (ListView) findViewById(R.id.allProgramsList);//获取最后选择的List Item查看 selectedChild = allProgramsList.getChildAt(_currentSelectedProgramIndex);//_previousSelectedProgramIndex &_currentSelectedProgramIndex 用于跟踪//当前/以前选择的程序索引如果(selectedChild != null){//获取选中的形状可绘制形状 = getResources().getDrawable(R.drawable.selected_item_selector);//改变选中的项目背景以突出显示selectedChild.setBackgroundDrawable(shape);//改变前一项,如果有的话(不是-1),到正常状态if (_previousSelectedProgramIndex != ._currentSelectedProgramIndex && _previousSelectedProgramIndex != -1) {TextView previousChild = (TextView) allProgramsList.getChildAt(_previousSelectedProgramIndex);previousChild.setBackgroundResource(R.drawable.item_selector);}}}

当用户点击 PROGRAMS 列表项以突出显示 EPISODESlvEpisodes 列表视图中加载的项时,我调用此方法.>

看起来像下图.

仅当 ListView 的项目多于其可见区域时才会出现问题.所以当我点击第一个项目时,它的背景被上面的代码改变了,但是在不可见项目中的其他一些项目也会改变背景.为什么??

我想我错过了一些东西,或者处理最初不可见的列表项是不同的.

OR 你可以指导我使用一种方法,我可以为那个被点击的项目声明一个背景选择器......并且只有被点击的项目保持突出显示......所以如果用户点击其他项目列表中隐藏项目中的项目,那么该项目将突出显示...因此列表中的任何时间都必须有一个突出显示的项目...如果可能的话,这将是很棒的.

非常感谢任何帮助,因为发布日期临近.谢谢

解决方案

使用类似:

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

programs_background.xml

<item android:state_activated="true" android:drawable="@drawable/shape"/></选择器>

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

解决方案

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>

这篇关于Android ListView 项目选择问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆