使用StaggeredGridLayoutManager预加载Android Recyclerview项目

时间:2017-08-01 11:59:03

标签: android performance android-recyclerview

问题:如何预加载recyclerview项目,为用户提供最佳用户体验。

发现此blog执行的操作与LinearLayoutManager相同,而不是StaggeredGridLayoutManager

方法setExtraLayoutSpacesetInitialPrefetchItemCount(int)StaggeredGridLayoutManager API中无法实现。

如何在用户滚动之前加载一些items(images)以提供更好的用户体验。

1 个答案:

答案 0 :(得分:0)

  

它不是完美的方式,但仍然有效。

     

在用户时创建用于在后台加载/缓存图像的意图服务   滚动以加载新帖子它将从缓存中获取它而不是去   到服务器。

<强> ImageCahce.java

public class ImageCache extends IntentService {

    Context mContext;

    public ImageCache() {
        super("ImageCache");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        Log.d("ImageCache", "Service Started");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null)
            if (intent.getStringExtra(Constants.IncomingClass) != null)
                if (intent.getStringExtra(Constants.IncomingClass).equalsIgnoreCase("Trending")) {
                    ArrayList<Trending> items = intent.getExtras().getParcelableArrayList(Constants.CacheKey);
                    StartCachingImages(items);
                }
        return START_NOT_STICKY;
    }

    //Trending logic
    private void StartCachingImages(final ArrayList<Trending> items) {

        if (items != null) {
            if (!items.isEmpty()) {
                for (int i = 0; i < items.size(); i++) {

                    Post pogo = items.get(i).postObj;
                    String imagePath = "";
                    if (pogo.type.equalsIgnoreCase("Picture")) {
                        imagePath = pogo.picture;
                    } else if (pogo.type.equalsIgnoreCase("Video")) {
                        imagePath = pogo.thumbnail;
                    } else if (pogo.type.equalsIgnoreCase("Multimedia")) {
                        //  imagePath = pogo.thumbnail;
                        if (pogo.picture.length() > 0) imagePath = pogo.picture;
                        else imagePath = pogo.thumbnail;
                    }

                    //Log.d("ImageCache", "index " + i + " started");
                    BackgroundCaching backgroundCaching = new BackgroundCaching(i, imagePath, items.get(i).userObj.picture);
                    backgroundCaching.execute();
                }
                //Log.d("ImageCache", "Self stopping");
                ImageCache.this.stopSelf();
            }
        }
    }

    class BackgroundCaching extends AsyncTask<String, Void, String> {

        String path, thumbnail;
        int id;

        public BackgroundCaching(int id, String path, String thumbnail) {
            this.id = id;
            this.path = path;
            this.thumbnail = thumbnail;
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                FutureTarget<File> future = Glide.with(mContext).load(path)
                        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
                future.get();
                //Log.d("ImageCache", "Loaded actual " + id);
            } catch (Exception e) {
                //Log.d("ImageCache", "Failed to load ->" + path);
            }
            try {
                FutureTarget<File> future = Glide.with(mContext).load(thumbnail)
                        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
                future.get();

                //Log.d("ImageCache", "Loaded thumbnail " + id);
            } catch (Exception e) {
                //Log.d("ImageCache", "Failed to load ->" + thumbnail);
            }
            return "Executed";
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("ImageCache", "Service Stopped");
    }
}

使用StartImageCacheService()启动服务。

 void StartImageCacheService(Context context, LinkedList<com.fayvo.Model.Trending> trendingHash) {
        if (context != null && trendingHash.size() > 0) {
            Intent i = new Intent(context, ImageCache.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putParcelableArrayListExtra(Constants.CacheKey, new ArrayList<com.fayvo.Model.Trending>(trendingHash));
            i.putExtra(Constants.IncomingClass, "Trending");
            context.startService(i);
        }
    }

为了提供更好的用户体验