Gridview无法正确滚动

时间:2016-11-17 12:31:10

标签: android gridview

当gridview滚动时,不正确。在滚动时,最后一项是第一项。最后一项和第一项是重叠的。之前我使用过gridview,但它是第一次出现。为什么会这样?

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>

1 个答案:

答案 0 :(得分:0)

 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;

     }