Java 为什么在我滚动时,RecyclerView(在我的情况下)中的CardView内容会发生变化?

Java 为什么在我滚动时,RecyclerView(在我的情况下)中的CardView内容会发生变化?,java,android,android-layout,android-recyclerview,android-cardview,Java,Android,Android Layout,Android Recyclerview,Android Cardview,我有和这篇文章一样的问题 但是去掉静力学不起作用 我所做工作的背景: 我在FlexboxLayout中添加了两个按钮,然后在CardView中添加了一个按钮(CardView,RecyclerView)。正在动态添加按钮 同样的事情也发生在两个TextView上,我在按钮s之后添加到CardView 问题: 按钮s和TextViews在我滚动时相乘 滚动时,正在交换不同的CardViews的上下文 视频: 我正在使用的内容: RecyclerView位于我的一个片段(ConstraintLayo

我有和这篇文章一样的问题

但是去掉静力学不起作用

我所做工作的背景:

我在FlexboxLayout中添加了两个按钮,然后在CardView中添加了一个按钮(
CardView
RecyclerView
)。正在动态添加按钮

同样的事情也发生在两个
TextView
上,我在
按钮
s之后添加到
CardView

问题:

按钮
s和
TextView
s在我滚动时相乘

滚动时,正在交换不同的
CardView
s的上下文

视频:

我正在使用的内容:

RecyclerView
位于我的一个
片段(
ConstraintLayout
)中,我在其中定义了回收器适配器

这是我的适配器

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
ArrayList<DataModel> dataSet = new ArrayList<DataModel>();
Context currentContext;

public class MyViewHolder extends RecyclerView.ViewHolder {

    public Button datumButton;
    public FlexboxLayout matchedWordsLayout;
    public LinearLayout textviewLayout;



    public MyViewHolder(View itemView) {
        super(itemView);
        this.datumButton = (Button) itemView.findViewById(R.id.datumButton);
        this.matchedWordsLayout = (FlexboxLayout) itemView.findViewById(R.id.matchedWordsLayout);
        this.textviewLayout = (LinearLayout) itemView.findViewById(R.id.textviewLayout);

    }
}


public CustomAdapter(ArrayList<DataModel> data, Context currentContext) {
    this.dataSet = data;
    // currentContext not getting it from here

}

@NonNull
@Override
public CustomAdapter onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view =  LayoutInflater.from(parent.getContext())
            .inflate(R.layout.positive_result_card, parent, false);

    MyViewHolder myViewHolder = new MyViewHolder(view);
    this.currentContext = parent.getContext();
    return myViewHolder;
}


@RequiresApi(api = Build.VERSION_CODES.O)
//@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int listPosition) {
    Button DatumButton = holder.datumButton;
    FlexboxLayout MatchedWordsLayout = holder.matchedWordsLayout;
    LinearLayout TextviewLayout = holder.textviewLayout;

    //Modify the button for date
    ArrayList <String> TTMMYY = dataSet.get(listPosition).getDatum();
    String Datum = String.join(".", TTMMYY);
    DatumButton.setText(Datum);
    DatumButton.setTag(Datum);
  

  



    // add button for each word
    ArrayList <String>  ButtonNames = dataSet.get(listPosition).getButtonnames();

    for (String Buttonname : ButtonNames) {
        Button sampleButton = new Button(currentContext);
        sampleButton.setText(Buttonname);
        sampleButton.setTag(Datum); 

        MatchedWordsLayout.addView(sampleButton);
    }

    
    ArrayList <String> textLines = dataSet.get(listPosition).getTextLines();
    

   
    for (String satzt : textLines){
        TextView sampleTextView = new TextView(currentContext);
        sampleTextView.setText(satzt);

        TextviewLayout.addView(sampleTextView);
    }





}

@Override
public int getItemCount() {
    return dataSet.size();
}
  }
公共类CustomAdapter扩展了RecyclerView.Adapter{ ArrayList数据集=新的ArrayList(); 语境; 公共类MyViewHolder扩展了RecyclerView.ViewHolder{ 公共按钮datumButton; 公共FlexboxLayout匹配WordsLayout; 公共线路布局文本视图布局; 公共MyViewHolder(查看项目视图){ 超级(项目视图); this.datumButton=(按钮)itemView.findViewById(R.id.datumButton); this.matchedWordsLayout=(FlexboxLayout)itemView.findViewById(R.id.matchedWordsLayout); this.textviewLayout=(LinearLayout)itemView.findViewById(R.id.textviewLayout); } } 公共CustomAdapter(ArrayList数据,上下文currentContext){ this.dataSet=数据; //currentContext无法从这里获取它 } @非空 @凌驾 public CustomAdapter onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){ View=LayoutInflater.from(parent.getContext()) .充气(R.layout.阳性结果卡、家长卡、假卡); MyViewHolder MyViewHolder=新的MyViewHolder(视图); this.currentContext=parent.getContext(); 返回myViewHolder; } @RequiresApi(api=Build.VERSION\u CODES.O) //@凌驾 public void onBindViewHolder(@NonNull MyViewHolder holder,int-listPosition){ 按钮DatumButton=保持架。DatumButton; FlexboxLayout MatchedWordsLayout=保持架。MatchedWordsLayout; LinearLayout TextviewLayout=holder.TextviewLayout; //修改日期按钮 ArrayList TTMMYY=dataSet.get(listPosition.getDatum(); 字符串基准=字符串连接(“.”,TTMMYY); DatumButton.SETEXT(基准); DatumButton.setTag(基准); //为每个单词添加按钮 ArrayList ButtonNames=dataSet.get(listPosition.getButtonnames(); 用于(字符串按钮名称:按钮名称){ Button sampleButton=新按钮(currentContext); sampleButton.setText(Buttonname); sampleButton.setTag(基准); MatchedWordsLayout.addView(sampleButton); } ArrayList textLines=dataSet.get(listPosition.getTextLines(); 用于(字符串satzt:textLines){ TextView sampleTextView=新的TextView(当前上下文); sampleTextView.setText(satzt); TextviewLayout.addView(sampleTextView); } } @凌驾 public int getItemCount(){ 返回dataSet.size(); } }

我的文本可能有错误

您在每个绑定上以编程方式添加
视图
s,但您从未删除它们,因此每个绑定只会添加更多(请记住,
视图持有者
s被重复使用,所以上次添加的
按钮和
文本视图
s仍然存在)。要修复此问题,请在添加新的子项之前,从
视图组中删除所有子项:

//添加:在添加更多子项之前删除所有子项
MatchedWordsLayout.removeAllViews();
用于(字符串按钮名称:按钮名称){
Button sampleButton=新按钮(currentContext);
sampleButton.setText(Buttonname);
sampleButton.setTag(基准);
MatchedWordsLayout.addView(sampleButton);
}
ArrayList textLines=dataSet.get(listPosition.getTextLines();
//添加:在添加更多之前删除所有子项
TextviewLayout.removeAllViews();
用于(字符串satzt:textLines){
TextView sampleTextView=新的TextView(当前上下文);
sampleTextView.setText(satzt);
TextviewLayout.addView(sampleTextView);
}

Thank's我想经过两天的努力,它终于成功了。问题:我认为数据集的每个实例都是单独的tretet,因此要为新卡添加的数据被设置为其原始值?另一个问题是,为什么在我滚动时会发生这种情况?滚动时会发生这种情况,因为当你从屏幕上滚动一个视窗时,它会被重新绑定并用于另一个数据段。当您通过滚动查看不同的数据段时,或者如果您使用
Adapter
上的
notify…
方法之一显式触发重新绑定,则会发生此重新绑定。