我正在创建一个益智游戏,我想拥有一个包含我的拼图图像的GridView。
我看了很多解决方案,但是对于我来说,这些解决方案都没有用。我看不到自己的gridView。
我正在使用ImageAdapter类将拼图块位图放在GridView中,并且我有一个包含所有位图的ArrayList。
在我的主班上,我有这个
GridView gridView = findViewById(R.id.gridPiece);
gridView.setAdapter(new GridViewAdapter(PuzzleActivity.this, R.layout.row_grid, images));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("WORKING");
}
});
我有GridViewAdapter类
@SuppressWarnings("ALL")
public class GridViewAdapter extends ArrayAdapter<Item> {
private Context mContext;
private int layoutResourceId;
private ArrayList<Item> images;
public GridViewAdapter(Context c, int layoutResourceId, ArrayList<Item> images) {
super(c, layoutResourceId, images);
mContext = c;
this.layoutResourceId = layoutResourceId;
this.images = images;
}
@Override
public int getCount() {
return 0;
}
@Override
public Item getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
Item item = images.get(position);
holder.imageItem.setImageBitmap(item.getImage());
return row;
}
static class RecordHolder {
TextView imageTitle;
ImageView imageItem;
}
}
我的gridView
<GridView
android:id="@+id/gridPiece"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:horizontalSpacing="10dp"
android:numColumns="1"
android:paddingHorizontal="10dp"
android:paddingVertical="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
tools:ignore="MissingConstraints,UnusedAttribute"
tools:layout_editor_absoluteY="0dp" />
以及我的gridView中的每个图像
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/item_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="10dp"
tools:ignore="ContentDescription,RtlHardcoded">
</ImageView>
</LinearLayout>
如果有人可以帮助我,将非常感谢,谢谢
答案 0 :(得分:0)
您应该返回这样的图像计数,而不是为getCount返回零:
@Override
public int getCount() {
return this.images.size();
}
,在getItem()函数中,您应该返回该项目而不是null:
@Override
public Item getItem(int position) {
return this.images.get(position);
}