我RecyclerView
显示另一个全高RecyclerView
个项目。我需要在内部RecylerView
适配器中插入一个新项。我在顶级RecyclerView
的适配器中编写了一个方法:
public void notifyInnerItemInserted(final int position, final int insertPosition) {
ViewHolder holder = (ViewHolder)mRecyclerView.findViewHolderForAdapterPosition(position);
if(holder != null) {
Day day = records.get(position);
holder.mAdapter.clear();
holder.mAdapter.addAll(day.getItems());
holder.mAdapter.notifyItemInserted(insertPosition);
}
}
但它不起作用。即使我尝试更新顶级的整个项目:dayAdapter.notifyDataItemInserted(position);
它也不会全部工作。看起来RecyclerView
拥有旧的可绘制缓存。下面是顶级适配器的完整代码:
public class DayAdapter extends RecyclerView.Adapter<DayAdapter.ViewHolder> {
private RecyclerView mRecyclerView;
public DayAdapter(RecyclerView recyclerView) {
mRecyclerView = recyclerView;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.li_day, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final Day day = records.get(position);
List<Record> items = day.mItems;
if(items == null || items.size() == 0) {
holder.mContent.setVisibility(View.GONE);
} else {
holder.mContent.setVisibility(View.VISIBLE);
holder.mAdapter.clear();
holder.mAdapter.addAll(day.getItems());
holder.mAdapter.notifyDataSetChanged();
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
private RecyclerView mContent;
private Adapter mAdapter = new Adapter();
public ViewHolder(final View itemView) {
super(itemView);
mContent = (RecyclerView) itemView.findViewById(R.id.rvContent);
mContent.setNestedScrollingEnabled(false);
FullExpandedListManager lm = new FullExpandedListManager(mContent.getContext());
SimpleItemDecoration decoration = new SimpleItemDecoration(mContent.getContext(),
SimpleItemDecoration.VERTICAL_LIST);
mContent.setLayoutManager(lm);
mContent.addItemDecoration(decoration);
mContent.setAdapter(mAdapter);
}
}
public static class Day extends BaseAdapter.Record<Day> {
private List<Record> mItems;
public List<Record> getItems() {
return mItems;
}
public Day setItems(List<Record> items) {
mItems = items;
return this;
}
}
public void notifyInnerItemInserted(final int position, final int insertPosition) {
ViewHolder holder = (ViewHolder)mRecyclerView.findViewHolderForAdapterPosition(position);
if(holder != null) {
holder.mAdapter.notifyItemInserted(insertPosition);
}
}
}
我做错了什么?如何解决我的解决方案?