安卓:在列表中滚动ListView项的背景问题 [英] Android:Listview item background issue on list scrolling

查看:186
本文介绍了安卓:在列表中滚动ListView项的背景问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面面临的问题,项背地面上滚动。

I facing below issue with item back ground on scrolling.

在我的应用我有一个ListView需要多选。另外这就是选择需要通过改变列表项的颜色,而不是复选框基础的方法psented再$ P $自定义列表。 对于这样的:在onclick如果位置选择或没有,然后设置背景的项目我检查。然而,这有问题,当我滚动列表。以一个例子: 假设列表中有50个项目。和图10是在一个时间可见。我选择说,第五项[从而改变背景。然后我滚动列表。滚动相应前面第五项列表的可见部分后,例如在列表中,但第五指数在可见部的项目第十五项,仍具有对应于选择状态的背景。虽然它不应该被置1,因为我没有选择15项呢。

In my application I have a listview which require multi-selection. Also this is a custom list where selection needs to be represented by change in list item color instead of check-box based approach. For this: In the OnClick I'm checking if the position is selected or not and then set the background for the item. However this has issue when I scroll the list. Taking an example: suppose the list has 50 items. And 10 are visible at a time. I select say 5th item [thus changing the background]. And then I scroll the list. After scroll the visible part of the list corresponding to earlier 5th item ,say 15th item in item of the list but 5th index in visible portion,still has background corresponding to selected state. Whereas it should not have been set since I have not selected 15th item yet.

我想: 一,在适配器的getView方法,如果该项目没有选择的项目之一,我设置一个后台别人different.Tried - setBackgroundColor以及setBackgrounddrawable。 B-在XML设置了cacheColorHint透明 C-有选择连接到项目和应对国发[pressed,选择在onlcick的项目。

I tried: a-In the getView method of adapter, if the item is not one of selected items I'm setting one background else different.Tried - setBackgroundColor as well setBackgrounddrawable. b- In the xml have set the cacheColorHint to transparent c- Have selector attached to items and the items responding to state [pressed,selected] in onlcick.

不过我依然无法获得项目的滚动去掉不需要的背景颜色。

However still I'm not able to get rid of unwanted background color for item on scrolling.

任何帮助。我尝试过各种职位中提到的各种建议,在SO但succcessful呢。

Any help. I tried various suggestion mentioned in various post in SO but not succcessful yet.

我试过

谢谢 普拉迪普

推荐答案

这是在Android的ListView的适配器的正常现象,它的getView()呼吁每一个滚动和每一个新的列表项它调用getView,如果列表视图当前项目在UI那么它convertView是不可见等于空:在时间列表视图采取的唯一可见的列表项的负载,如果一次显示10元出50,然后listView.getChildCount()将返回只有10不是50。 在你的情况下,当您选择5,它反映了选5 + 10(可视项目数)= 15,25,35,45了。 为了解决这个问题,你应该有一个标志,联想与您的每一个的listItem的数据,例如,如果您有字符串数组的ItemData [50]作为数组,然后采取布尔isSelected [50]使用初始值false每个数组。

this is a normal behavior of ListView adapter in android, its getView() called on every scroll and for every new list item it call getView, if listview item currently not visible on UI then its convertView is equals to null: At a time listview take load of only visible list items, if it showing at a time 10 element out of 50, then listView.getChildCount() will return only 10 not 50. In your case when you select 5, it reflected selection for 5+10(visible items count) = 15, 25, 35, 45 too. To solve this problem you should have a flag associate with your each listItem data, for example if you have string array itemData[50] as array, then take an array of boolean isSelected[50] with initial value false for each.

一起来看看getView(),适配器类:

Take a look for getView(), in adapter class:

      public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
                    string text = itemData[position]
        if (convertView == null) {
            rowLayout = (RelativeLayout) LayoutInflater.from(context)
                    .inflate(R.layout.list_view_item, parent, false);
            holder = new ViewHolder();

            holder.txtString= (TextView) rowLayout
                    .findViewById(R.id.txtTitle);
            rowLayout.setTag(holder);
        } else {
            rowLayout = (RelativeLayout) convertView;
            holder = (ViewHolder) rowLayout.getTag();
        }


        if(isSelected[position] == true){
                   holder.txtString.setText("Selected")
                   rowLayout.setBackGround(selected)
        }else{
                             holder.txtString.setText("Not Selected")
             rowLayout.setBackGround(notSelected)
        }




    public class ViewHolder {
        public TextView txtString;

    }

和在listView.setOnItemClickListener你的Activity类():

and in your Activity class on listView.setOnItemClickListener():

 listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    // TODO Auto-generated method stub
                      isSelected[position] = true  // on selection

                      RelativeLayout rowLayout = (RelativeLayout) view;
          rowLayout.setBackGround(Selected);


                      // also set here background selected for view by getting layout refference


                    }
            });

这篇关于安卓:在列表中滚动ListView项的背景问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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