其他分享
首页 > 其他分享> > Android网格视图根据数据更改列数

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

作者:互联网

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

解决方法:

您需要在回收器适配器中区分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);
        }
    }
}

标签:android,gridview,android-adapter,android-gridview
来源: https://codeday.me/bug/20190717/1486934.html