动态添加组件到RecyclerView

时间:2015-12-29 19:45:54

标签: android android-custom-view android-recyclerview recycler-adapter

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

这是一个项目的布局:

<?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添加视图。 这是我的适配器:

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时,事件会正确显示,但滚动后会显示多次事件。我知道问题是通过致电addLayouts(...)中的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);
        }
    }

Duplicated views

1 个答案:

答案 0 :(得分:6)

好吧终于明白了。 首先将您的布局更改为:

  <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参数:

LinearLayout llDay,llday1;

并在addLayouts方法内部更改:

 viewHolder.llDay.addView(teachingDiaryLayout);

 viewHolder.llDay1.addView(teachingDiaryLayout);

在for循环之前添加

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