/ / GridView con diferente ancho de columna en filas - Android, gridview

GridView con diferente ancho de columna en filas - Android, gridview

Tengo un GridView en el que necesito mostrar imágenes. He aplicado debajo de la lógica:

If 1 Photo : 1 row and 1 col and full width image.
If 2 Photos : 1 row and 2 cols with equal width of both images (half of screen width)
If 3 photos: 2 rows. 1st row will be having 1 col (1 image with full width)
and 2nd row will be having 2 images with equal width (half of screen width)

If 4 or more photos : width will be half for each column.

He conseguido para el resto de los casos, el únicoEl problema es configurar para el caso 3 cuando tengo 3 fotos. Quiero 1 foto de ancho completo en 2 filas y 2 fotos de ancho medio igual en la segunda fila, pero mi código me da 2 fotos de ancho medio igual en la fila 1 y una foto de ancho medio en la fila 2.

Necesito usar Gridview solamente, por favor dígame si esto puede ser posible usando Gridview. Ya hice esta pregunta pero no obtuve ninguna respuesta. Por favor, ayúdame si tienes alguna idea aquí.

Muchas gracias.

Quiero tener el siguiente diseño:

enter image description here

Respuestas

7 para la respuesta № 1

Ver codigo abajo

He creado demo y funciona bien.

  rv = (RecyclerView) findViewById(R.id.rv);
final MyAdapter adapter = new MyAdapter();
rv.setAdapter(adapter);
GridLayoutManager mLayoutManager = new GridLayoutManager(this, 2);
rv.setLayoutManager(mLayoutManager);

mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (adapter.getItemCount() == 1) {
return 2;
} else if (adapter.getItemCount() == 2) {
return 1;
} else if (adapter.getItemCount() == 3) {
if (position == 0) {
return 2;
} else {
return 1;
}
} else {

return 1;

}


}
});