【问题标题】:Scrolling is not smooth in Android RecyclerView using StaggeredGridLayout使用 StaggeredGridLayout 在 Android RecyclerView 中滚动不流畅
【发布时间】:2016-03-31 21:03:59
【问题描述】:

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

FantasyTabAdpter.java

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

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

【问题讨论】:

  • Fantasy 是一个包含有关奇幻电影的信息的对象,我们从中获取图像和文本的 id

标签: android imageview android-recyclerview staggeredgridlayout


【解决方案1】:

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

Caching UI data

The Magic of LRU Cache

然后在了解原因和解决方法后,您可以使用以下图像库:

Glide

picasso

那些实现缓存策略,所以您只需要导入其中一个并使用它来代替:

 this.fantasyImage.setImageResource(fantasy.getFantasyHeaderImageID());

你可以使用

Picasso.with(context).load(fantasy.getFantasyHeaderImageID()).into(this.fantasyImage);

【讨论】:

  • 滑翔更好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多