我正在尝试使用57个项目实现Custom GridView
。但只有前五个图像正确显示,其余图像重复向下滚动。向上滚动图像是迷失方向的。
我需要做些什么才能正确显示57张图像?谢谢
Main.java
public static int [] imagenesIngredientes = {
R.drawable.rodilla_elige_ing_1,
R.drawable.ingre_1,
R.drawable.ingre_2,
R.drawable.ingre_3,
R.drawable.rodilla_elige_ing_2,
R.drawable.ingre_4,
R.drawable.ingre_5,
R.drawable.ingre_6,
R.drawable.ingre_7,
R.drawable.ingre_8
...};
gridViewFotos.setNumColumns(2);
gridViewFotos.setHorizontalSpacing(10);
gridViewFotos.setVerticalSpacing(10);
GridViewImageAdapter adapter = new GridViewImageAdapter(this, imagenesIngredientes);
gridViewFotos.setAdapter(adapter);
Adapter.java
private Activity context;
private int[] imagenes;
public GridViewImageAdapter(Activity context, int[] imagenes) {
super();
this.context = context;
this.imagenes = imagenes;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final RecordHolder holder;
if (convertView == null) {
view = context.getLayoutInflater().inflate(R.layout.gridview_row, null);
holder = new RecordHolder();
holder.imageItem = (ImageView) view.findViewById(R.id.participar_gridview_imagen);
holder.imageItem.setImageResource(imagenes[position]);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(Utils.anchoPantalla(context) / 2, Utils.anchoPantalla(context) / 2);
holder.imageItem.setLayoutParams(params);
view.setTag(holder);
} else {
holder = (RecordHolder) view.getTag();
}
return view;
}
private class RecordHolder {
ImageView imageItem;
}
public int getCount() {
return imagenes.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
main.xml中
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<GridView
android:id="@+id/participar_gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:listSelector="#FFFFFFFF" >
</GridView>
</RelativeLayout>
row.xml
<RelativeLayout 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">
<ImageView
android:id="@+id/participar_gridview_imagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
</RelativeLayout>
答案 0 :(得分:0)
将图像绑定代码移出if else条件,如下所示
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final RecordHolder holder;
if (convertView == null) {
view = context.getLayoutInflater().inflate(R.layout.gridview_row, null);
holder = new RecordHolder();
holder.imageItem = (ImageView) view.findViewById(R.id.participar_gridview_imagen);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(Utils.anchoPantalla(context) / 2, Utils.anchoPantalla(context) / 2);
holder.imageItem.setLayoutParams(params);
view.setTag(holder);
} else {
holder = (RecordHolder) view.getTag();
}
holder.imageItem.setImageResource(imagenes[position]);
return view;
}