1
我有一個5列的網格視圖,我想爲整個行着色,這意味着點擊5個單元格。點擊工作正常,但當我滾動他們點擊顏色設置兩三行後單元格,因爲它更改視圖位置。gridview單元格查看位置更改後滾動...顏色被設置爲不同的單元格,而不是單擊的單元格
這是我的GridView:
<GridView
android:id="@+id/gridView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/CornflowerBlue"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="5"
android:verticalSpacing="5dp">
</GridView>
具有custonm佈局:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/LightBlue"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="22sp" />
和我的gridview的適配器代碼:
public class Fragment2 extends Fragment {
............................
gridView2 = (GridView) getView().findViewById(R.id.gridView2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
getActivity(), R.layout.custom_layout, stg1); //stg1-array
gridView2.setAdapter(adapter2);
gridView2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String message;
int p = (int) Math.ceil(position/5) * 5; //to color 5 cells from starting on click
try{
gridView2.getChildAt(p).setBackgroundColor(Color.GREEN);
gridView2.getChildAt(p+1).setBackgroundColor(Color.GREEN);
gridView2.getChildAt(p+2).setBackgroundColor(Color.GREEN);
gridView2.getChildAt(p+3).setBackgroundColor(Color.GREEN);
gridView2.getChildAt(p+4).setBackgroundColor(Color.GREEN);
}
});
最初,它工作正常,但滾動彩色後被設置爲除點擊之外的其他單元格?我該怎麼辦??我不想使用底座適配器....
downvoter指定原因?? – Shiv 2013-02-21 05:21:58
而不是通過檢查位置來設置顏色,嘗試創建gridview元素列表並根據列表索引來決定,以便滾動時位置不會改變。 – techieWings 2013-02-21 06:11:49
花花公子的點擊事件工作正常,即使我得到單擊項iam的細節,只是說視圖的顏色,即滾動後的單元格視圖chnges位置,所以顏色沒有設置爲適當的單元格......我不明白你是什麼已經寫了??? – Shiv 2013-02-21 06:18:34