首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用StaggeredGridLayoutManager预装安卓回收项目

用StaggeredGridLayoutManager预装安卓回收项目
EN

Stack Overflow用户
提问于 2017-08-01 11:59:03
回答 1查看 1.7K关注 0票数 2

问题:如何预加载回收查看项目,为用户提供最佳的用户体验。

发现这个blog做同样的事情,但是它使用LinearLayoutManager而不是StaggeredGridLayoutManager

方法setExtraLayoutSpacesetInitialPrefetchItemCount(int)StaggeredGridLayoutManager API中不具有雪崩性。

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

EN

回答 1

Stack Overflow用户

发布于 2017-10-09 10:30:53

这不是完美的方式,但仍然有效。 创建意图服务,用于在后台加载/缓存图像,当用户滚动加载新的post时,它将从缓存中获取它,而不是转到服务器。

ImageCahce.java

代码语言:javascript
复制
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()启动服务。

代码语言:javascript
复制
 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);
        }
    }

为了提供更好的用户体验

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45437197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档