我为两个recyclerview嵌套了适配器,就像这样:
Parent Adapter:
public class ParentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static ArrayList<ParentItem> parentItemList;
private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
private com.example.todotestapp.ChildAdapter childAdapter;
public ParentAdapter(ArrayList<ParentItem> parentItemList) {
this.parentItemList = parentItemList;
}
public class ParentViewHolder extends RecyclerView.ViewHolder {
private RecyclerView childRecyclerView;
public ParentViewHolder(final View parentView) {
super(parentView);
childRecyclerView = parentView.findViewById(R.id.child_recyclerview);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View parentView = inflater.inflate(R.layout.parent_layout, parent, false);
return new ParentViewHolder(parentView);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final ParentItem someParentItem = parentItemList.get(position);
ArrayList<ChildItem> childItemList = someParentItem.getChildItemList();
RecyclerView childRecyclerView = ((ParentViewHolder) holder).childRecyclerView;
LinearLayoutManager layoutManager = new LinearLayoutManager(childRecyclerView.getContext());
layoutManager.setInitialPrefetchItemCount(childItemList.size());
childAdapter = new com.example.todotestapp.ChildAdapter(childItemList);
childRecyclerView.setLayoutManager(layoutManager);
childRecyclerView.setAdapter(childAdapter);
childRecyclerView.setRecycledViewPool(viewPool);
}
@Override
public int getItemCount() {return parentItemList.size(); }
}
子适配器:
public class ChildAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<ChildItem> childItemList;
public ChildAdapter(ArrayList<ChildItem> childItemList) {
this.childItemList = childItemList;
}
public class ChildViewHolder extends RecyclerView.ViewHolder {
CheckBox childCheckBox;
public ChildViewHolder(final View childView) {
super(childView);
childCheckBox = childView.findViewById(R.id.child_checkbox);
childCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ChildItem checkedChildItem = childItemList.get(getAdapterPosition());
ParentItem parentItemOfCheckedChildItem = checkedChildItem.getParentItem();
int parentAdapterPos = ParentAdapter.parentItemList.indexOf(parentItemOfCheckedChildItem);
int mainPos = MainActivity.mainParentItemList.indexOf(parentItemOfCheckedChildItem);
ArrayList<ChildItem> aChildItemList = parentItemOfCheckedChildItem.getChildItemList();
aChildItemList.remove(checkedChildItem);
childItemList.remove(checkedChildItem);
parentItemOfCheckedChildItem.setChildItemList(aChildItemList);
ParentAdapter.parentItemList.set(parentAdapterPos, parentItemOfCheckedChildItem);
MainActivity.mainParentItemList.set(mainPos, parentItemOfCheckedChildItem);
MainActivity.mainChildItemList.remove(checkedChildItem);
checkedChildItem.setIsChecked(true);
MainActivity.mainChildItemList.add(checkedChildItem);
ParentItem checkedParentItem = ParentAdapter.parentItemList.get(ParentAdapter.parentItemList.size() - 1);
checkedParentItem.addToChildItemList(checkedChildItem);
ParentAdapter.parentItemList.set(ParentAdapter.parentItemList.size() - 1, checkedParentItem);
notifyDataSetChanged();
}
});
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View childView = inflater.inflate(R.layout.child_layout, parent, false);
return new ChildViewHolder(childView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
final ChildItem aChildItem = childItemList.get(position);
}
@Override
public int getItemCount() {return childItemList.size();}
}
我知道ChildAdapter很乱,但是我在处理数据时遇到了麻烦。我想从当前的ParentItem中删除checkedChildItem,将其添加到新的ParentItem中,然后刷新数据。我当前的代码执行前两个步骤(从原始ParentItem的childItemList中删除ChildItem,并将其添加到新的ParentItem中),但仅在刷新活动后显示数据。
我已经尝试将数据传递给ParentAdapter "updateDataAfterChecked"方法,但我仍然无法更新它,因为ChildItemViewHolder是一个静态上下文和notifyDataSetChanged()不是,所以我似乎找不到一种方法来更新ParentAdapter,而不去一个不同的活动和回来。有什么建议吗?
------------------------------------EDIT----------------------------------------
我能够访问。notifydatasetchanged()利用LiveData的ParentItemList,但ChildItem仍然没有出现在第二个ParentItem,直到页面刷新(如果我调用finish()然后startActivity(getIntent()),一切工作,因为它应该,除了这会导致一个不愉快的效果,我不喜欢)。有什么建议来帮助解决这个问题吗?谢谢。
------------------------------------ 编辑 ----------------------------------------
我瞄准的是类似谷歌任务应用程序(android)所做的——如果你检查项,它被删除的日期和添加到"Completed"实时的;我不知道这些信息是否对答案有帮助,但我认为值得一提。
你应该使用Sectioned-RecyclerView
参考下面的代码:https://github.com/IntruderShanky/Sectioned-RecyclerView
希望上面的链接能帮到你。
使用ViewModels和mutableelvedata来解决这个问题。
In MainActivity:
public class MainActivity extends AppCompatActivity implements LifecycleOwner {
public static ChildViewModel childViewModel;
public static ArrayList<ChildItem> childAdapterData;
private ParentAdapter parentAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setViewModel();
// Populate childAdapterData in setAdapter()
setAdapter();
setRecyclerView();
updateViewModel();
}
...
public static void updateViewModel() {
childViewModel.getChildItemLiveData().setValue(childAdapterData);
}
public void setViewModel() {
childViewModel = new ViewModelProvider(this).get(ChildViewModel.class);
final Observer<ArrayList<ChildItem>> observer = new Observer<ArrayList<ChildItem>>() {
@Override
public void onChanged(ArrayList<ChildItem> childItems) {
// Do things
// Don't have to do notifyDataSetChanged--also notifyItemRangeChanged(), etc.
parentAdapter.notifyDataSetChanged();
}
};
childViewModel.getChildItemLiveData().setValue(childAdapterData);
childViewModel.getChildItemLiveData().observe(this, observer);
}
}
In ChildAdapter:
public class ChildAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<ChildItem> childItemList;
public ChildAdapter(ArrayList<ChildItem> childItemList) {
this.childItemList = childItemList;
}
public class ChildViewHolder extends RecyclerView.ViewHolder {
CheckBox childCheckBox;
public ChildViewHolder(final View childView) {
super(childView);
childCheckBox = childView.findViewById(R.id.child_checkbox);
childCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ChildItem checkedChildItem = childItemList.get(getAdapterPosition());
// Remove from ParentItem A
...
// Add to ParentItem B
...
MainActivity.childAdapterData.set(parentItemAPosition, parentItemA);
MainActivity.childAdapterData.set(parentItemBPosition, parentItemB);
MainActivity.updateViewModel();
}
});
}
}
...
}
最后,ViewModel
public class ChildViewModel extends ViewModel {
public MutableLiveData<ArrayList<ChildItem>> childItemLiveData;
public MutableLiveData<ArrayList<ChildItem>> getChildItemLiveData() {
if(childItemLiveData == null) {
childItemLiveData = new MutableLiveData<>();
}
return childItemLiveData;
}
}