在滚动完成之前,Recyclerview不能平滑滚动

Moh*_*kia 9 android smooth-scrolling recycler-adapter android-recyclerview recyclerview-layout

我有滚动问题.我的意思是滚动速度很快,但在滚动完成之前看起来像是滞后.在这里我定义__CODE__:

RecyclerView recyclerView=fragment.getRecyclerView();
        LinearLayoutManager layoutManager = new LinearLayoutManager(fragment.getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(fragment.getActivity(), LinearLayoutManager.VERTICAL));
 ArrayList<InstaPostModel> rowListItems=getInstaPostListSample();
        InstaPostAdapter rcAdapter = new InstaPostAdapter(rowListItems);
        recyclerView.setAdapter(rcAdapter);
Run Code Online (Sandbox Code Playgroud)

这是__CODE__.

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final InstaPostModel p = items.get(position);
    context = holder.itemView.getContext();
    Glide.with(context).load(R.mipmap.mee_small).into(holder.userPhoto);
    Glide.with(context).load(R.drawable.post_image).into(holder.photo_content);
    Glide.with(context).load(R.mipmap.love_gray_icon).into(holder.bt_like);
    Glide.with(context).load(R.mipmap.comment_icon).into(holder.bt_comment);
    Glide.with(context).load(R.mipmap.share_icon).into(holder.bt_share);

    holder.userNameTextView.setText(p.getPosterUserName());
    holder.postContent.setText(p.getText());
    holder.post_date.setReferenceTime(p.getDate().getTime());
}
Run Code Online (Sandbox Code Playgroud)

这是 __CODE__

<?xml version="1.0" encoding="utf-8"?>

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/qatar_now_posts_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        tools:context=".uis.fragments.home.QatarNowFragment"
        />
Run Code Online (Sandbox Code Playgroud)

编辑

我有一个底部导航栏__CODE__,它在滚动时显示.

编辑2

这是显示滞后的视频链接.

编辑3

也许我在测试应用程序时发现了问题.我看到图表和应用程序从Ram内存大约120 MB,但仍然不知道如何解决这个问题.

编辑4

__CODE__

RecyclerView recyclerView=fragment.getRecyclerView();
        LinearLayoutManager layoutManager = new LinearLayoutManager(fragment.getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(fragment.getActivity(), LinearLayoutManager.VERTICAL));
 ArrayList<InstaPostModel> rowListItems=getInstaPostListSample();
        InstaPostAdapter rcAdapter = new InstaPostAdapter(rowListItems);
        recyclerView.setAdapter(rcAdapter);
Run Code Online (Sandbox Code Playgroud)

这是 __CODE__

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final InstaPostModel p = items.get(position);
    context = holder.itemView.getContext();
    Glide.with(context).load(R.mipmap.mee_small).into(holder.userPhoto);
    Glide.with(context).load(R.drawable.post_image).into(holder.photo_content);
    Glide.with(context).load(R.mipmap.love_gray_icon).into(holder.bt_like);
    Glide.with(context).load(R.mipmap.comment_icon).into(holder.bt_comment);
    Glide.with(context).load(R.mipmap.share_icon).into(holder.bt_share);

    holder.userNameTextView.setText(p.getPosterUserName());
    holder.postContent.setText(p.getText());
    holder.post_date.setReferenceTime(p.getDate().getTime());
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了所有的解决方案,但没有人帮助我.我添加了所有代码.有人可以帮忙吗?

Khe*_*raj 4

我没有看到你的代码有任何问题。仍需考虑以下几点。

(1)如果您从 API 获取的图像尺寸太大,可能会发生什么情况。检查 API 的大小并使其最多减少 200 kb。(100-150 kb 也足够了)。

(2) 您不应该禁用缓存,因为它将图像存储在缓存中并在下次通过缓存而不是通过 URL 加载来改善用户体验。

(3) 现在,您不需要使用material-ripple库来实现 Android 中提供的简单涟漪效果。将此属性添加到您的ImageView

 android:background="?attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)

(4) 检查您一次装载的物品数量。您不应该一次加载许多项目。参见架构组件分页库

(5) 还要检查资源的大小,例如love_gray_icon. 我认为最大应该是70*70。如果你的尺寸比这个大,你应该调整它的大小。一次调整多个图像的大小。还要在使用前压缩图像,这会将图像大小减少最多 80%。调整大小后,压缩图像

(6) Glide 是一个维护良好的库,因此onViewRecycled是多余的。

最后对您的代码进行了一些修改。

  private void loadImage(ImageView iv, String url) {
        Glide.with(iv.getContext()).load(url).thumbnail(0.5f).into(iv);
    }

    private void loadImage(ImageView iv, int resId) {
        Glide.with(iv.getContext()).load(resId).thumbnail(0.5f).into(iv);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final InstaPostModel p = items.get(position);

        loadImage(holder.userPhoto, Urls.BASE_URI + p.getUserPhotoUrl());
        loadImage(holder.photo_content, Urls.BASE_URI + p.getPostPhotoUrl());
        loadImage(holder.bt_like, R.mipmap.love_gray_icon);
        loadImage(holder.bt_comment, R.mipmap.comment_icon);
        loadImage(holder.bt_share, R.mipmap.share_icon);

        holder.userNameTextView.setText(p.getPosterUserName());
        holder.postContent.setText(p.getText());
        holder.post_date.setReferenceTime(p.getDate().getTime());
    }
Run Code Online (Sandbox Code Playgroud)