想要在recyclerview中重复项目

时间:2017-11-28 11:58:51

标签: android android-layout android-recyclerview recyclerview-layout

我想在recyclerview中重复这些项目。搜索后我找不到任何污垢。我想在完成加载后从第一个开始再次启动项目。我已经完成了在recyclerview中获取数据的简单编码。

     public class DateAdapter extends RecyclerView.Adapter<DateAdapter.DateViewHolder> {
        private ArrayList<LabelerDate> dateDataList;


        private static final int VIEW_TYPE_PADDING = 1;
        private static final int VIEW_TYPE_ITEM = 2;
        private int paddingHeightDate = 0;

        private int selectedItem = -1;

        public DateAdapter(ArrayList<LabelerDate> dateData, int paddingHeightDate) {
            this.dateDataList = dateData;
            this.paddingHeightDate = paddingHeightDate;

        }


        @Override
        public DateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == VIEW_TYPE_ITEM) {
                final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,
                        parent, false);
                return new DateViewHolder(view);
            } else {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,
                        parent, false);

                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();
                layoutParams.height = paddingHeightDate;
                view.setLayoutParams(layoutParams);
                return new DateViewHolder(view);
            }
        }

        @Override
        public void onBindViewHolder(DateViewHolder holder, int position) {
            LabelerDate labelerDate = dateDataList.get(position);
            int count = dateDataList.size();
            int middleRowIndex = (int)count/2 ;
//            holder.tvDate.setTextSize(TypedValue.COMPLEX_UNIT_SP,(int)(count * middleRowIndex));
//            int prevIndex = middleRowIndex - 1;
//            int nextIndex = middleRowIndex + 1;

            if (getItemViewType(position) == VIEW_TYPE_ITEM) {
                holder.tvDate.setText(labelerDate.getNumber());
                holder.tvDate.setVisibility(View.VISIBLE);

                Log.d(TAG, "default " + position + ", selected " + selectedItem);
                if (position == selectedItem) {
                    Log.d(TAG, "center" + position);
                    holder.tvDate.setTextColor(Color.parseColor("#76FF03"));
                    holder.tvDate.setTextSize(35);

                } else {
                    holder.tvDate.setTextColor(Color.BLACK);
                    holder.tvDate.setTextSize(18);
                }
            } else {
                holder.tvDate.setVisibility(View.INVISIBLE);
            }
        }

        public void setSelecteditem(int selecteditem) {
            this.selectedItem = selecteditem;
            notifyDataSetChanged();
        }

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

        @Override
        public int getItemViewType(int position) {
            LabelerDate labelerDate = dateDataList.get(position);
            if (labelerDate.getType() == VIEW_TYPE_PADDING) {
                return VIEW_TYPE_PADDING;
            } else {
                return VIEW_TYPE_ITEM;
            }

        }


        public class DateViewHolder extends RecyclerView.ViewHolder {
            public TextView tvDate;

            public DateViewHolder(View itemView) {
                super(itemView);
                tvDate = (TextView) itemView.findViewById(R.id.tv_year);
            }
        }
    }

我想实现如下

  

item1 item2 item3 item4 item5 item1 item2 item3 item4 item5

2 个答案:

答案 0 :(得分:1)

以下方法将帮助您将列表项填充到多个原始数据项中。

  

在你getItemCount()方法

@Override
public int getItemCount() {
    return dateDataList.size() * MULTIPLIER; //MULTIPLIER is the number of times you want your data to repeat
}
  

在你onBindViewHolder()方法

@Override
public void onBindViewHolder(DateViewHolder holder, int position) {
    LabelerDate labelerDate = dateDataList.get(position%dateDataList.size());
}

答案 1 :(得分:0)

 private ArrayList<LabelerDate> dateDataList;

这是您要在回收商上展示的商品的来源。当你填充所述列表时,请确保重复该过程,以便重复项目。