Android网格视图根据数据更改列数

时间:2017-06-14 06:53:00

标签: android gridview android-adapter android-gridview

enter image description here 我想安排网格视图,如下图所示。

5 个答案:

答案 0 :(得分:2)

您需要将RecyclerViewGridLayoutManager一起使用。然后,您可以使用GridLayoutManager.setSpanSizeLookup动态设置列数。

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;
            }
        }
    });

答案 1 :(得分:2)

您可以使用 StaggeredGridLayoutManager

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

有关详细信息,请点击此链接StaggeredGridLayoutManager

例如,请参阅此图片enter image description here

答案 2 :(得分:2)

您可以使用RecyclerView而不是GridView并像这样写

    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);

答案 3 :(得分:2)

您需要在回收站适配器中区分viewType。

<强> DEMO HERE

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);
        }
    }
}

答案 4 :(得分:2)

这个人会工作。试试这个

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);