tablas studio programacion móviles gridlayout dinamicas desarrollo curso aplicaciones android gridview android-adapter android-gridview

studio - programacion android pdf 2018



Vista de cuadrícula de Android Cambia el número de columnas según los datos (5)

Quiero organizar la vista de cuadrícula como la imagen de abajo.


Este funcionará. Prueba esto

RecyclerView RecyclerView=RecyclerView)findViewById(R.id.card_recycler_view); GridLayoutManager glm = new GridLayoutManager(getActivity(), 4); glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { switch(position%3) { case 0: return 4; break; case 1: return 2; break; case 2: return 2; break; } } }); mRecyclerView.setLayoutManager(glm);


Necesita espaciar viewType en el adaptador de reciclador.

DEMO AQUÍ

public class RVStatisticAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<Statistic> mList; public RVStatisticAdapter(List<Statistic> list) { this.mList = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view; switch (viewType) { case CITY_TYPE: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.statistic_row_one, parent, false); return new CityViewHolder(view); case EVENT_TYPE: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.statistic_row_two, parent, false); return new EventViewHolder(view); } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { Statistic object = mList.get(position); if (object != null) { switch (object.getType()) { case CITY_TYPE: ((CityViewHolder) holder).mTitle.setText(object.getTitle()); ((CityViewHolder) holder).no.setText(object.getNo()); ((CityViewHolder) holder).playerone.setText(object.getPlayer_one()); break; case EVENT_TYPE: ((EventViewHolder) holder).mTitle.setText(object.getTitle()); ((EventViewHolder) holder).no.setText(object.getNo()); ((EventViewHolder) holder).playerone.setText(object.getName()); ((EventViewHolder) holder).playertwo.setText(object.getPlayer_two()); break; } } } @Override public int getItemCount() { if (mList == null) return 0; return mList.size(); } @Override public int getItemViewType(int position) { if (mList != null) { Statistic object = mList.get(position); if (object != null) { return object.getType(); } } return 0; } public static class CityViewHolder extends RecyclerView.ViewHolder { private TextView mTitle,no,playerone; public CityViewHolder(View itemView) { super(itemView); mTitle = (TextView) itemView.findViewById(R.id.tv_title); no = (TextView) itemView.findViewById(R.id.tv_no); playerone = (TextView) itemView.findViewById(R.id.tv_player_one); } } public static class EventViewHolder extends RecyclerView.ViewHolder { private TextView mTitle,no,playerone,playertwo; public EventViewHolder(View itemView) { super(itemView); mTitle = (TextView) itemView.findViewById(R.id.tv_title); no = (TextView) itemView.findViewById(R.id.tv_no); playerone = (TextView) itemView.findViewById(R.id.tv_player_one); playertwo = (TextView) itemView.findViewById(R.id.tv_player_two); } } }


Necesitas usar RecyclerView con GridLayoutManager para eso. Luego puede usar GridLayoutManager.setSpanSizeLookup para establecer dinámicamente el conteo de columnas.

gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { switch (adapter.getItemViewType(position)) { case VIEW_TYPE_1: return 1; case VIEW_TYPE_2: return 2; default: return -1; } } });


Puede tomar RecyclerView en lugar de GridView y escribir de esta manera

RecyclerView mRecyclerView=(RecyclerView)findViewById(R.id.card_recycler_view); GridLayoutManager glm = new GridLayoutManager(getActivity(), 4); glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { if (position % 3 == 2) { return 4; } else { return 2; } } }); mRecyclerView.setLayoutManager(glm);


puedes usar StaggeredGridLayoutManager

private StaggeredGridLayoutManager gaggeredGridLayoutManager; gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1); recyclerView.setLayoutManager(gaggeredGridLayoutManager);

para más información siga este enlace StaggeredGridLayoutManager

por ejemplo, ver esta imagen