使用StaggeredGridLayout预加载Picasso图像

时间:2015-05-21 10:01:03

标签: java android android-recyclerview staggeredgridlayout

我有一个StaggeredGridLayoutManager,它可以在2列宽的网格中加载20张卡片,这些卡片包含图片(从缓存,网络或Picasso存储中加载)。

问题:

目前,当您向下滚动列表时,图片会在卡片滑入视图时加载:

https://www.youtube.com/watch?v=1xMNmMjqEbI

所需解决方案:

我想要的是将图像加载到屏幕外,因此有一个像这个视频的无缝用户体验:

https://www.youtube.com/watch?v=4c7ZID7yjII

这些视频来自博客文章,该文章使用 LinearLayoutManager 而不是 StaggeredGridLayoutManager 解决了这个问题。我似乎无法弄清楚这个问题的等效解决方案。我对Android开发很新,并试图阅读文档,但我似乎找不到任何导致解决方案的东西。

代码:

    // Setup RecyclerView
    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.gridview_posts);
    mRecyclerView.setHasFixedSize(true);

    // Setup StaggeredGridLayoutManager
    mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // Set Adapter
    mPostsAdapter = new PostAdapter(rootView.getContext(), posts);
    mRecyclerView.setAdapter(mPostsAdapter);

    // Start RestClient & get posts
    restClient = new RestClient(getString(R.string.api_location));
    getPosts();

3 个答案:

答案 0 :(得分:3)

预加载要在缓存中显示的图像,因此在滚动列表时不是加载它们,而是已经在内存中。

Picasso.with(this).load("url").noFade().into(imageView);

另外,删除Picasso fadeIn动画。

dictionary[word]+=1

答案 1 :(得分:0)

刚刚想出了我自己问题的部分解决方案。要为像我这样的新人发布帖子,试图解决这个问题。

RecyclerView方法 setItemViewCacheSize(int size)会将缓存中的项目数量保留在缓存中,这样就不会出现闪烁类型的视图加载。

虽然提前加载了特定数量的视图,但我还没有找到前瞻性解决方案。

答案 2 :(得分:0)

对我来说,它正常工作

Picasso.get()
 .load(uploadCurrent.getImageUrl())
 .placeholder(R.drawable.logoblack_loading)
 .config(Bitmap.Config.RGB_565)
 .into(holder.imageView);

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <ImageView
        android:id="@+id/id_imgItem"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"/>
    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/id_imgItem"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:gravity="center_horizontal"
        android:textSize="18sp"/>

</RelativeLayout>
相关问题