0

GridViewの特定のアイテムの背景色を(位置ごとに)変更したい。

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }


    parent.getChildAt(1).setBackgroundColor(Color.RED);

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

動作しません。

OnClickListenerで使用すると、次のように機能します。

public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
    view.setBackgroundResource(android.R.drawable.btn_default);
}

でもクリックせずに変更したいです。

4

2 に答える 2

1

parent.getChildAt(1).setBackgroundColor(Color.RED);試す代わりに

if(position==1){ // item's position that you want to change background color
    [VIEW_YOU_WANT_TO_CHANGE_BACKGROUND].setBackgroundColor(Color.RED);
}else{
    // Set other item's background color to default background color you want
    [VIEW_YOU_WANT_TO_CHANGE_BACKGROUND].setBackgroundColor(Color.[WHAT_COLOR_YOU_WANT]);
}

お役に立てれば

于 2013-03-24T21:52:50.367 に答える
0

のすべての位置で、aViewを子で膨らませることができます。次に、アイテム全体の背景を設定します。ImageViewgetView()View

public View getView(int position, View convertView, ViewGroup parent) {
        final ImageView imageView;
        View v = null;
        if (convertView == null) {
            v = getLayoutInflater().inflate(R.layout.item_grid, parent, false);
            imageView = (ImageView) v.findViewById(R.id.image);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
            v.setBackground(R.drawable.whateverBackground)
        } else {
            v = convertView;
        }
        return v;
}

このようにitem_grid.xml見えます

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="120dip"
    android:adjustViewBounds="true"
    android:contentDescription="@string/descr_image"
    android:scaleType="centerCrop" />
于 2013-03-24T21:15:02.010 に答える