首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用StaggeredGridLayout的Android RecyclerView中滚动不流畅

在使用StaggeredGridLayout的Android RecyclerView中滚动不流畅
EN

Stack Overflow用户
提问于 2016-04-01 05:03:59
回答 1查看 2K关注 0票数 0

我使用StaggeredGridLayoutManager创建了一个有两列的RecyclerView,每个项目包含2个图像和文本,第一个图像的大小可以是(284*572,840*401或283*232)第二个35*35和一个文本,列表看起来很棒,但问题是,当用户向下滚动不平滑和不连续,图像是从本地存储(可绘制),我们可以做些什么来批准滚动体验。

FantasyTabAdpter.java

代码语言:javascript
复制
public class FantasyTabAdapter extends RecyclerView.Adapter<FantasyTabAdapter.MyViewHolder> {
    private LayoutInflater inflater;
    private ArrayList<Fantasy> fantasies;

    public FantasyTabAdapter(Context context, ArrayList<Fantasy> fantasies) {
        inflater = LayoutInflater.from(context);
        this.fantasies = fantasies;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.fantasy_tab_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final Fantasy fantasy = fantasies.get(position);
        holder.setData(fantasy, position);
    }

    @Override
    public int getItemCount() {
        return fantasies.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView fantasyImage;
        TextView fantasyTitle;
        ImageView fantasyStatus;

        public MyViewHolder(View itemView) {
            super(itemView);
            fantasyImage = (ImageView) itemView.findViewById(R.id.fantasy_tab_image);
            fantasyTitle = (TextView) itemView.findViewById(R.id.fantasy_tab_title);
            fantasyStatus = (ImageView) itemView.findViewById(R.id.fantasy_tab_status);
        }

        public void setData(Fantasy fantasy, int position) {
            this.fantasyTitle.setText(fantasy.getFantasyHeader());
            this.fantasyImage.setImageResource(fantasy.getFantasyHeaderImageID());
            this.fantasyStatus.setImageDrawable(fantasy.statusImagePlsgod);
        }
    }
}

在片段中设置RecyclerView

代码语言:javascript
复制
RecyclerView myRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);

StaggeredGridLayoutManager setLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
FantasyTabAdapter fantasyTabAdapter = new FantasyTabAdapter(getActivity(),fantasies);

myRecyclerView.setLayoutManager(setLayoutManager);
myRecyclerView.setAdapter(fantasyTabAdapter);
EN

回答 1

Stack Overflow用户

发布于 2016-04-01 13:41:09

您可以缓存图像以提高性能。首先看这两个视频:

Caching UI data

The Magic of LRU Cache

然后,在理解了原因和如何解决它的想法之后,您可以使用如下图像库:

Glide

picasso

它们实现了缓存策略,因此您只需导入其中一个并使用它,例如,而不是:

代码语言:javascript
复制
 this.fantasyImage.setImageResource(fantasy.getFantasyHeaderImageID());

您可以使用

代码语言:javascript
复制
Picasso.with(context).load(fantasy.getFantasyHeaderImageID()).into(this.fantasyImage);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36343742

复制
相关文章

相似问题

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