2016-01-23 122 views
0

我用100個項目(每個項目包含三個textView)填充我的gridView,顯然它需要滾動。如果我點擊一個項目,它的背景會變成紅色。我的問題是,當我使用滾動時,我看到另一個項目也變成了紅色!爲什麼?滾動GridView並更改項目網格中的背景顏色

在我的活動中,我有String [] subg,String [] dEstancias,String [] limp。 (每個包含100個字符串)。在我的活動:

final CustomGrid adapter = new CustomGrid(this, subg, dEstancias, limp, previousSelectedPosition); 

// Getting a reference to gridview of MainActivity 
final GridView gridView = (GridView) findViewById(R.id.gridview); 

// Setting an adapter containing images to the gridview 
gridView.setAdapter(adapter); 

gridView.setOnItemClickListener(new OnItemClickListener() { 


     public void onItemClick(AdapterView parent, View v,int position, long id) 
     {    

      previousSelectedPosition = position; 
      v.setBackgroundColor(Color.RED); 

     } 

}); 

在我的適配器我getView爲:

public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View grid; 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) { 

      grid = new View(mContext); 
      grid = inflater.inflate(R.layout.grid_item, parent, false); 

     } else { 
      grid = (View) convertView; 
     } 

     TextView textView = (TextView) grid.findViewById(R.id.sgob); 
     TextView textView2 = (TextView) grid.findViewById(R.id.item); 
     TextView textView3 = (TextView) grid.findViewById(R.id.limp); 

     textView.setText(subg[position]); 
     textView2.setText(estancias[position]); 
     textView3.setText(limp[position]); 

     return grid; 
    } 

我的網格項是:

<?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" 
    android:padding="40dp" 
    android:layout_gravity="center" 
    android:background="@android:color/white"> 

    <TextView 
     android:id="@+id/sgob" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:textColor="@android:color/black" 
     android:background="#2cc546" 
     android:gravity="center_horizontal" 
    /> 
    <TextView 
     android:id="@+id/item" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:textColor="@android:color/black" 
     android:background="#2cc546" 
     android:gravity="center_horizontal" 
    /> 

    <TextView 
     android:id="@+id/limp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:textColor="@android:color/black" 
     android:background="#2cc546" 
     android:gravity="center_horizontal" 
    /> 

</LinearLayout> 

回答

0

嘗試使用ViewHolder模式。

編輯getView在適配器這樣

public View getView(int position, View convertView, ViewGroup parent) { 
    final MyViewHolder viewHolder; 

    if (convertView == null) { 
     LayoutInflater layoutInflater = LayoutInflater.from(mContext); 
     convertView = layoutInflater.inflate(R.layout.grid_item, parent, false); 

     viewHolder = new MyViewHolder(); 

     viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.layout); 
     viewHolder.textView = (TextView) convertView.findViewById(R.id.sgob); 
     viewHolder.textView2 = (TextView) convertView.findViewById(R.id.item); 
     viewHolder.textView3 = (TextView) convertView.findViewById(R.id.limp); 

     convertView.setTag(grid); 
    } else { 
     viewHolder = (MyViewHolder) convertView.getTag(); 
    } 

    viewHolder.textView.setText(subg[position]); 
    viewHolder.textView2.setText(estancias[position]); 
    viewHolder.textView3.setText(limp[position]); 

    viewHolder.layout.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      v.setBackgroundColor(Color.RED); 
     } 
    }); 

    return convertView; 
} 

由於背景色是ViewHolder的onClickListener正在改變,你不必在onItemClickListener再次更改背景顏色。

並在您的網格xml文件中添加一個ID爲LinearLayout。 然後創建MyViewHolder.java

public class MyViewHolder { 
    LinearLayout layout; 
    TextView textView; 
    TextView textView2; 
    TextView textView3; 
}