nocmmnt nocmmnt - 21 days ago 6
Android Question

Gridview does not scrolling properly

When gridview is scrolling, does not properly. When it is scrolling, the last item comes first. The last item and the first item are overlaid. I used gridview before but it's coming for the first time. Why is this happening?

public class GridViewAdapter extends BaseAdapter {

private static LayoutInflater mInflater;

private View mView;

private int mPosition;

private ViewGroup mParent;

private Context mContext;

private ArrayList<String> mTexts = new ArrayList<>();

public GridViewAdapter(Context context, ArrayList<String> texts){

mContext = context;

mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

mTexts = texts;

}

@Override
public int getCount() {

return mTexts.size();

}

@Override
public Object getItem(int position) {

return mTexts.get(position);

}

@Override
public long getItemId(int position) {

return position;

}

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

if (convertView == null) {

mView = mInflater.inflate(R.layout.items, parent, false);

mPosition = position;

mParent = parent;

setCategoryName(position);

setAmount();

}

return mView;

}

private void setAmount(){

TextView amount = (TextView) mView.findViewById(R.id.amount);

}

private void setCategoryName(int position){

TextView categoryName = (TextView) mView.findViewById(R.id.category_name);

categoryName.setText(mTexts.get(position));

}
}


items.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<!-- Amount -->

<TextView
android:id="@+id/amount"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/shape_category"
android:text="40"
android:textSize="20dp"

/>

<!-- Category Name -->

<TextView
android:id="@+id/category_name"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:gravity="center"

/>

<!-- Edit category -->

<FrameLayout
android:id="@+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"

>

</FrameLayout>



Answer
 public class GridViewAdapter extends BaseAdapter {

  private Context mContext;

  private ArrayList<String> mTexts = new ArrayList<>();

 public GridViewAdapter(Context context, ArrayList<String> texts){

  mContext = context;


   mTexts = texts;

   }

   @Override
    public int getCount() {

    return mTexts.size();

   }

  @Override
  public Object getItem(int position) {

  return position;

  }

   @Override
   public long getItemId(int position) {

   return position;

   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
   LayoutInflater mInflater = (LayoutInflater)    
  mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

     convertView= mInflater.inflate(R.layout.items, null, false);

     TextView amount = (TextView) convertView.findViewById(R.id.amount);
      TextView categoryName = (TextView)convertView.findViewById(R.id.category_name);

    categoryName.setText(mTexts.get(position));


      }

     return convertView;

     }