mercredi 8 avril 2015

GridView in Android doesn't correctly display all their items

I'm trying to implement a Custom GridView with 57 items. But only the first five images are displayed correctly, the rest are repeated to scroll down. And to scroll up images are disoriented.


What I have to do to get the 57 images display correctly? Thank you


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://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
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>

Aucun commentaire:

Enregistrer un commentaire