如何在recyclerview的2个数据之间插入子标题?

时间:2019-03-25 15:24:31

标签: android-recyclerview

我正在做这样的事情

https://imgur.com/mqiNBoP.jpg

如您所见,有2个recyclerview彼此相邻放置,但这不是重点

要点是这个

https://imgur.com/DVbAlAa.jpg

您会看到有10个序列以json格式显示(不要怪我为什么以文本形式显示它们,我是懒惰的)

但是相应的recyclerview仅显示其中的9个,丢失了第一个数据(左侧的数据)

我认为这应该是我的getItemViewType的问题,但我不知道出了什么问题

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;

    private ArrayList<Train> train_datas;

    public RecyclerViewAdapter(Context context, ArrayList<Train> train_datas) {
        this.context = context;
        this.train_datas = train_datas;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());

        if(viewType == 1) {
            View view = layoutInflater.inflate(R.layout.train_header, parent,false);
            return new ViewHolderHeader(view);
        }
        else {
            View view = layoutInflater.inflate(R.layout.train_cards, parent,false);
            return new ViewHolderContent(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Train train = train_datas.get(position);

        if (holder.getItemViewType() == 1) {
            ViewHolderHeader holderHeader = (ViewHolderHeader) holder;

            holderHeader.tvStation.setText(Utils.getStationName(train.getStation()));
            return;
        }

        else {
            ViewHolderContent holderContent = (ViewHolderContent) holder;

            // Some complicate mechenics here

            return;
        }
    }

    @Override
    public int getItemCount() {
        return train_datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        int type = Integer.valueOf(train_datas.get(position).getSeq());

        return type;
    }

    public class ViewHolderContent extends RecyclerView.ViewHolder {
        public TextView tvSequence;
        public TextView tvTime;
        public TextView tvDest;
        public ImageView platImg;
        public TextView tvTtnt;

        public CardView cardView;

        public ViewHolderContent(View itemView) {
            super(itemView);
            tvSequence = itemView.findViewById(R.id.tvSequence);
            tvTime = itemView.findViewById(R.id.tvTime);
            tvDest = itemView.findViewById(R.id.tvDest);
            platImg = itemView.findViewById(R.id.platImg);
            tvTtnt = itemView.findViewById(R.id.tvTtnt);

            cardView = itemView.findViewById(R.id.cardView);
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Toast.makeText(v.getContext(), "click " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
                    return false;
                }
            });
        }
    }

    public class ViewHolderHeader extends RecyclerView.ViewHolder {
        public TextView tvStation;

        public ViewHolderHeader(View itemView) {
            super(itemView);
            tvStation = itemView.findViewById(R.id.tvStation);
        }
    }
}

所以我要问的是,如何添加标头以及我的所有数据(其中有10个)出现?

非常感谢您

编辑:我目前了解的是,我正在尝试在“ getItemViewType()== 1”时给标头xml充气,但由于标头xml占用了第一个数据,所以我无法显示它,但是我还是不知道怎么解决

0 个答案:

没有答案