为什么滚动时 RecyclerView 中的 CardView 内容(在我的例子中)会发生变化?
Why is the content of CardView inside a RecyclerView (in my case) changing when I scroll?
我也有这样的问题post .
但是去掉静力学是行不通的。
我正在做的事情的背景:
我在 FlexboxLayout
和 CardView
中添加了几个按钮(CardView
在 RecyclerView
中)。正在动态添加按钮。
同样的事情发生在几个 TextView
上,我在 Button
之后添加到 CardView
。
问题:
Button
s 和 TextView
s 正在相乘,而我滚动。
滚动时正在交换不同 CardView
的上下文。
视频:https://sendvid.com/adugp8vh
我用的是:
RecyclerView
在我的 Fragment
之一 (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();
}
}
我的文字可能有错误
您在每个绑定上以编程方式添加 View
s,但您从不删除它们,因此每个绑定只会添加更多(请记住 ViewHolder
s 被重复使用,因此 Button
s 和上次添加的 TextView
s 仍然存在)。要修复它,请在添加新子项之前从 ViewGroup
中删除所有子项:
// Added: remove all the children before we add more
MatchedWordsLayout.removeAllViews();
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();
// Added: remove all the children before we add more
TextviewLayout.removeAllViews();
for (String satzt : textLines){
TextView sampleTextView = new TextView(currentContext);
sampleTextView.setText(satzt);
TextviewLayout.addView(sampleTextView);
}
我也有这样的问题post
但是去掉静力学是行不通的。
我正在做的事情的背景:
我在 FlexboxLayout
和 CardView
中添加了几个按钮(CardView
在 RecyclerView
中)。正在动态添加按钮。
同样的事情发生在几个 TextView
上,我在 Button
之后添加到 CardView
。
问题:
Button
s 和 TextView
s 正在相乘,而我滚动。
滚动时正在交换不同 CardView
的上下文。
视频:https://sendvid.com/adugp8vh
我用的是:
RecyclerView
在我的 Fragment
之一 (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();
}
}
我的文字可能有错误
您在每个绑定上以编程方式添加 View
s,但您从不删除它们,因此每个绑定只会添加更多(请记住 ViewHolder
s 被重复使用,因此 Button
s 和上次添加的 TextView
s 仍然存在)。要修复它,请在添加新子项之前从 ViewGroup
中删除所有子项:
// Added: remove all the children before we add more
MatchedWordsLayout.removeAllViews();
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();
// Added: remove all the children before we add more
TextviewLayout.removeAllViews();
for (String satzt : textLines){
TextView sampleTextView = new TextView(currentContext);
sampleTextView.setText(satzt);
TextviewLayout.addView(sampleTextView);
}