ArrayAdapter 顺序中的 ListView 在滚动时混淆 [英] ListView in ArrayAdapter order get's mixed up when scrolling

查看:22
本文介绍了ArrayAdapter 顺序中的 ListView 在滚动时混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义 ArrayAdapter 中有一个 ListView,它在每一行中显示一个图标 ImageView 和一个 TextView.当我使列表足够长以让您滚动浏览时,顺序开始正确,但是当我开始向下滚动时,一些较早的条目开始重新出现.如果我向上滚动,旧的顺序就会改变.重复执行此操作最终会导致整个列表顺序看似随机.所以滚动列表要么导致子顺序改变,要么绘图没有正确刷新.

I have a ListView in a custom ArrayAdapter that displays an icon ImageView and a TextView in each row. When I make the list long enough to let you scroll through it, the order starts out right, but when I start to scroll down, some of the earlier entries start re-appearing. If I scroll back up, the old order changes. Doing this repeatedly eventually causes the entire list order to be seemingly random. So scrolling the list is either causing the child order to change, or the drawing is not refreshing correctly.

什么会导致这样的事情发生?我需要项目向用户显示的顺序与它们添加到 ArrayList 的顺序相同,或者至少保持一个静态顺序.如果我需要提供更详细的信息,请告诉我.任何帮助表示赞赏.谢谢.

What could cause something like this to happen? I need the order the items are displayed to the user to be the same order they are added to the ArrayList, or at LEAST to remain in one static order. If I need to provide more detailed information, please let me know. Any help is appreciated. Thanks.

推荐答案

我遇到了类似的问题,但是当单击自定义列表中的项目时,屏幕上的项目会按顺序反转.如果我再次点击,他们会回到原来的位置.

I was having similar issues, but when clicking an item in the custom list, the items on the screen would reverse in sequence. If I clicked again, they'd reverse back to where they were originally.

阅读本文后,我检查了重载 getView 方法的代码.我正在从convertedView 获取视图,如果它为空,那就是我构建我的东西的时候.但是,在放置断点后,我发现它在每次单击和后续单击时都调用此方法,convertedView 不为空,因此未设置项目.

After reading this, I checked my code where I overload the getView method. I was getting the view from the convertedView, and if it was null, that's when I'd build my stuff. However, after placing a breakpoint, I found that it was calling this method on every click and on subsequent clicks, the convertedView was not null therefore the items weren't being set.

这是一个例子:


public View getView(int position, View convertView, ViewGroup parent)
{
    View view = convertView;
    if (view == null)
    {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.listitemrow, null);

        RssItem rssItem = (RssItem) super.getItem(position);
        if (rssItem != null)
        {
            TextView title = (TextView) view.findViewById(R.id.rowtitle);
            if (title != null)
            {
                title.setText(rssItem.getTitle());
            }
        }
    }
    return view;
}

细微的变化是将视图上空检查的右大括号移动到刚好在膨胀之后:

The subtle change is moving the close brace for the null check on the view to just after inflating:


public View getView(int position, View convertView, ViewGroup parent)
{
    View view = convertView;
    if (view == null)
    {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.listitemrow, null);
    }
    RssItem rssItem = (RssItem) super.getItem(position);
    if (rssItem != null)
    {
        TextView title = (TextView) view.findViewById(R.id.rowtitle);
        if (title != null)
        {
            title.setText(rssItem.getTitle());
        }
    }
    return view;
}

我希望这能帮助遇到同样问题的其他人.

I hope this helps others who experience this same problem.

这篇关于ArrayAdapter 顺序中的 ListView 在滚动时混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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