簡體   English   中英

嵌套適配器的Android notifyDataSetChanged不起作用

[英]Android notifyDataSetChanged for nested Adapter doesn't work

在我的SectionsListAdapter適配器中,我嵌套了名為MonthLessonsList Adapter ,在從Web服務獲取日期並設置了新數據並為適配器調用notifyDataSetChanged方法之后,主適配器為SectionsListAdapter可以刷新,但是嵌套的適配器不刷新, notifyDataSetChanged調用了notifyDataSetChanged后,不會刷新無法正常工作且不顯示新數據

Fragment調用並設置新數據:

monthSectionsItems = SQLite.select().from(MonthSections.class).queryList();
adapter.setData(monthSectionsItems);

和我的帶嵌套適配器的適配器:

public class SectionsListAdapter extends RecyclerView.Adapter<SectionsListAdapter.MyViewHolder> {

    private final OnItemSelected listener;
    private List<MonthSections> list;
    private Context context;
    private MonthLessonsList lessonsListAdapter;

    public SectionsListAdapter(List<MonthSections> followingsList, Context mContext, OnItemSelected listener) {
        this.list = followingsList;
        this.context = mContext;
        this.listener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.sections_list_row, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        holder.indicatorIcon.setText(list.get(position).getSection_month_name());
        ...

        List<SectionLesson> lessonsList = list.get(position).getLessons();
        lessonsListAdapter = new MonthLessonsList(lessonsList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
        holder.userChildCategories.setLayoutManager(mLayoutManager);
        holder.userChildCategories.setAdapter(lessonsListAdapter);

        ...
    }

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

    public void setData(List<MonthSections> data) {
        list.clear();
        list.addAll(data);
        notifyDataSetChanged();
        lessonsListAdapter.notifyDataSetChanged();
    }

    public List<MonthSections> getData() {
        return list;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ...

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

            section_title.setGravity(Gravity.RIGHT);
        }
    }

    public class MonthLessonsList extends RecyclerView.Adapter<MonthLessonsList.LessonsViewHolder> {
        private List<SectionLesson> lessonItem;

        public class LessonsViewHolder extends RecyclerView.ViewHolder {
            public TextView title;

            public LessonsViewHolder(View view) {
                super(view);
                title = view.findViewById(R.id.title);
            }
        }

        public MonthLessonsList(List<SectionLesson> lists) {
            this.lessonItem = lists;
        }

        @Override
        public LessonsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.child_list_row, parent, false);

            return new LessonsViewHolder(itemView);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(@NonNull LessonsViewHolder holder, int position) {
            SectionLesson lesson = lessonItem.get(position);
            holder.title.setText("درس: " + (position + 1) + ") " + lesson.getTitle());
        }

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

}

onBindViewHolder聲明子適配器,並從全局級別刪除。 並且無需通知子適配器。 因為這將在調用父onBindViewHolder時自動填充。 喜歡

MonthLessonsList lessonsListAdapter = new MonthLessonsList(lessonsList);
holder.userChildCategories.setAdapter(lessonsListAdapter);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM