1

我正在尝试使用 GridView 创建一组图像按钮。我正在使用数组列表来存储图像。每次通过 getView() 方法将图像分配给按钮时,图像数组列表都会减一。

我的问题是我的网格中只显示了一个按钮!我不确定这是否是因为每个按钮调用了多次 getView() 方法。如果是这样,我猜图像数组在分配所有按钮之前就变空了。

任何帮助表示赞赏。这是我的代码

活动主.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:layout_width= 'fill_parent'
android:layout_height= 'fill_parent'
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"    
/>

item_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="35dp"
android:orientation="horizontal" 
>
<ImageButton
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="15dp"
    />
</RelativeLayout>

随机分配给按钮然后从列表中删除的图像:assignImag()e 方法位于 MainActivity 类中,每次从适配器类的 getView() 方法中调用:

public class MainActivity extends Activity {
ArrayList imgs = new ArrayList();
GridView gridView; 
.....
......
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gridView = (GridView) findViewById(R.id.gridView1);
                      gridView.setAdapter(new ButtonAdapter(this));
}


public class ButtonAdapter extends BaseAdapter 
{
    private Context context;
    ImageButton imageButton;


    public ButtonAdapter(Context c) 
    {
        context = c;
    }

    //---returns the number of images---
    public int getCount() {
        return imgs.size();
    }

    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }

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

public View getView(int position, View convertView, ViewGroup parent) 
    {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         View gridView;
            if (convertView == null) {          
            gridView = new View(context);
                gridView = inflater.inflate(R.layout.item_layout, null);
                imageButton = (ImageButton) gridView.findViewById(R.id.button1);
                    // assign image to the button
        assignImage(imageButton);

                    } else {
                        gridView = (View) convertView;
                            }
        return gridView;
    }

} 
}

public void assignImage(ImageButton b){

    Random generator = new Random();
    int index = generator.nextInt(imgs.size());
    b.setContentDescription(imgs.get(index).toString());
    b.setBackgroundResource(imgs.get(index));
                imgs.remove(index);
}
}
4

1 回答 1

0

像这样修改你的适配器:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.img1, R.drawable.img2,
            R.drawable.img3, R.drawable.img4,
            -----
           R.drawable.imgN
    };

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

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

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(67, 55));
        return imageView;
    }

}

无需为 gridView 中的每个图像创建单独的布局

于 2013-04-26T17:13:08.133 回答