Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用交错布局预加载毕加索图像_Java_Android_Android Recyclerview_Staggeredgridlayout - Fatal编程技术网

Java 使用交错布局预加载毕加索图像

Java 使用交错布局预加载毕加索图像,java,android,android-recyclerview,staggeredgridlayout,Java,Android,Android Recyclerview,Staggeredgridlayout,我有一个StaggedGridLayoutManager,可以在2列宽的网格中加载20张包含图像的卡(从缓存、网络或毕加索的存储中加载) 问题: // Setup RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.gridview_posts); mRecyclerView.setHasFixedSize(true); // Setup StaggeredGridLayo

我有一个StaggedGridLayoutManager,可以在2列宽的网格中加载20张包含图像的卡(从缓存、网络或毕加索的存储中加载)

问题:

    // 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();
<?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>
当前,当您向下滚动列表时,图像会随着卡滑入视图而加载,如下图所示:

所需解决方案:

    // 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();
<?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>
我希望将图像加载到屏幕外,因此有一个无缝的用户体验,如以下视频:

这些视频来自一篇博客文章,该文章使用LinearLayoutManager而不是StaggedGridLayoutManager解决了这个问题。我似乎无法找到解决这个问题的等效方法。我对安卓系统的开发还比较陌生,并尝试阅读了文档,但似乎找不到任何解决方案

代码:

    // 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();
<?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>

预加载要在缓存中显示的图像,这样在滚动列表时就不会加载它们了,它们已经在内存中了

 Picasso.with(this).load("url").fetch();
此外,删除毕加索·法登动画

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

刚刚找到了解决我自己问题的部分方法。我会为任何像我这样想弄明白这一点的新手发布

RecyclerView方法setItemViewCacheSize(int-size)将保留缓存中项目的需求量,这样就不会有闪烁类型的视图加载

我还没有找到一个前瞻性的解决方案来提前加载特定数量的视图。

对我来说,它很有效

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

    // 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();
<?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>

所有图像都已从缓存加载,当我设置noFade()时,图像会在屏幕上显示时弹出。我需要以某种方式将布局的大小设置为大于物理屏幕的大小,以便在用户看到卡之前将其弹出到位。