私は 5 列の girdview を持っており、クリックすると 5 つのセルを意味する行全体に色を付けたいと考えています。クリックは正常に機能しますが、クリックしてスクロールすると、ビューの位置が変わるため、2行または3行後のセルに色が設定されます。
これは私のグリッドビューです:
<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>
カスタムレイアウトがあります:
<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);
}
});
最初は正常に動作しますが、スクロールした後、クリックしたセル以外のセルに色が設定されますか?? 私は何をすべきか??私はベースアダプターを使いたくありません....