Wednesday, 11 July 2018

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
Edit 3 maybe i found the problem when i am testing the app i saw the chart and the app took about 120 mb from Ram memory but still don't know how to fix this.

Edit 4 InstaPostAdapter class

public class InstaPostAdapter
        extends RecyclerView.Adapter<InstaPostAdapter.ViewHolder> {

    private List<InstaPostModel> items = new ArrayList<>();
    Context context;


    // Provide a suitable constructor (depends on the kind of dataset)
    public InstaPostAdapter(List<InstaPostModel> items) {
        this.items = items;
    }

    @Override
    public InstaPostAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.insta_post_list_item, parent, false);
        // set the view's size, margins, paddings and layout parameters
        return new ViewHolder(v);
    }

    @Override
    public void onViewRecycled(@NonNull ViewHolder holder) {
        super.onViewRecycled(holder);
        Glide.with(context).clear(holder.userPhoto);
        Glide.with(context).clear(holder.photo_content);
        Glide.with(context).clear(holder.bt_comment);
        Glide.with(context).clear(holder.bt_like);
        Glide.with(context).clear(holder.bt_share);
    }




    // 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);
        context = holder.itemView.getContext();
            Glide.with(context)
                    .load(Urls.BASE_URI +items.get(position).getUserPhotoUrl()).thumbnail(0.5f)
                    .apply(new RequestOptions()
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .skipMemoryCache(true).dontAnimate()
                    )
                    .into(holder.userPhoto);
            Glide.with(context)
                    .load(Urls.BASE_URI +items.get(position).getPostPhotoUrl()).thumbnail(0.5f)
                    .apply(new RequestOptions()
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .skipMemoryCache(true).dontAnimate()
                    )
                    .into(holder.photo_content);
            Glide.with(context)
                    .load(R.mipmap.love_gray_icon).thumbnail(0.5f)
                    .apply(new RequestOptions()
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .skipMemoryCache(true).dontAnimate()
                    )
                    .into(holder.bt_like);
            Glide.with(context)
                    .load(R.mipmap.comment_icon).thumbnail(0.5f)
                    .apply(new RequestOptions()
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .skipMemoryCache(true).dontAnimate()
                    )
                    .into(holder.bt_comment);
            Glide.with(context)
                    .load(R.mipmap.share_icon).thumbnail(0.5f)
                    .apply(new RequestOptions()
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .skipMemoryCache(true).dontAnimate()
                    )
                    .into(holder.bt_share);
            holder.userNameTextView.setText(p.getPosterUserName());
            holder.postContent.setText(p.getText());
            holder.post_date.setReferenceTime(p.getDate().getTime());

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return items.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void add(InstaPostModel post, int i) {
        items.add(post);
        notifyItemInserted(i);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public ImageView userPhoto;
        public TextView userNameTextView;
        public ImageView bt_more;
        public ImageView photo_content;
        public ImageView bt_like;
        public ImageView bt_comment;
        public ImageView bt_share;
        public TextView postContent;
        public RelativeTimeTextView post_date;

        public ViewHolder(View v) {
            super(v);
            userPhoto = v.findViewById(R.id.insta_user_photo);
            userNameTextView = v.findViewById(R.id.insta_user_name);
            bt_more = v.findViewById(R.id.insta_bt_more);
            photo_content = v.findViewById(R.id.insta_photo_content);
            bt_like = v.findViewById(R.id.insta_bt_like);
            bt_comment = v.findViewById(R.id.insta_bt_comment);
            bt_share = v.findViewById(R.id.insta_bt_share);
            postContent = v.findViewById(R.id.insta_post_content);
            post_date = v.findViewById(R.id.insta_post_date);
            setClickListener();
        }

        private void setClickListener() {
            bt_more.setOnClickListener(view -> {
                PopupMenu popupMenu = new PopupMenu(context, view);
                popupMenu.setOnMenuItemClickListener(item -> {
                    Snackbar.make(view, item.getTitle() + " Clicked", Snackbar.LENGTH_SHORT).show();
                    return true;
                });
                popupMenu.inflate(R.menu.menu_feed_popup);
                popupMenu.show();
            });
            bt_like.setOnClickListener(view -> Snackbar.make(view, "Like Clicked", Snackbar.LENGTH_SHORT).show());
            bt_comment.setOnClickListener(view -> Snackbar.make(view, "Comment Clicked", Snackbar.LENGTH_SHORT).show());
            bt_share.setOnClickListener(view -> Snackbar.make(view, "Share Clicked", Snackbar.LENGTH_SHORT).show());
        }

    }


}

and this is InstaPostListItem.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/insta_user_photo"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginBottom="@dimen/spacing_middle"
        android:layout_marginLeft="@dimen/spacing_large"
        android:layout_marginTop="@dimen/spacing_middle" />

    <TextView
        android:id="@+id/insta_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/spacing_middle"
        android:layout_marginLeft="@dimen/spacing_middle"
        android:layout_marginStart="@dimen/spacing_middle"
        android:layout_marginTop="@dimen/spacing_middle"
        android:text="Person name"
        android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
        android:textColor="@color/material_grey_800"
        android:textStyle="bold"
        app:layout_constraintStart_toEndOf="@id/insta_user_photo" />

    <com.balysv.materialripple.MaterialRippleLayout
        style="@style/RippleStyleBlack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent">

        <ImageView
            android:id="@+id/insta_bt_more"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:clickable="true"
            android:padding="8dp"
            android:tint="@color/grey_hard"
            app:srcCompat="@drawable/ic_more_gray" />
    </com.balysv.materialripple.MaterialRippleLayout>

    <ImageView
        android:id="@+id/insta_photo_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxHeight="250dp"
        android:scaleType="centerCrop"
        android:visibility="visible"
        app:layout_constraintTop_toBottomOf="@id/insta_user_photo" />

    <com.balysv.materialripple.MaterialRippleLayout
        android:id="@+id/insta_bt_like_material"
        style="@style/RippleStyleBlack"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/spacing_middle"
        app:layout_constraintTop_toBottomOf="@+id/insta_photo_content">

        <ImageView
            android:id="@+id/insta_bt_like"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:clickable="true" />
    </com.balysv.materialripple.MaterialRippleLayout>

    <com.balysv.materialripple.MaterialRippleLayout
        android:id="@+id/insta_bt_comment_material"
        style="@style/RippleStyleBlack"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/spacing_middle"
        app:layout_constraintStart_toEndOf="@+id/insta_bt_like_material"
        app:layout_constraintTop_toBottomOf="@+id/insta_photo_content">


        <ImageView
            android:id="@+id/insta_bt_comment"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:clickable="true" />
    </com.balysv.materialripple.MaterialRippleLayout>

    <com.balysv.materialripple.MaterialRippleLayout
        android:id="@+id/insta_bt_share_material"
        style="@style/RippleStyleBlack"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@+id/insta_bt_comment_material"
        app:layout_constraintTop_toBottomOf="@+id/insta_photo_content">


        <ImageView
            android:id="@+id/insta_bt_share"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:clickable="true" />
    </com.balysv.materialripple.MaterialRippleLayout>

    <View
        android:id="@+id/emptyView"
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:layout_marginBottom="@dimen/spacing_medium"
        android:background="@color/grey_soft"
        app:layout_constraintTop_toBottomOf="@+id/insta_bt_share_material" />

    <TextView
        android:id="@+id/insta_post_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="@dimen/spacing_xlarge"
        android:text="Sample text"
        android:textColor="@color/grey_dark"
        android:visibility="visible"
        app:layout_constraintTop_toBottomOf="@+id/emptyView" />
    />

    <com.github.curioustechizen.ago.RelativeTimeTextView
        android:id="@+id/insta_post_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="-"
        android:textAppearance="@style/TextAppearance.AppCompat.Caption"
        android:textColor="@color/grey_medium"
        android:textStyle="normal"
        app:layout_constraintTop_toBottomOf="@+id/insta_post_content" />
    />
</android.support.constraint.ConstraintLayout>

i tried all the solution but no one help me. i have added all my code please any help?



from Recyclerview not scrolling smoothly before scrolling finish

No comments:

Post a Comment