当前位置:  开发笔记 > 编程语言 > 正文

将组件动态添加到RecyclerView

如何解决《将组件动态添加到RecyclerView》经验,为你挑选了1个好方法。

我正在展示日常活动.事件/日数是可变的.RecView中的每个项目都是一个日期,其中应包含与事件数量一样多的视图.

这是一个项目的布局:




    

    


我想动态地向llDay添加视图.这是我的适配器:

public class DiaryAdapter extends RecyclerView.Adapter{
    ArrayList> diaryEvents = new ArrayList<>(); 
    Context context;

    public DiaryAdapter(ArrayList> diaryEvents, Context context) {
        this.diaryEvents = diaryEvents;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_day_diary, parent, false);
        DiaryDayViewHolder viewHolder = new DiaryDayViewHolder(viewGroup);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        DiaryDayViewHolder viewHolder = (DiaryDayViewHolder) holder;
        ArrayList events = diaryEvents.get(position);

        if (events.size() > 0){
            addLayouts(events, viewHolder);
        }
        else{
            viewHolder.tvNothing.setVisibility(View.VISIBLE);
        }
    }

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

    private void addLayouts(ArrayList events, DiaryDayViewHolder viewHolder) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        for (Displayable event : events) {
            switch (event.getEventType()){
                case Types.TYPE_TEACHING:
                    TeachingDiaryLayout teachingDiaryLayout = new TeachingDiaryLayout(context);
                    teachingDiaryLayout.setLayoutParams(params);
                    teachingDiaryLayout.setViews((Teaching) event);
                    viewHolder.llDay.addView(teachingDiaryLayout);
                    viewHolder.tvDay.setText("DAY"); // TODO: day + date

                    break;
                case Types.TYPE_TASK: // TODO
                    break;
                case Types.TYPE_EXAM: // TODO
                    break;
            }
        }
    }
}

首次显示RecyclerView时,事件会正确显示,但滚动后会显示多次事件.我知道这个问题是通过调用addLayouts(...)in来引起的,onBindViewHolder(...)但我不知道如何为每天创建正确数量的视图.

更新:ViewHolder添加:

public static class DiaryDayViewHolder extends RecyclerView.ViewHolder {
        LinearLayout llDay;
        TextView tvDay;
        TextView tvNothing;

        public DiaryDayViewHolder(View itemView) {
            super(itemView);

            llDay = (LinearLayout) itemView.findViewById(R.id.llDay);
            tvDay = (TextView) itemView.findViewById(R.id.tvDay);
            tvNothing = (TextView) itemView.findViewById(R.id.tvNothing);
        }
    }

重复的视图



1> Rohit Ramkum..:

好吧终于想通了.首先将您的布局更改为:

  

    

    
 

现在在您的视图中添加另一个LinearLayout参数:

LinearLayout llDay,llday1;

并在addLayouts方法内部更改:

 viewHolder.llDay.addView(teachingDiaryLayout);

 viewHolder.llDay1.addView(teachingDiaryLayout);

也是在for循环添加之前

     viewHolder.llDay1.removeAllViews();
for (Displayable event : events)

推荐阅读
ar_wen2402851455
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有