删除其他适配器位置的视图

问题描述

问题::我试图有条件地在适配器的onBindViewHolder部分内的回收器视图中删除项目的线性布局组件,但我不知道如何访问上一个视图要做到这一点。我该怎么办?以下内容适用于最近添加的项目,但是如何针对先前添加的项目执行此操作?这是在适配器内部进行的,因此我无法直接访问recyclerView或layoutmanager。

if (holder.mContainer.getParent() != null) {
       ((ViewGroup) holder.mEmotionLayout.getParent()).removeView(holder.mEmotionLayout);
}
holder.mMainLayout.addView(holder.mEmotionLayout,1); 

如果您需要我在帖子中添加任何内容,请告诉我。谢谢。

更新:我尝试将线性布局管理器传递给适配器,如下所示,然后尝试使用后续代码删除有问题的视图,但是findViewById始终返回null。

private linearlayoutmanager linearlayoutmanager;

public ChatAdapter(linearlayoutmanager linearlayoutmanager) {
   
    this.linearlayoutmanager = linearlayoutmanager;

在onBindViewHolder内部:

((ViewGroup) linearlayoutmanager.getChildAt(position-1).findViewById(R.id.emotionLayout).getParent()).removeView(linearlayoutmanager.getChildAt(position-1).findViewById(R.id.emotionLayout));

解决:使用数组列表保存旧的viewHolders取得了一些成功,但是我遇到了超出范围的异常,有时会在重新启动或重新加载活动后使应用程序崩溃。

java.lang.indexoutofboundsexception: Index: 1,Size: 1

//within onBindViewHolder

private ArrayList mainLinearLayouts = new ArrayList();
private ArrayList emotionLinearLayouts = new ArrayList();

mainLinearLayouts.add(holder.mMainLayout);
emotionLinearLayouts.add(holder.mEmotionLayout);

if (position > 0) {
         if (chatList.get(position - 1).getCurrentUser()) {
                    LinearLayout mll = (LinearLayout) mainLinearLayouts.get(position - 1);
                    LinearLayout ell = (LinearLayout) emotionLinearLayouts.get(position - 1);
                    mll.removeView(ell);
                }
            }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)