![](/img/trans.png)
[英]Setting ids to items in an Arraylist in a RecyclerView Android Studio
[英]RecyclerView items in ArrayList
我正在开发一个android项目,并且将一些TextViews放入RecyclerView中,与此同时,我试图将这些内容作为ViewHolder类型放入数组列表中。 在对该程序进行了一些测试之后,我了解到插入ArrayList的项目只是屏幕上显示的项目。 例如,如果我的屏幕适合15个textview,并且我在回收视图和arraylist内放了30个textview,则arraylist的大小将仅为15,因此我无法对其余项目进行任何更改。
另外,当我向下滚动到回收站视图时,arraylist会获得滚动时已显示的项目的大小,但是当我滚动回到顶部并尝试更改TextViews的数量并减少程序崩溃时,它们会变大。
我想要的是在arraylist中也添加所有已添加到回收站视图中的项目,以便可以使用它们。
回收者视图类代码:
public class Tab1Child1Numbers extends RecyclerView.Adapter<Tab1Child1Numbers.ViewHolder> {
ArrayList<Integer> textFront;
ArrayList<Integer> textBack;
ArrayList<Integer> colors;
Context context;
ArrayList<ViewHolder> texts = new ArrayList<>();
public Tab1Child1Numbers(Context context, ArrayList<Integer> textFront, ArrayList<Integer> textBack, ArrayList<Integer> colors) {
super();
this.context = context;
this.textFront = textFront;
this.textBack = textBack;
this.colors = colors;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.tab1_child1_numbers, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
viewHolder.textFront.setText(textFront.get(i)+"");
viewHolder.textBack.setText(textBack.get(i)+"");
viewHolder.textFront.setBackgroundColor(colors.get(i));
viewHolder.textBack.setBackgroundColor(colors.get(i));
texts.add(viewHolder);
// cardsFront.add(viewHolder.imageFront);
// cardsBack.add(viewHolder.imgThumbnail);
viewHolder.setClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
if (isLongClick) {
} else {
Deck deck = new Deck();
deck.flipCard(texts.get(position).frame, texts.get(position).textFront, texts.get(position).textBack);
}
}
});
}
@Override
public int getItemCount() {
return textFront.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public TextView textFront;
public TextView textBack;
public FrameLayout frame;
public LinearLayout layout;
private ItemClickListener clickListener;
public ViewHolder(View itemView) {
super(itemView);
textFront = (TextView) itemView.findViewById(R.id.txt1);
textBack = (TextView) itemView.findViewById(R.id.txt2);
frame = (FrameLayout) itemView.findViewById(R.id.list_frame);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
public void setClickListener(ItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
@Override
public void onClick(View view) {
clickListener.onClick(view, getPosition(), false);
}
@Override
public boolean onLongClick(View view) {
clickListener.onClick(view, getPosition(), true);
return true;
}
}
class Deck {
private void flipCard(View rootLayout, View cardFace, View cardBack) {
FlipAnimation flipAnimation = new FlipAnimation(cardFace, cardBack);
if (cardFace.getVisibility() == View.GONE) {
flipAnimation.reverse();
}
rootLayout.startAnimation(flipAnimation);
}
public void flipAll(){
System.out.println(texts.size()+"--------");
randomize();
for (int i = 0; i < texts.size(); i++) {
flipCard(texts.get(i).frame, texts.get(i).textFront, texts.get(i).textBack);
}
}
private void randomize(){
for (int i=0; i<texts.size(); i++){
if (texts.get(i).textFront.getVisibility() == View.GONE) {
texts.get(i).textFront.setText(textFront.get(i) + "");
texts.get(i).textFront.setBackgroundColor(colors.get(i));
}
else {
texts.get(i).textBack.setText(textBack.get(i) + "");
texts.get(i).textBack.setBackgroundColor(colors.get(i));
}
}
}
}
看来您正在尝试做不应做的事情。
每当ViewHolder
用户显示ViewHolder
时,系统就会调用onBindViewHolder()
。 因此,最初将为屏幕上的每个视图调用一次,然后为屏幕外的少数视图调用一次(以便它们可以在屏幕上很好地滚动)。
但是,这意味着当用户滚动浏览适配器中的数据时,也会重复调用它。 在这些情况下, ViewHolder
传递给onBindViewHolder()
可能已被回收 ,这意味着它以前可能被用来显示不同的数据,现在被重新用于显示新的数据。
假设您的数据集中有100万个项目,但是布局的设置方式是,一次只能在屏幕上看到15个项目。 系统很有可能会创建大约20个ViewHolder
实例(在屏幕上有15个,在屏幕外可以重用5个)。 如果用户滚动足够多,则将使用这20个相同的ViewHolders来显示所有一百万个项目。 他们只会不断地重复使用。
这就是为什么这行有问题的原因:
texts.add(viewHolder);
您将最终建立一个列表,其中可能包含数百万个对象,而相同的20个ViewHolder
在列表中多次出现。
这将是更好的考虑存储在不同的方式你的数据,而不是试图存储ViewHolder
秒后,以onBindViewHolder()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.