google / flexbox-layout

Flexbox for Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Scroll view sluggish and jittery

LeoXJ978 opened this issue · comments

Issues and steps to reproduce

I use FlexboxLayoutManager with RecyclerView.Adapter to show short texts of roughly 4x20 on the screen at any time. To isolate the issue from data loading, I tested with a static set of items about 1000 loaded. As I scroll down, new items are shown from below and older items go out from the top. When I scroll fast, the views become very sluggish and jittery with abrupt stops in between. Interestingly, the sluggishness only seems to happen when I scroll down (scrolling up appears much better), even scrolling down again after having scrolled back up.

I have a RecyclerView within a LinearLayout, as

    <LinearLayout
        android:id="@+id/activity_words"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/words_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="start"
            android:scrollbars="vertical"
            android:verticalSpacing="10dp" >
        </androidx.recyclerview.widget.RecyclerView>
    </LinearLayout>

Each item for the RecyclerView is defined as:

    <LinearLayout
        android:id="@+id/lauout"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/list_padding">

        <TextView
            android:id="@+id/term"
            android:text="@{word.term}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="start"
            android:textSize="@dimen/title_size"
            android:minWidth="50dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:textStyle="normal|bold"/>
    </LinearLayout>

In Java code,

                FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
                layoutManager.setFlexDirection(FlexDirection.ROW);
                layoutManager.setJustifyContent(JustifyContent.FLEX_START);
                layoutManager.setItemPrefetchEnabled(true);
                binding.wordsList.setLayoutManager(layoutManager);
                adapter.setTermSize(25f);
                binding.wordsList.setAdapter(adapter);

Expected behavior

I expect to see smoother scrolling

As I browsed through the code, without fully comprehending it, I found something I can't explain. In FlexboxLayoutManager.java, view cache is cleared while scrolling:

    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler,
            RecyclerView.State state) {
        if (isMainAxisDirectionHorizontal() ||
                (mFlexWrap == FlexWrap.NOWRAP && !isMainAxisDirectionHorizontal())) {
            int scrolled = handleScrollingMainOrientation(dy, recycler, state);
            mViewCache.clear(); 
            return scrolled;
        } else {
            int scrolled = handleScrollingSubOrientation(dy);
            mAnchorInfo.mPerpendicularCoordinate += scrolled;
            mSubOrientationHelper.offsetChildren(-scrolled);
            return scrolled;
        }
    }

I commented out the line mViewCache.clear(); in my local copy, then things appear drastically improved for me, at least scrolling down again becomes very smooth, though I don't know yet if it would cause other problems.

Version of the flexbox library

3.0.0