2

I have a grid view and data updated in grid view using api calls and grid item loads a image from url using picasso library all are perfect but when i am scrolling down to up the grid items not scrolled and repeating the items(up to down scrolling was perfect).

Adapterclass.java

private class ProductAdapter extends BaseAdapter {
        ViewHolder holder = null;
        @Override
        public int getCount() {
            return mProducts != null ? mProducts.size() : 0;
        }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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

        holder = new ViewHolder();
        if (convertView == null) {
          //  convertView = LayoutInflater.from(Products_activity.this).inflate(R.layout.product_layout, null, false);

            LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.product_layout, null);

            holder.product_name = (TextView) convertView.findViewById(R.id.product_name);
            holder.product_descr = (TextView) convertView.findViewById(R.id.product_desc);
            holder.product_price = (TextView) convertView.findViewById(R.id.product_price);
            holder.product_pic = (ImageView) convertView.findViewById(R.id.iv_division_img);
            holder.product_share = (ImageView) convertView.findViewById(R.id.productshare_button);
            holder.like_image=(ImageView) convertView.findViewById(R.id.like_image);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }





        if (mProducts != null) {
            String url = "http://staggershop.pivalue.net/assets/product_images/"+mProducts.get(position).product_image;
            String urls = url.replaceAll(" ", "%20");
            Picasso.with(getApplicationContext()).load(urls).resize(300,300).noFade().error(R.drawable.home_brands).into(holder.product_pic);
            holder.product_name.setText(mProducts.get(position).product_name);
            //product_descr.setText(mProducts.get(position).pr);
            // product_price.setText(mProducts.get(position).product_price);
           /* Picasso
                    .with(getApplicationContext())
                    .setLoggingEnabled(true);

            Picasso.with(getApplicationContext())
                    .load(urls)
                    .error(R.drawable.home_brands).memoryPolicy(MemoryPolicy.NO_STORE)
                    .networkPolicy(NetworkPolicy.NO_STORE)
                    .into(product_pic);*/
            holder.product_descr.setText(mProducts.get(position).product_description);

            if(!recently_beans.isEmpty()){
                for(int i=0;i<recently_beans.size();i++) {
                    if (mProducts.get(position).prdid.equals(recently_beans.get(i).prod_id))
                    {
                        holder.like_image.setImageResource(R.drawable.like);

                    }
                }

                     notifyDataSetChanged();


            }






        }

        holder.product_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
                bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
            }
        });
        holder.like_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        public void run() {
                            if (!recently_beans.isEmpty()) {

                                for (int i = 0; i < recently_beans.size(); i++) {
                                    if (mProducts.get(position).prdid.equals(recently_beans.get(i).prod_id)) {

                                        holder.like_image.setImageResource(R.drawable.dislike);
                                        Inserfavorite(mProducts.get(position).prdid);


                                    }
                                }


                            }
                        }
                    });

                }

            });

           // notifyDataSetChanged();
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Inserrecent(mProducts.get(position).prdid);
                Intent intent = new Intent(Products_activity.this, Product_description.class);

                startActivity(intent);


            }
        });
        return convertView;
    }

}
public static class ViewHolder {
    public TextView product_name;
    public TextView product_descr;
    public TextView product_price;
    public ImageView product_pic ;
    public ImageView product_share;
    public ImageView like_image;

}

This my product.xml

 <RelativeLayout
        android:id="@+id/rl_divisioin_image"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="1"
         android:id="@+id/product_img">


         <ImageView
             android:id="@+id/iv_division_img"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_below="@+id/view"
             android:adjustViewBounds="true"
             android:layout_centerHorizontal="true"
             android:scaleType="fitXY"
             android:src="@drawable/home_brands" />
     </LinearLayout>

            <LinearLayout
                android:id="@+id/prod_name_layout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_below="@+id/product_img"
                android:layout_weight="1"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/product_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:elevation="4dp"
                    android:fontFamily="@string/font_family_regular"
                    android:gravity="center"
                  android:padding="5dp"
                    android:maxLines="1"
                    android:text="product name"
                    android:textColor="@color/black"
                    android:textSize="10sp" />
                <TextView
                    android:id="@+id/product_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:elevation="4dp"
                    android:fontFamily="@string/font_family_regular"
                    android:gravity="center"
                    android:maxLines="1"
                    android:padding="5dp"
                    android:text="product Description"
                    android:textColor="@color/black"
                    android:textSize="10sp" />
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/product_desc"
                    android:layout_gravity="center"
                    android:layout_marginTop="20dp">
                    <TextView
                        android:id="@+id/product_price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentTop="true"
                        android:elevation="4dp"
                        android:text="100$"
                        android:textColor="@color/black"
                        android:textSize="12sp"
                        android:textStyle="bold" />
                    <ImageView
                        android:id="@+id/productshare_button"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_alignParentEnd="true"
                        android:elevation="4dp"
                        android:layout_marginRight="5dp"
                        android:src="@mipmap/symbol_dotss"
                        android:text="product name"
                        android:textColor="@color/black"
                        android:textSize="10sp" />
                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>
        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
android:layout_margin="5dp"
            android:id="@+id/like_image"
            android:layout_alignParentEnd="true"
            android:src="@drawable/dislike" />
    </RelativeLayout>

activty_product.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/rltool"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toolbar1"
            android:background="@color/textColorPrimary"

            app:titleTextColor="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"

            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/AppTheme.Toolbar"
            app:layout_scrollFlags="scroll|enterAlways">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/search1"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"

                android:background="@drawable/background_shape_buttons">



                <!-- <EditText
                     android:id="@+id/searchEditText"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     android:textColorHint="#D6D5C0"
                     android:hint="Search..................."
                     android:layout_gravity="center"
                     android:backgroundTint="#00000000" />-->
                <SearchView
                    android:id="@+id/search"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:textColorHint="#D6D5C0"

                    android:text="@string/search"
                    android:layout_gravity="center"
                    android:iconifiedByDefault="false">

                    <requestFocus />
                </SearchView>

            </LinearLayout>
        </android.support.v7.widget.Toolbar>

        <View
            android:id="@+id/shadow_view"
            android:layout_width="match_parent"
            android:layout_alignBottom="@+id/toolbar1"
            android:visibility="visible"
            android:layout_height="3dp"
            android:background="@drawable/toolbar_shadow" />
</RelativeLayout>
   <GridView
        android:id="@+id/product_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:scrollbars="vertical"

       android:layout_margin="15dp"
        android:horizontalSpacing="10dp"
        android:numColumns="2"

        android:verticalSpacing="10dp">


    </GridView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
raviteja
  • 67
  • 1
  • 13

1 Answers1

4

To avoid the unexpected behaviour , provide the implementations of

@Override
public Object getItem(int position) {
    return mProducts.get(position);
}

@Override
public long getItemId(int position) {
    //return position; or better to provide some unique id 
    // as there is one in your code, product id
    return mProducts.get(position).prod_id;
}

Note : remove notifyDataSetChanged(); in getView because it will trigger the adapter to refresh all view while they are under creating which can cause this behaviour.

Recommended Read

What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • thanks for immediate response....I tried above like u but it doesn't work@Pavneet_Singh – raviteja May 11 '18 at 06:52
  • if there is a possibility that `(mProducts != null) ` is `false` if yes then you will see repeated items , add an else case with default values otherwise the above solution should work for repeated items – Pavneet_Singh May 11 '18 at 07:00
  • but it repeated items from scrolling down to up only (up to down works perfectly).....i can't understand why its happens – raviteja May 11 '18 at 07:20
  • 1
    try adding an `else` case and put a log there and see if it is being triggered and remove `notifyDataSetChanged();` no need and make sure to use about answer's implementation – Pavneet_Singh May 11 '18 at 07:25
  • Ya its worked when i am removing notifyDataSetChanged(); You are awesome bro.....Once again Thank u so much@Pavneet bro – raviteja May 11 '18 at 07:52
  • I am glad that I could help, happy coding , happy coding – Pavneet_Singh May 11 '18 at 07:54