RecyclerView在滚动时创建和绑定所有ViewHolders

时间:2016-07-17 21:14:07

标签: android android-recyclerview

我有一个奇怪的问题,我无法指出。我的应用程序从网站获取电影数据,特别是海报图像。我创建了一个RecyclerView,它应该显示2列网格中的所有海报。

滚动体验极其滞后,由于某些原因,即使数据列表中没有任何更改,这些项目也会在滚动中间进行交换。

我尝试调试应用程序,发现第一次调用notifyDataSetChanged()时,我的适配器会调用onCreateViewHolder(...)来查找列表中的每个项目(大约90次,当在任何给定时刻只能看到6张海报时),onBindViewAdapter(...)也是如此。 然后,在滚动时,它会重新创建并重新绑定所有ViewHolders!

我一次又一次检查了我的代码,但我们无法确定原因。非常感谢您的帮助。

RecyclerView.adapter:

public class MovieRecyclerViewAdapter extends RecyclerView.Adapter<MovieRecyclerViewAdapter.ViewHolder> {
    private final Cinema cinema;
    private final List<Movie> movies;
    private final Context context;
    private final DisplayMetrics displayMetrics;
    private final Picasso picasso;

    public MovieRecyclerViewAdapter(Context context, Cinema cinema, List<Movie> movies) {
        this.context = context;
        this.cinema = cinema;
        this.movies = movies;

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        displayMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(displayMetrics);

        // Set up Picasso with okHttp3
        okhttp3.OkHttpClient okHttp3Client = new okhttp3.OkHttpClient();
        OkHttp3Downloader okHttp3Downloader = new OkHttp3Downloader(okHttp3Client);
        picasso = new Picasso.Builder(context)
                .downloader(okHttp3Downloader)
                .build();
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Movie movie = movies.get(position);
        int imgViewWidth = displayMetrics.widthPixels / 2;
        int imgViewHeight = (int)(imgViewWidth * 1.4);
        picasso.with(context)
                .load(cinema.getPosterUrl(movie))
                .resize(imgViewWidth, imgViewHeight)
                .into(holder.imageView);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;

        public ViewHolder(View view) {
            super(view);
            this.imageView = (ImageView) view.findViewById(R.id.fragment_movies_poster);
        }
    }
}

RecyclerView项目布局:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_movies_poster"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</ImageView>

RecyclerView XML定义:

<android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_movies_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/fragment_movies" />

RecyclerView配置,设置适配器:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_movies_grid, container, false);

        Context context = view.getContext();
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.fragment_movies_recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(context, columnCount));
        movies = new ArrayList<>();
        adapter = new MovieRecyclerViewAdapter(context, cinema, movies);
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);

        fetchMovies(view); // Populates movies list

        return view;
    }

修改

自发布此问题以来,我发现了一个类似的问题:Android RecyclerView Creates and Binds all the views on Dataset change

实际上,一旦我将项目layout_height设置为固定值而不是&#34; wrap_content&#34;问题是固定的。显然,RecyclerView会尝试创建其所有视图以确定其高度。

但是,我尝试了该问题中提供的解决方案,调用LayoutManager&#39; lm.setAutoMeasureEnabled(false),其layout_height设置为&#34; wrap_content&#34;问题依然存在。有没有建议如何在没有出现此问题的情况下获得自适应layout_height?

1 个答案:

答案 0 :(得分:0)

我最终通过为项目使用自定义ImageView子类来避免此问题,覆盖onMeasure()并为其提供固定宽高比,根据宽度确定高度。

FixedAspectRatioImageView:

public class FixedAspectRatioImageView extends ImageView{
    public FixedAspectRatioImageView(Context context) {
        super(context);
    }

    public FixedAspectRatioImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FixedAspectRatioImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = (int) (widthSize * 1.4);
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY));
    }
}

XML中定义的RecyclerView项目:

<?xml version="1.0" encoding="utf-8"?>
<com.michaelsvit.kolnoa.FixedAspectRatioImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_movies_poster"
    android:layout_width="match_parent"
    android:layout_height="288dp"> // Value here does not matter, as it is dynamically set
                                   // to be 1.4 times width

</com.michaelsvit.kolnoa.FixedAspectRatioImageView>