按项目位置在GridView中设置背景

时间:2013-03-24 12:11:30

标签: android gridview view background

我想更改GridView中特定项目的背景颜色(按位置)。

gridview.getChildAt(1).setBackgroundResource(android.R.drawable.btn_default);

不起作用。

如果我在OnClickListener中使用它,它可以工作:

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

但我想在没有点击的情况下进行更改。


修改

ImageAdapter:

public class ImageAdapter extends BaseAdapter {
private final String TAG = getClass().toString();

private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

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;
}

活动:

final GridView gridview = (GridView) view.findViewById(R.id.gridView);
gridview.setAdapter(new ImageAdapter(MainActivity.this));
gridview.setSelection(0);
    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    View previous = null;
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            if(previous == view) {
                view.setBackgroundResource(android.R.drawable.btn_default);
            } else {
                view.setBackgroundResource(android.R.drawable.btn_default);
                if(previous != null) previous.setBackgroundResource(0);
                previous =  view;
            }

        }
    });

XML:

<GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:numColumns="4" >
</GridView>

3 个答案:

答案 0 :(得分:1)

getView()中使用此,看看你的背景是否有所改变:

gridView.getChildAt(position).setBackgroundColor(Color.RED);

不要忘记@Override上使用getView()

@Override
public View getView(int position, View convertView, ViewGroup parent) {

}

答案 1 :(得分:1)

试试这个

@Override
    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;
        }


        if(position==1)
             imageview.setBackgroundColor(Color.RED);

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

答案 2 :(得分:1)

您可以将setBackground用于项目适配器。

试试这个。 item_image.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_item_selection">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp" />
</LinearLayout>

bg_item_selection.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@color/white_color" android:state_pressed="false"/>
<item android:drawable="@color/black"/>

在适配器中。

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ItemUserEntity itemUserEntity = new ItemUserEntity();
    itemUserEntity = arrayList.get(position);
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {

        convertView = inflater.inflate(R.layout.item_image, parent,
                false);
    }


    return convertView;

}