RecyclerView适配器显示的图像多于应有的图像

时间:2015-11-21 21:16:29

标签: android android-layout android-xml android-recyclerview

我有一个RecyclerView适配器,可以将另一个布局(child_layout.xml)扩展到主布局(main_layout.xml)。

这是RecyclerView适配器:

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

    private static Context context;
    private List<Message> mDataset;

    public RecyclerAdapter(Context context, List<Message> myDataset) {
        this.context = context;
        this.mDataset = myDataset;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener, View.OnClickListener {
        public TextView title;
        public LinearLayout placeholder;

        public ViewHolder(View view) {
            super(view);
            view.setOnCreateContextMenuListener(this);

            title = (TextView) view.findViewById(R.id.title);
            placeholder = (LinearLayout) view.findViewById(R.id.placeholder);
        }
    }

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_layout, parent, false);
        ViewHolder vh = new ViewHolder((LinearLayout) view);

        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Message item = mDataset.get(position);

        holder.title.setText(item.getTitle());

        int numImages = item.getImages().size();

        if (numImages > 0) {
            View inflater = LayoutInflater.from(holder.placeholder.getContext()).inflate(R.layout.child_layout, holder.placeholder, false);
            ImageView image = (ImageView) inflater.findViewById(R.id.image);
            Glide.with(context)
                .load("http://www.website.com/test.png")
                .fitCenter()
                .into(image);
            holder.placeholder.addView(inflater);
        }
    }

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

}

以下是main_layout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <LinearLayout
        android:id="@+id/placeholder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        />

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

以下是child_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

问题是,当我滚动浏览我的应用时,部分视图会显示2个以上的图片(当child_layout只有一个ImageView时):

http://i.imgur.com/AiYYs9l.png

为什么会这样?为什么有些视图只显示一个图像时会显示多个图像?

1 个答案:

答案 0 :(得分:2)

onBindViewHolder用于将数据绑定到您在onCreateViewHolder中创建的视图(因此名称)。您不应每次在onBindViewHolder中对新视图进行充气,因为这会消除RecyclerView的巨大好处,即查看回收。导致此问题的原因是,每次调用onBindViewHolder时,您都会将子项充气并添加到主视图中。对于单个视图,可以多次调用此方法,这意味着每次调用时都会添加新的子视图。您可以通过仅在onCreateViewHolder内部膨胀子视图来解决此问题。您在绑定中的条件检查表明您不希望它始终可见。如果没有图像,你可以在子视图上调用setVisibility来隐藏它。