2017-08-01 128 views
1

问题:如何预加载recyclerview项目以给用户提供最佳的用户体验。预加载Android RecyclerView项目与StaggeredGridLayoutManager

发现此blog做同样的事情,但它与LinearLayoutManager不与StaggeredGridLayoutManager

方法setExtraLayoutSpacesetInitialPrefetchItemCount(int)StaggeredGridLayoutManager API中是不可避免的。

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

+0

你得到这方面的任何解决方案? – Rose

回答

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); 
     } 
    } 

为了让更好的用户体验,我有

相关问题