1

I have Recyclerview in NestedScrollView. Now I have 1000 of items to be populate in Recylerview but its showing UI is working too much work on its main thread and continuously gets crashed or asked for wait message.Any sugggestion would be appreciated.

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/view_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg">

        <android.support.v4.widget.NestedScrollView

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">


                        <android.support.v7.widget.RecyclerView

                            android:id="@+id/recylerview"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:divider="@null"
                            android:orientation="horizontal" />

        </android.support.v4.widget.NestedScrollView>
    </RelativeLayout>



//List Item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/view_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dp">


        <RelativeLayout
            android:id="@+id/rl_img"
            android:layout_width="@dimen/img_wextra"
            android:layout_height="wrap_content"
            android:background="@drawable/rl_bg"
            android:layout_alignBottom="@+id/ll_info"
            android:layout_alignTop="@+id/tv_title"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/img"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:padding="4dp"
                android:maxHeight="30dp"
                android:maxWidth="30dp"
                android:scaleType="fitXY" />


            <ProgressBar
                android:id="@+id/pb_img"
                android:layout_width="@dimen/pb_size"
                android:layout_height="@dimen/pb_size"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:visibility="visible"

                />


        </RelativeLayout>

        <ImageView
            android:id="@+id/img_arrow"
            android:layout_width="@dimen/frwd_size"
            android:layout_height="@dimen/frwd_size"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/icon_next" />


        <me.grantland.widget.AutofitTextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/ll_info"
            android:layout_toLeftOf="@+id/img_arrow"

            android:textColor="@color/item_heading"
            android:textSize="@dimen/text_size_medium" />


        <me.grantland.widget.AutofitTextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/ll_info"
            android:layout_below="@+id/tv_title"
            android:layout_toLeftOf="@+id/img_arrow"

            android:textSize="@dimen/text_size_small" />

        <me.grantland.widget.AutofitTextView
            android:id="@+id/tv_data"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/ll_info"
            android:layout_below="@+id/tv_name"
            android:layout_toLeftOf="@+id/img_arrow"

            android:textSize="@dimen/text_size_small" />


        <LinearLayout
            android:id="@+id/ll_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/ll_btns"
            android:layout_alignLeft="@+id/ll_btns"
            android:layout_below="@+id/tv_loc"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="4dp"
            android:layout_toLeftOf="@+id/img_arrow"
            android:layout_toRightOf="@+id/rl_img"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_title_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.5"

                    android:textColor="@color/text_heading_color"
                    android:textSize="@dimen/text_size_small" />

                <TextView
                    android:id="@+id/tv_title_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.5"

                    android:textColor="@color/text_heading_color"
                    android:textSize="@dimen/text_size_small" />




                <TextView
                    android:id="@+id/tv_title_3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.5"

                    android:textColor="@color/text_heading_color"
                    android:textSize="@dimen/text_size_small" />



                <TextView
                    android:id="@+id/tv_title_4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.5"

                    android:textColor="@color/text_heading_color"
                    android:textSize="@dimen/text_size_small" />

            </LinearLayout>


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">



                <TextView
                    android:id="@+id/tv_userName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight=".5"

                    android:maxLines="1"
                    android:text="0000000"
                    android:textColor="@color/colorBlack"
                    android:textSize="@dimen/text_size_small" />


                <TextView
                    android:id="@+id/tv_userdes"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight=".5"

                    android:maxLines="1"
                    android:text="0000000"
                    android:textColor="@color/colorBlack"
                    android:textSize="@dimen/text_size_small" />


                <TextView
                    android:id="@+id/tv_usernum"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight=".5"
                    android:maxLines="1"
                    android:text="0000000"
                    android:textColor="@color/colorBlack"
                    android:textSize="@dimen/text_size_small" />



                <TextView
                    android:id="@+id/tv_userrating"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight=".5"
                    android:maxLines="1"
                    android:text="0000000"
                    android:textColor="@color/colorBlack"
                    android:textSize="@dimen/text_size_small" />


            </LinearLayout>


        </LinearLayout>


    </RelativeLayout>
</RelativeLayout>


    Code:

         recylerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
            recylerview.setItemAnimator(new DefaultItemAnimator());
            recylerview.setNestedScrollingEnabled(false);
            recylerview.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
            ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getActivity(), R.dimen.item_offset_mini);
            recylerview.addItemDecoration(itemDecoration);


    recylerview.setAdapter(itemAdapter);

//Adapter
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {

    public interface OnItemClickListener {
        void onItemClick(ItemModel item);
    }

    private List<ItemModel> items;
    private Context context;
    private OnItemClickListener listener;


    public ItemAdapter(Context context, List<ItemModel> items, OnItemClickListener listener) {
        this.items = items;
        this.context = context;
        this.listener = listener;
    }

    @Override
    public ItemAdapter.ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemAdapter.ItemViewHolder viewHolder, int i) {

        viewHolder.bind(items.get(i), listener);


        viewHolder.tv_title.setText(items.get(i).getTitle());
        viewHolder.tv_name.setText(items.get(i).getItemTitle());
        viewHolder.tv_loc.setText(items.get(i).getItemLoc());
        viewHolder.tv_name.setTextColor(Color.BLACK);
        viewHolder.tv_loc.setTextColor(Color.BLACK);


        viewHolder.tv_userName.setText(items.get(i).getUserName());
        viewHolder.tv_userdes.setText(items.get(i).getUserDes());


        FragmentUtils.setBOLDFont(viewHolder.tv_title);
        FragmentUtils.setFont(viewHolder.tv_name);
        FragmentUtils.setFont(viewHolder.tv_loc);



        FragmentUtils.setFont(viewHolder.tv_title_1);
        FragmentUtils.setFont(viewHolder.tv_title_2);
        FragmentUtils.setFont(viewHolder.tv_title_3);
        FragmentUtils.setFont(viewHolder.tv_title_4);

        // Loading profile image
        if (items.get(i).getImage().size() != 0) {

            Drawable myIcon = context.getResources().getDrawable( R.drawable.not_found );
            ImageUtils.displayImageFromUrl(context, items.get(i).getImage().get(0).getThumbnail(), viewHolder.img,myIcon, new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@android.support.annotation.Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    viewHolder.pb_img.setVisibility(View.GONE);
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    viewHolder.pb_img.setVisibility(View.GONE);
                    return false;
                }
            });

        } else {
            ImageUtils.displayImageFromResources(context, viewHolder.img, R.drawable.not_found);
            viewHolder.pb_img.setVisibility(View.GONE);
        }

    }

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

    public class ItemViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.tv_title)
        public TextView tv_title;

        @BindView(R.id.tv_name)
        public TextView tv_name;

        @BindView(R.id.tv_des)
        public TextView tv_des;

        @BindView(R.id.tv_userName)
        public TextView tv_userName;

        @BindView(R.id.tv_userLoc)
        public TextView tv_userLoc;



        @BindView(R.id.img)
        public ImageView img;

        @BindView(R.id.pb_img)
        public ProgressBar pb_img;


        @BindView(R.id.tv_title_1)
        public TextView tv_title_1;

        @BindView(R.id.tv_title_2)
        public TextView tv_title_2;

        @BindView(R.id.tv_title_3)
        public TextView tv_title_3;

        @BindView(R.id.tv_title_4)
        public TextView tv_title_4;


        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bind(final ItemModel item, final OnItemClickListener listener) {
            itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    listener.onItemClick(item);
                }

            });

        }
    }
}


///Log:

    01-23 10:14:19.343 11462-11467/com.app I/art: Do full code cache collection, code=252KB, data=241KB
    01-23 10:14:19.344 11462-11467/com.app I/art: Starting a blocking GC JitCodeCache
    01-23 10:14:19.344 11462-11467/com.app I/art: After code cache collection, code=213KB, data=181KB
    01-23 10:14:20.212 11462-11467/com.app I/art: Do partial code cache collection, code=250KB, data=207KB
    01-23 10:14:20.213 11462-11467/com.app I/art: After code cache collection, code=250KB, data=207KB
    01-23 10:14:20.213 11462-11467/com.app I/art: Increasing code cache capacity to 1024KB
    01-23 10:14:27.706 11462-11462/com.app I/Choreographer: Skipped 505 frames!  The application may be doing too much work on its main thread.
    01-23 10:14:35.519 11462-11462/com.app I/Choreographer: Skipped 464 frames!  The application may be doing too much work on its main thread.
    01-23 10:14:43.019 11462-11462/com.app I/Choreographer: Skipped 449 frames!  The application may be doing too much work on its main thread.
    01-23 10:14:50.488 11462-11462/com.app I/Choreographer: Skipped 447 frames!  The application may be doing too much work on its main thread.
13
  • are you using offset? Commented Jan 23, 2018 at 5:06
  • yes for spacing in items
    – User42590
    Commented Jan 23, 2018 at 5:07
  • If you have lots of data then you have to use handler class to bind data, Handler class will not stuck UI thread. Commented Jan 23, 2018 at 5:08
  • 1
    please post your item layout as well
    – Naveen Dew
    Commented Jan 23, 2018 at 5:09
  • 1
    @fab see my updated code
    – User42590
    Commented Jan 23, 2018 at 5:26

2 Answers 2

5

This is happening because you have a <RecyclerView> tag with wrap_content for its height inside a <NestedScrollView>. This means that every single one of your 1000 items will have a viewholder created for it, causing thousands of views to be inflated and populated. I imagine that even if you wait for all of this to happen, your app will crash with an OutOfMemoryError.

The obvious question is why you want to have a RecyclerView inside a NestedScrollView. If you had this instead:

<RelativeLayout
    ...>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recylerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

the RecyclerView would actually be able to perform view recycling and your app should become responsive.

2
  • I have a lot of information in my fragment in which I am showing/adding recylerview and want to scroll all screen with recylerview
    – User42590
    Commented Jan 23, 2018 at 6:05
  • @User42590 then you should use a fixed height for your RecyclerView tag. You must to use something other than wrap_content if you have a large number of items in the recycler
    – Ben P.
    Commented Jan 23, 2018 at 6:15
3

Yes @Ben is correct your RecyclerView is VERTICAL and having wrap_content plus 1000 items there is no sense using it inside NestedScrollView

Try simply using RecyclerView inside Relativelayout or Linearlayout

also, I can see you have given match_parent to ImageView and again giving height in parent layout in this way the everytime its getting resize which is extra load in UI

also, note that maxWidth and maxHeight won't work with match_parent

<RelativeLayout
            android:id="@+id/rl_img"
            android:layout_width="@dimen/img_wextra"
            android:layout_height="wrap_content"
            android:background="@drawable/rl_bg"
            android:layout_alignBottom="@+id/ll_info"
            android:layout_alignTop="@+id/tv_title"
            android:orientation="horizontal">
        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:padding="4dp"
            android:maxHeight="30dp"
            android:maxWidth="30dp"
            android:scaleType="fitXY" />

One more thing it's better to use placeholder instead of Progressbar for Imageview.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.