android - 将动态 TextView 添加到 RecyclerView ViewHolder 会弄乱 View

标签 android android-layout android-viewholder

我试图在我的 View 持有者中动态实现 TextView ,在第一次滚动时一切顺利,卡片等每个属性都是如此,但是如果我再次向上滚动, TextView 开始弹出它们应该弹出的位置'

我的自定义适配器:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {

public interface OnItemClickListener {
    void onItemClick(MyProduct product);
}

int counter = 0;
private OnItemClickListener listener;
private Context context;
List<MyProduct> myProducts;


public CustomAdapter(OnItemClickListener listener, List<MyProduct> myProducts, Context context)
{
    super();
    this.listener = listener;
    this.myProducts = myProducts;
    this.context = context;
}

@Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.product_list, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {

    if( myProducts != null) {

        holder.bind(myProducts.get(position), listener);
        counter++;
    }
}

@Override
public int getItemCount() {
    return myProducts.size();
}


public class ViewHolder extends RecyclerView.ViewHolder {
    //Views
    public ImageView imageView;
    public TextView textViewProductTitle;
    public TextView textViewProductDescription;
    public TextView textViewProductSerialNumber;
    public TextView textViewProductPrice;
    List<TextView> textViews;
    ViewGroup viewGroup;


    //Initializing Views
    public ViewHolder(View itemView) {
        super(itemView);


        viewGroup = (ViewGroup)itemView.findViewById(R.id.linearCatalog);
        textViews = new LinkedList<>();
        imageView = (ImageView) itemView.findViewById(R.id.thumbnail);
        textViewProductTitle = (TextView) itemView.findViewById(R.id.textViewProductTitle);
        textViewProductDescription = (TextView) itemView.findViewById(R.id.textViewProductDescription);
        textViewProductSerialNumber = (TextView) itemView.findViewById(R.id.textViewProductSerialNumber);
        textViewProductPrice = (TextView) itemView.findViewById(R.id.textViewProductPrice);

    }

    public void bind(final MyProduct item, final OnItemClickListener listener) {



        Glide.with(context).load(item.getProductImage())
        /*.asBitmap().format(DecodeFormat.PREFER_ARGB_8888).*/
        .crossFade()
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .into(imageView);
        textViewProductTitle.setText(item.getProductTitle());
        textViewProductDescription.setText(item.getProductDescription());
        textViewProductSerialNumber.setText(item.getProductSn());
        textViewProductPrice.setText(item.getProductPrice());

        if(counter< myProducts.size())
        {
            for (int i = 0; i < item.getBranchCounter(); i++) {
                TextView textView = new TextView(context);
                textView.setText(item.getAddressList().get(i));
                textViews.add(textView);
                viewGroup.addView(textView);
            }
        }

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                listener.onItemClick(item);
            }
        });
    }
}

任何建议都会有帮助,你!

最佳答案

将 View 动态添加到回收器 View 中是一个坏主意。回收器 View 的要点是它在多个项目上回收相同的 View 结构。因此,当它回收时,您突然将该 View 添加到不需要它的新项目中。没有动态 View 几乎是拥有回收站 View 的先决条件。

更好的方法是使用可见性。将 TextView 放在所有 View 中,但让它消失。在您的绑定(bind)中,您可以将每个项目的 TextView 的可见性设置为 GONE 或 VISIBLE。这将使您获得想要的效果。

另一种方法是在有和没有额外 View 的情况下使用不同的 View 类型,但这往往更复杂。

关于android - 将动态 TextView 添加到 RecyclerView ViewHolder 会弄乱 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38805389/

相关文章:

android - 在方向之间切换并具有动态非 UI fragment 和单独的布局文件时膨胀异常

android - 为什么在 View 动画后 setVisibility 不起作用?

android - 查看覆盖操作工具栏?

android - 如何在谷歌登录按钮android中居中文本

java - 如何使用 View 持有者模式制作自定义适配器?

android - 用于在 android 中浏览 XML(特别是 rss)的 Intent 过滤器

android - 从 Canvas 上的单个位图绘制多次

java - XML 格式不正确

Android - 如何使用 RecyclerView.ViewHolder 将 View 添加到 LinearLayout

android - 在 onCreateViewHolder 中创建的 ViewHolder 中手动创建的 View