2016-11-17 123 views
-4

當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> 

+0

顯示適配器代碼 – Sanoop

+0

你在GridView中顯示? itemview中有沒有可點擊的內容? –

+0

我沒有使用任何可點擊的視圖,我編輯items.xml – nocmmnt

回答

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; 

    } 
+0

好的,謝謝。這是作品。但我不明白,爲什麼mView不起作用? – nocmmnt

+0

2件事情..你已經使用靜態的inflater。並且在getitem()方法中,您只需返回position.not項。 –

+0

非常感謝,Rushi Ayyappa。 – nocmmnt