动态将组件添加到RecyclerView [英] Adding components dynamically to RecyclerView

查看:233
本文介绍了动态将组件添加到RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm displaying daily events. The number of events/day is variable. Each item in the RecView is a Day which should contain as many views as the number of events.

这是一项的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/llDay">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvDay"
        android:text="TODAY"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:textSize="25sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvNothing"
        android:text="No events"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:textSize="18sp"
        android:textStyle="italic"
        android:visibility="gone"/>

</LinearLayout>

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

I'd like to add views to the llDay dynamically. Here is my Adapter:

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

    public DiaryAdapter(ArrayList<ArrayList<Displayable>> 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<Displayable> 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<Displayable> 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时,事件将正确显示,但滚动后会多次显示一些事件.我知道问题是通过在onBindViewHolder(...)中调用addLayouts(...)引起的,但是我不知道如何为每天创建正确的视图量.

When the RecyclerView is displayed first, the events are displayed correctly, but after scrolling some events are displayed multiple times. I know that the problem is caussed by calling the addLayouts(...) in the onBindViewHolder(...) but I don't know how to create the correct amount of views for each day.

更新:添加了ViewHolder:

UPDATE: ViewHolder added:

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

推荐答案

好吧,终于知道了. 首先将您的布局更改为:

Ok finally figured it out. Firstly change your layout to:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/llDay">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvDay"
        android:text="TODAY"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:textSize="25sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvNothing"
        android:text="No events"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:textSize="18sp"
        android:textStyle="italic"
        android:visibility="gone"/>
 <LinearLayout 
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/llDay1"/>
</LinearLayout>

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

now in your view holder add another LinearLayout parameter:

LinearLayout llDay,llday1;

并在addLayouts方法内部进行更改:

and inside the addLayouts method change:

 viewHolder.llDay.addView(teachingDiaryLayout);

 viewHolder.llDay1.addView(teachingDiaryLayout);

也在for循环之前添加

Also before the for loop add

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

这篇关于动态将组件添加到RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆