1

My recyclerview scrolling is laggy. It lags when I started scrolling by touching the Recyclerview, but when doesn't lag when started from the view above it. I've also disabled nestedscrolling on recyclerview.

Here is my layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:overScrollMode="never">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="HAHHAHA"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/mRecyclerViewMain"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:background="@android:color/transparent">

            </android.support.v7.widget.RecyclerView>


        </LinearLayout>

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


</FrameLayout>

Here is a video to the lag. It's like the scroll is skipping some layout.

Charlie
  • 148
  • 1
  • 16

1 Answers1

2

Modify your xml like this:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            xmlns:app="http://schemas.android.com/apk/res-auto">


                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/mRecyclerViewMain"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior"
                        android:background="@android:color/transparent">

                    </android.support.v7.widget.RecyclerView>

        </FrameLayout>

and set your textView in recyclerView's header,it will resolve your issue

<TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="HAHHAHA"/>

in adapter class set viewType for header and items list

@Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (layoutInflater == null)
            layoutInflater = LayoutInflater.from(parent.getContext());

        if (viewType == 0) {
            // bind your headerview
        }
        if (viewType == 1) {
            //bind your recyclerview
        } 
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderHolder)
            // viewHolder for header view
        if (holder instanceof ListHolder) {
            // viewholder for list
        } 
    }

using this you can pass viewType

@Override
    public int getItemViewType(int position) {

        if (position == 0)
            return 0;
        else if (ArrayList.size() == 0)
            return 1;

    }
Nilam Vaddoriya
  • 433
  • 2
  • 10
  • performance is a little bit better by setting .setHasFixedSize(true) in recyclerview thanks anyway – Charlie Aug 24 '17 at 05:17