androidandroid-recyclerviewsmooth-scrollingrecyclerview-layout

Recyclerview not scrolling smoothly before scrolling finish


i am having problem with scrolling i mean the scrool fast but it's look like lagging before scrolling finish here i define RecyclerView :

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

and here onBindViewHolder

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

and here RecyclerView.xml

<?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"
        />

Edit also i have bottom navigation bar in same fragment and it show when scrolling

Edit 2 here is video link showing the lag
i tried all the solution but no one help me. i have added all my code please any help?


Solution

  • I did not see any problem with your code. Still below points need to be considered.

    (1) What could happen that size of image you are getting from API is too large. Check size of API and make it reduce by 200 kb max. (100-150 kb will be enough too).

    (2) You should not disable cache as it will improve user experience by storing image in cache and load next time by cache not by URL.

    (3) You don't need to use material-ripple library for a simple ripple effect available in Android now. Add this attribute to your ImageView

     android:background="?attr/selectableItemBackground"
    

    (4) Check how many items you are loading at once. You should not load many items at once. See Architecture component paging library

    (5) Also check size of your resources like love_gray_icon. I think it should be max 70*70. If you have larger than that, you should resize it. Resize multiple images at once. Also compress images before use, that will decrease image size up-to 80%. After resize, compress images.

    (6) Glide is an well maintained library so onViewRecycled is redundant.

    Finally bit amendment in your code.

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