【问题标题】:Why getView() of BaseAdapter showing image multiple times?为什么 BaseAdapter 的 getView() 多次显示图像?
【发布时间】:2012-10-30 09:17:58
【问题描述】:

我正在使用以下代码来制作自定义listview。我必须动态显示图像,因此所有图像都在 Linearlayout 中添加到此。问题是这些动态图像添加了多次。下面是我getView()的代码。

    LinearLayout fbpiclayout = null;

        if (convertView == null)
            vi = inflater.inflate(R.layout.popular_event_list, null);

         fbpiclayout = (LinearLayout) vi
                .findViewById(R.id.fbpic_layout);

        ArrayList<String> list=  mDbManager
                .getAllValuesComingSoonFriendList(facebookEventList
                        .get(position).getEventId());


        int height = 0;


        for(int i=0;i<list.size();i++)
        {
            if(i<3)
            {
                String friendPics = "http://graph.facebook.com/"
                        + list.get(i)
                        + "/picture?type=large";

                Log.d("Friends","list no "+i+" "+mDbManager
                        .getFacebookFriendName(list.get(i)));

                ImageView fbFriendimage = new ImageView(getActivity());
                LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
                vp.setMargins(3, 0, 3, 3);
                fbFriendimage.setLayoutParams(vp);
                fbFriendimage.setAdjustViewBounds(true);
                fbFriendimage.getLayoutParams().height = height;

                fbFriendimage.getLayoutParams().width = width_iv;
                fbFriendimage.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //  image.setImageBitmap(bm);
                imageLoader.DisplayImage(friendPics, fbFriendimage);
                fbpiclayout.addView(fbFriendimage, vp);     

            }
        }

请就这个问题向我提出建议。 提前致谢。

【问题讨论】:

  • 多次是什么意思?为什么在 getView 方法中使用循环?
  • 删除 if(convertView == null)
  • 在每个列表项中,我必须添加多个图像。这些图像 url 存储在列表中。就像列表位置一样,我有 3 张图片。 M 将这 3 个图像循环添加到线性布局。多次意味着,在我的列表中只有 3 张图像,但线性布局在该列表位置显示 6 或 9 张图像。每次 getView 调用它都没有增加。
  • @Manikandan 它的作品,但它使列表视图生涩和缓慢。对此有何建议?
  • @Gaurav 使用lazyLoading,您的列表视图可以正常工作。

标签: android list listview baseadapter


【解决方案1】:

试试下面的代码:

LinearLayout fbpiclayout = null;

        if (convertView == null)
            vi = inflater.inflate(R.layout.popular_event_list, null);

         fbpiclayout = (LinearLayout) vi
                .findViewById(R.id.fbpic_layout);

          //if the convertView was not null it would have the child view you added the 
          //  last time liner layout was used. So remove the added child view.  
          fbpiclayout.removeAllViews();

        ArrayList<String> list=  mDbManager
                .getAllValuesComingSoonFriendList(facebookEventList
                        .get(position).getEventId());


        int height = 0;


        for(int i=0;i<list.size();i++)
        {
            if(i<3)
            {
                String friendPics = "http://graph.facebook.com/"
                        + list.get(i)
                        + "/picture?type=large";

                Log.d("Friends","list no "+i+" "+mDbManager
                        .getFacebookFriendName(list.get(i)));

                ImageView fbFriendimage = new ImageView(getActivity());
                LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
                vp.setMargins(3, 0, 3, 3);
                fbFriendimage.setLayoutParams(vp);
                fbFriendimage.setAdjustViewBounds(true);
                fbFriendimage.getLayoutParams().height = height;

                fbFriendimage.getLayoutParams().width = width_iv;
                fbFriendimage.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //  image.setImageBitmap(bm);
                imageLoader.DisplayImage(friendPics, fbFriendimage);
                fbpiclayout.addView(fbFriendimage, vp);     

            }
        }

EDIT1:尝试使用智能图像视图http://loopj.com/android-smart-image-view/ 来提高性能....

【讨论】:

  • 谢谢@praful Bhatnagar:请给我建议。我们可以采取任何方法使列表顺利进行。 M 已经在使用 Lazy Adapter 。我认为 getView 每次都在调用并使 listview 生涩 n 变慢。
【解决方案2】:

不要使用那个循环。将该列表的大小放入 getCount() 中。它会正常工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多