在我的SectionsListAdapter
适配器中,我嵌套了名为MonthLessonsList
的Adapter
,在从web服务获取数据并设置新数据并为适配器调用notifyDataSetChanged
方法后,主适配器(如SectionsListAdapter
)可以刷新,但嵌套适配器不会刷新,并且在为此调用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();
}
}
}
2条答案
按热度按时间z4iuyo4d1#
decompose
onBindViewHolder
内部的子适配器,并从全局级别删除。而且不需要通知子适配器。因为当父onBindViewHolder
被调用时,它会自动填充。像iyfamqjs2#
假设你有两个适配器AdapterA和AdapterB,现在AdapterB在AdapterA中设置,你需要更新AdapterB中的数据,只需在AdapterA中创建一个方法,如下所示:
现在从你的Activity/Fragment调用这个方法,并给予AdapterA和AdapterB的列表,这将改变你在Nested ClusterView中的数据。