2

I am using Customized grid view. The childs are repeating while loading and changing position while scrolling. Below is the code.

// 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;

Depending on the count I am changing Grid Layout its working.

I am using Universal loader to show images using urls

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

please let me know what wrong I am doing.

TommySM
  • 3,793
  • 3
  • 24
  • 36

1 Answers1

0

Altough your using Universal Loader, I think it's related to this SO question, and this this SO question and I quote (first one):

The reason is that the view you are reusing from the convertView parameter belongs to a previous row that was possibly already being loaded by Picasso for another picture.

This is only really necessary when using the convertView, if you have just inflated a new layout, it won't be needed..but you can call always to make your code easier.

(queted the first but you should read both of them ;) )

Picasso has a CaneclRequest method, read about Universl Loader, I'm guessing it has something similar, and also, see if you can modify your getView method, seems cumbersome.

Hope this Helps!

Community
  • 1
  • 1
TommySM
  • 3,793
  • 3
  • 24
  • 36
  • Hi TommySM, thanks for your reply. I solved this problem by enabling DisplayImageOptions.Builder().cacheOnDisk(true).cacheInMemory(true).build(); in universal loader. –  Jun 08 '15 at 07:38
  • Good Job! It would be good if you could post the answer yourself and mark it as the correct answer so it will help others, also, if my answer helped in some way, please consider upvoting :) – TommySM Jun 08 '15 at 09:53