使用通用图像加载器在GridView中重复子项

时间:2015-06-06 08:02:23

标签: android android-gridview universal-image-loader

我正在使用自定义网格视图。在滚动时加载和改变位置时,孩子们正在重复。以下是代码。

// TODO Auto-generated method stub
        // View grid;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // grid = new View(mContext);
        if (convertView == null) {

            if (gridCount == 0) {

                convertView = inflater.inflate(R.layout.customgridxmlfirst,null);

                RelativeLayout relativeLayout1 = (RelativeLayout) convertView
                        .findViewById(R.id.relative_first);
                relativeLayout1.getLayoutParams().height = (int) (screenWidth / 2);
                relativeLayout1.getLayoutParams().width = (int) (screenWidth / 2.1);

                ImageView imageView = (ImageView) convertView
                        .findViewById(R.id.grid_image_first);
                imageView.getLayoutParams().width = (int) (screenWidth / 2.5);
                imageView.getLayoutParams().height = (int) (screenWidth / 2.5);
                // imageView.setImageBitmap(bitmapBitmaparrayList.get(position));

                imageLoader.displayImage(bitmapBitmaparrayList.get(position),
                        imageView);

                imageView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        ProductDetailFragment gridViewFragment = new ProductDetailFragment();

                        FragmentManager fm = ((MainActivity) mContext)
                                .getFragmentManager();
                        Bundle bundle = new Bundle();
                        bundle.putInt("positon", position);

                        String imagurl = bitmapBitmaparrayList.get(position);
                        bundle.putString("imageurl", imagurl);
                        gridViewFragment.setArguments(bundle);

                        fm.beginTransaction()
                                .replace(R.id.launchfragmentlayout,
                                        gridViewFragment).addToBackStack(null)
                                .commit();

                        Toast.makeText(mContext, "Clicked" + position,
                                Toast.LENGTH_SHORT).show();

                    }
                });

                TextView productprice = (TextView) convertView
                        .findViewById(R.id.productprice);

                // productprice.setTextSize(screenWidth/70);

                TextView productDetails = (TextView) convertView
                        .findViewById(R.id.grid_text_desc);
                // productDetails.setText(productDescArray.get(position));
                // productDetails.setTextSize(screenWidth/70);

                TextView productDiscount = (TextView) convertView
                        .findViewById(R.id.disc_ammount);
                productDiscount.setPaintFlags(productDiscount.getPaintFlags()
                        | Paint.STRIKE_THRU_TEXT_FLAG);

                Button shareButton = (Button) convertView
                        .findViewById(R.id.shareproduct);
                shareButton.getLayoutParams().height = (int) (screenWidth / 19);
                shareButton.getLayoutParams().width = (int) (screenWidth / 19);
                // shareButton.setBackgroundResource(R.drawable.addtocart);

            } else if (gridCount == 1) {
                convertView = inflater.inflate(R.layout.customgridsecond, null);

                RelativeLayout linearLayout = (RelativeLayout) convertView
                        .findViewById(R.id.relativelaout_sec_image);
                linearLayout.getLayoutParams().width = (int) (screenWidth / 2.7);
                linearLayout.getLayoutParams().height = (int) (screenWidth / 2.7);

                RelativeLayout relativelaout_sec = (RelativeLayout) convertView
                        .findViewById(R.id.layout_text);
                relativelaout_sec.getLayoutParams().height = (int) (screenWidth / 2.9);

                TextView productDiscount = (TextView) convertView
                        .findViewById(R.id.disc_price);
                productDiscount.setPaintFlags(productDiscount.getPaintFlags()
                        | Paint.STRIKE_THRU_TEXT_FLAG);

                TextView productDesc = (TextView) convertView
                        .findViewById(R.id.productdesctxt);
                // productDesc.setText(productDescArray.get(position));

                ImageView imageView = (ImageView) convertView
                        .findViewById(R.id.grid_image_sec);
                imageView.getLayoutParams().width = (int) (screenWidth / 3);
                imageView.getLayoutParams().height = (int) (screenWidth / 3);

                imageView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        ProductDetailFragment gridViewFragment = new ProductDetailFragment();
                        Bundle bundle = new Bundle();
                        bundle.putInt("position", position);
                        bundle.putString("imageurl",
                                bitmapBitmaparrayList.get(position));
                        gridViewFragment.setArguments(bundle);

                        FragmentManager fm = ((MainActivity) mContext)
                                .getFragmentManager();
                        fm.beginTransaction()
                                .replace(R.id.launchfragmentlayout,
                                        gridViewFragment).addToBackStack(null)
                                .commit();
                        Toast.makeText(mContext, "Clicked" + position,
                                Toast.LENGTH_SHORT).show();

                    }
                });

                imageLoader.displayImage(bitmapBitmaparrayList.get(position),
                        imageView);

                Button shareButton = (Button) convertView
                        .findViewById(R.id.sharesecond);
                shareButton.getLayoutParams().height = (int) (screenWidth / 19);
                shareButton.getLayoutParams().width = (int) (screenWidth / 19);

                Button addToCartButton = (Button) convertView
                        .findViewById(R.id.addto_cart);
                addToCartButton.getLayoutParams().width = (int) (screenWidth / 4);
                addToCartButton.getLayoutParams().height = (int) (screenWidth / 14);

            } else if (gridCount == 2) {

                convertView = inflater.inflate(R.layout.customisedgridthird,
                        null);

                RelativeLayout sampleLayout = (RelativeLayout) convertView
                        .findViewById(R.id.samplelayout);
                sampleLayout.getLayoutParams().width = (int) (screenWidth / 1.05);
                sampleLayout.getLayoutParams().height = (int) (screenWidth / 1.3);

                ImageView imageView = (ImageView) convertView
                        .findViewById(R.id.grid_image_third);
                imageView.getLayoutParams().width = (int) (screenWidth / 2);
                imageView.getLayoutParams().height = (int) (screenWidth / 2);
                imageLoader.displayImage(bitmapBitmaparrayList.get(position),
                        imageView);

                imageView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        ProductDetailFragment gridViewFragment = new ProductDetailFragment();
                        Bundle bundle = new Bundle();
                        bundle.putInt("position", position);
                        bundle.putString("imageurl",
                                bitmapBitmaparrayList.get(position));
                        gridViewFragment.setArguments(bundle);

                        gridViewFragment.setArguments(bundle);

                        FragmentManager fm = ((MainActivity) mContext)
                                .getFragmentManager();
                        fm.beginTransaction()
                                .replace(R.id.launchfragmentlayout,
                                        gridViewFragment).addToBackStack(null)
                                .commit();
                        Toast.makeText(mContext, "Clicked" + position,
                                Toast.LENGTH_SHORT).show();

                    }
                });

                Button shareButton = (Button) convertView
                        .findViewById(R.id.sharethird);
                shareButton.getLayoutParams().height = (int) (screenWidth / 19);
                shareButton.getLayoutParams().width = (int) (screenWidth / 19);

                TextView productDiscount = (TextView) convertView
                        .findViewById(R.id.disc_price_third);
                productDiscount.setPaintFlags(productDiscount.getPaintFlags()
                        | Paint.STRIKE_THRU_TEXT_FLAG);

                TextView producTextView = (TextView) convertView
                        .findViewById(R.id.productpriceseondgviewthird);

            }

        }

        return convertView;

根据计数,我正在改变网格布局的工作。

我使用Universal loader来显示使用网址的图片

imageLoader.displayImage(bitmapBitmaparrayList.get(position), imageView);
请告诉我我的错误。

1 个答案:

答案 0 :(得分:0)

使用Universal Loader,我认为它与this SO questionthis SO question相关 我引用(第一个):

  

原因是你从convertView参数重用的视图属于Picasso可能已经为另一张图片加载的前一行。

     

这在使用convertView时才真正有用,如果您刚刚为新布局充气,则不需要它。但您可以随时致电以使代码更容易。

(排在第一位,但你应该阅读它们;))

Picasso有一个CaneclRequest方法,读到Universl Loader,我猜它有类似的东西,而且,看看你是否可以修改你的getView方法,看起来很麻烦。

希望这有帮助!