当键盘隐藏时,ListView 回收错误位置的项目

发布于 2024-11-05 02:43:56 字数 4839 浏览 0 评论 0原文

我有一个非常奇怪的错误,我无法理解。以下是视频形式的行为 http://www.youtube.com/watch?v=hHuUzNDI68w< /a>

现在解释一下。我有一个活动,称为 NewsFeed。它由提要项的 ListView 组成(每个提要项都有一张来自 url 的照片,我使用缓存类加载该照片,以及侧面的一些文本)如果我正常打开此活动,则一切都很好,当我滚动所有内容时按预期工作。但是,如果我首先按搜索键(其中出现键盘),然后从搜索结果中选择某些内容,则会打开 NewsFeed 活动 首先,当它出现时,键盘仍然向上,然后一瞬间键盘向下,最终结果是顶部的两个图像是相同的(它们不应该是)

通过一些调试我发现由于某种原因,前两行的 viewHolder 在第二轮中最终成为同一个对象(一旦键盘最终消失)

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

        final HashMap<String, Object> game = getItem(position);
        CollapsedNewsHolder collapsedNewsHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.news_collapsed_module, null);
            collapsedNewsHolder = new CollapsedNewsHolder();
            collapsedNewsHolder.newsPicture = (ImageView) convertView.findViewById(R.id.NewsPicture);
            collapsedNewsHolder.progress = (ProgressBar) convertView.findViewById(R.id.Progress);
            collapsedNewsHolder.title = (TextView) convertView.findViewById(R.id.title);
            collapsedNewsHolder.newsPerson = (TextView) convertView.findViewById(R.id.newsPerson);
            collapsedNewsHolder.upCount = (TextView) convertView.findViewById(R.id.upCount);
            collapsedNewsHolder.commentCount = (TextView) convertView.findViewById(R.id.commentCount);
            collapsedNewsHolder.relativeTime = (TextView) convertView.findViewById(R.id.RelativeTime);
            collapsedNewsHolder.setTypeface();

            //collapsedNewsHolder.imageCallback = new MyImageCallback(mActivity, collapsedNewsHolder.newsPicture, collapsedNewsHolder.progress, MyImageCallback.SMALL, 200);
            //collapsedNewsHolder.itemClickListener = new ItemClickListener();

            convertView.setTag(collapsedNewsHolder);
        } else
            collapsedNewsHolder = (CollapsedNewsHolder) convertView.getTag();

        if ((Integer) getItem(position).get("type") != NewsFeed.POSTER)
            collapsedNewsHolder.title.setTextSize(13f);

        collapsedNewsHolder.newsPicture.setImageBitmap(null);
        collapsedNewsHolder.progress.setVisibility(View.VISIBLE);
        ImageUtil.imageCache.loadAsync((String) game.get("img"), new MyImageCallback(mActivity, collapsedNewsHolder.newsPicture, collapsedNewsHolder.progress, MyImageCallback.SMALL, 200), mContext);

        collapsedNewsHolder.title.setText((String) game.get("title"));
        collapsedNewsHolder.newsPerson.setText((String)game.get("newsPerson"));

        collapsedNewsHolder.relativeTime.setText((String) game.get("relativeTime"));
        collapsedNewsHolder.upCount.setText((Integer) game.get("upCount") + "");
        collapsedNewsHolder.commentCount.setText((Integer) game.get("commentCount") + "");
        //convertView.setOnClickListener(collapsedNewsHolder.itemClickListener.set((String) game.get("id"), position));
        convertView.setOnClickListener(new ItemClickListener((String) game.get("id"), position));
        return convertView;
    }

此外,这里还有一个错误日志说明了问题。请注意内存位置和图像 url,以及列表中的位置。

05-05 17:34:54.774: ERROR/NewsFeedAdapter(7101): Position- 0 Image- android.widget.ImageView@460977f8 Loading image- http://lh3.ggpht.com/o0vA6-MRGAXhhYYkPa1xf3WsPlDe4XnK2xWZeMhmXKBQI8eHGTAos0l1zqC_nox2MrRHKcCKNnPgzqLctsqh25e3LMc=s500

05-05 17:34:54.804: ERROR/NewsFeedAdapter(7101): Position- 1 Image- android.widget.ImageView@46142f28 Loading image- http://lh6.ggpht.com/RcTe-3O-Rywdp0zizVx6k6lSEuPpYbDYnu1UoEnWbhLk6BzwzXt29BvK5rMPAqkrhgtvxvddSTL3pBnMDeLIXVV3s0EZng=s500

05-05 17:34:55.154: ERROR/NewsFeedAdapter(7101): Position- 0 Image- android.widget.ImageView@46142f28 Loading image- http://lh3.ggpht.com/o0vA6-MRGAXhhYYkPa1xf3WsPlDe4XnK2xWZeMhmXKBQI8eHGTAos0l1zqC_nox2MrRHKcCKNnPgzqLctsqh25e3LMc=s500

05-05 17:34:55.174: ERROR/NewsFeedAdapter(7101): Position- 1 Image- android.widget.ImageView@460977f8 Loading image- http://lh6.ggpht.com/RcTe-3O-Rywdp0zizVx6k6lSEuPpYbDYnu1UoEnWbhLk6BzwzXt29BvK5rMPAqkrhgtvxvddSTL3pBnMDeLIXVV3s0EZng=s500

05-05 17:34:55.304: ERROR/NewsFeedAdapter(7101): Position- 2 Image- android.widget.ImageView@4615cd28 Loading image- http://lh4.ggpht.com/V95vJI4WHUUluC8_jHaJw011U6RO7I9rVBQsdSvwDrnUrOCN2YvqhHftr7o0YMNv8cIEBEjsnLNvIH0Y1KoB-rzw6hrKiQ=s500

05-05 17:34:55.334: ERROR/NewsFeedAdapter(7101): Position- 3 Image- android.widget.ImageView@46061610 Loading image- http://lh3.ggpht.com/-c7Ss0t2luOD-npAFZcjRYbnuasqjUNTOv75pFhyprhyzGDf8QdIz_PrJS5qcmBb0a5xrXzZWZYH7oc06eo=s500

05-05 17:34:55.384: ERROR/NewsFeedAdapter(7101): Position- 4 Image- android.widget.ImageView@4613d058 Loading image- http://lh3.ggpht.com/iCKp7kwcnbnZGD4vBb-Mz_mtSKPvST-YGeekJ9khljpG-Ua5Kl71ETdn8LLulULmq9fgNy8huooi_CfrT4W3gjqoFhiB=s500

I have a really weird bug, that I cannot understand. Here is the behavior in video form http://www.youtube.com/watch?v=hHuUzNDI68w

Now to explain. I have an activity, called NewsFeed. which consists of a ListView of feed items (each has a photo that comes from a url, which I load using a caching class, and some text on the side) if I open this activity normaly, everything is alright, when I scroll around everything works as expected. If I however first press the search key, wherein the keyboard comes up, and I select something from the search results, the NewsFeed activity opens
At first when it shows up, the keyboard is still up, and then a split second later the keyboard goes down the end result is that the 2 images on top are identical (they shouldn't be)

Through some debugging I have figured out that for some reason the viewHolder for the top 2 rows, ends up being the same object on the second go round (once the keyboard is finally gone)

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

        final HashMap<String, Object> game = getItem(position);
        CollapsedNewsHolder collapsedNewsHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.news_collapsed_module, null);
            collapsedNewsHolder = new CollapsedNewsHolder();
            collapsedNewsHolder.newsPicture = (ImageView) convertView.findViewById(R.id.NewsPicture);
            collapsedNewsHolder.progress = (ProgressBar) convertView.findViewById(R.id.Progress);
            collapsedNewsHolder.title = (TextView) convertView.findViewById(R.id.title);
            collapsedNewsHolder.newsPerson = (TextView) convertView.findViewById(R.id.newsPerson);
            collapsedNewsHolder.upCount = (TextView) convertView.findViewById(R.id.upCount);
            collapsedNewsHolder.commentCount = (TextView) convertView.findViewById(R.id.commentCount);
            collapsedNewsHolder.relativeTime = (TextView) convertView.findViewById(R.id.RelativeTime);
            collapsedNewsHolder.setTypeface();

            //collapsedNewsHolder.imageCallback = new MyImageCallback(mActivity, collapsedNewsHolder.newsPicture, collapsedNewsHolder.progress, MyImageCallback.SMALL, 200);
            //collapsedNewsHolder.itemClickListener = new ItemClickListener();

            convertView.setTag(collapsedNewsHolder);
        } else
            collapsedNewsHolder = (CollapsedNewsHolder) convertView.getTag();

        if ((Integer) getItem(position).get("type") != NewsFeed.POSTER)
            collapsedNewsHolder.title.setTextSize(13f);

        collapsedNewsHolder.newsPicture.setImageBitmap(null);
        collapsedNewsHolder.progress.setVisibility(View.VISIBLE);
        ImageUtil.imageCache.loadAsync((String) game.get("img"), new MyImageCallback(mActivity, collapsedNewsHolder.newsPicture, collapsedNewsHolder.progress, MyImageCallback.SMALL, 200), mContext);

        collapsedNewsHolder.title.setText((String) game.get("title"));
        collapsedNewsHolder.newsPerson.setText((String)game.get("newsPerson"));

        collapsedNewsHolder.relativeTime.setText((String) game.get("relativeTime"));
        collapsedNewsHolder.upCount.setText((Integer) game.get("upCount") + "");
        collapsedNewsHolder.commentCount.setText((Integer) game.get("commentCount") + "");
        //convertView.setOnClickListener(collapsedNewsHolder.itemClickListener.set((String) game.get("id"), position));
        convertView.setOnClickListener(new ItemClickListener((String) game.get("id"), position));
        return convertView;
    }

Also here is an error log that illustrates the problem. Note the memory locations and the image urls, in regards to the position in the list.

05-05 17:34:54.774: ERROR/NewsFeedAdapter(7101): Position- 0 Image- android.widget.ImageView@460977f8 Loading image- http://lh3.ggpht.com/o0vA6-MRGAXhhYYkPa1xf3WsPlDe4XnK2xWZeMhmXKBQI8eHGTAos0l1zqC_nox2MrRHKcCKNnPgzqLctsqh25e3LMc=s500

05-05 17:34:54.804: ERROR/NewsFeedAdapter(7101): Position- 1 Image- android.widget.ImageView@46142f28 Loading image- http://lh6.ggpht.com/RcTe-3O-Rywdp0zizVx6k6lSEuPpYbDYnu1UoEnWbhLk6BzwzXt29BvK5rMPAqkrhgtvxvddSTL3pBnMDeLIXVV3s0EZng=s500

05-05 17:34:55.154: ERROR/NewsFeedAdapter(7101): Position- 0 Image- android.widget.ImageView@46142f28 Loading image- http://lh3.ggpht.com/o0vA6-MRGAXhhYYkPa1xf3WsPlDe4XnK2xWZeMhmXKBQI8eHGTAos0l1zqC_nox2MrRHKcCKNnPgzqLctsqh25e3LMc=s500

05-05 17:34:55.174: ERROR/NewsFeedAdapter(7101): Position- 1 Image- android.widget.ImageView@460977f8 Loading image- http://lh6.ggpht.com/RcTe-3O-Rywdp0zizVx6k6lSEuPpYbDYnu1UoEnWbhLk6BzwzXt29BvK5rMPAqkrhgtvxvddSTL3pBnMDeLIXVV3s0EZng=s500

05-05 17:34:55.304: ERROR/NewsFeedAdapter(7101): Position- 2 Image- android.widget.ImageView@4615cd28 Loading image- http://lh4.ggpht.com/V95vJI4WHUUluC8_jHaJw011U6RO7I9rVBQsdSvwDrnUrOCN2YvqhHftr7o0YMNv8cIEBEjsnLNvIH0Y1KoB-rzw6hrKiQ=s500

05-05 17:34:55.334: ERROR/NewsFeedAdapter(7101): Position- 3 Image- android.widget.ImageView@46061610 Loading image- http://lh3.ggpht.com/-c7Ss0t2luOD-npAFZcjRYbnuasqjUNTOv75pFhyprhyzGDf8QdIz_PrJS5qcmBb0a5xrXzZWZYH7oc06eo=s500

05-05 17:34:55.384: ERROR/NewsFeedAdapter(7101): Position- 4 Image- android.widget.ImageView@4613d058 Loading image- http://lh3.ggpht.com/iCKp7kwcnbnZGD4vBb-Mz_mtSKPvST-YGeekJ9khljpG-Ua5Kl71ETdn8LLulULmq9fgNy8huooi_CfrT4W3gjqoFhiB=s500

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2024-11-12 02:43:56

我有类似的问题。我通过将 android:windowSoftInputMode 设置为 adjustPan 解决了这个问题。

I had a similar problem. I solved it by setting android:windowSoftInputMode to adjustPan.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文