Search code examples
androidandroid-recyclerviewandroid-viewpager

How to fix unexpected behavior on viewpager in recyclerview when fast scrolling?


My RecyclerView has viewpager on each row for several swipeable buttons.

When I scroll fast in a default state, it works fine.

But some views (pages) are partially shown on default pages. I can watch them they are swiped to default page from swiped page before (Actually not theirs. Single row which is recycled in above)

Sometimes really small 'partial view' is appeared corner even if they were set as default like below.

Example][1 Left top corner. I didn't even touch that row at all.

I suspect it's inflating speed, so I used SparseIntArray, Remove another view, Make small array for the test, Remove resources on views. But all were worthless.

Also results in SparseIntArray are all normal as expected.

Here are some codes below. Also, all xmls are

PageWidth

public float getPageWidth(int position)
    {
        if(position != 3 && position != 0)
        {
            return 0.15f;
        }
        else
        {
            return 1f;
        }
    }

Init and Load

@Override
    public void onBindViewHolder(@NonNull mAdapter.ClipViewHolder clipViewHolder, int position)
    {
        if(stateArray.get(position) > 0 && stateArray.get(position) <= 4)
        {
            clipViewHolder.myPager.setCurrentItem(stateArray.get(position) - 1);
        }
        else
        {
            clipViewHolder.myPager.setCurrentItem(3);
            stateArray.append(position, clipViewHolder.myPager.getCurrentItem() + 1);
        }
    }

Save

@Override
    public void onViewRecycled(@NonNull ClipViewHolder holder)
    {
        stateArray.put(holder.getAdapterPosition(), holder.myPager.getCurrentItem() + 1);
        super.onViewRecycled(holder);
    }

I want to show them smoothly no matter what swipe state was, no matter how fast scroll speed. Is there any solution?


Solution

  • //Put below code in XML file    
    <android.support.v4.widget.NestedScrollView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
      <android.support.v7.widget.RecyclerView
       android:id="@+id/recycleView"
       android:nestedScrollingEnabled="false"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
    />
    
    // put below lines in your activity as
    /* catche memory */
    recyclerView.setItemViewCacheSize(200);
    recyclerView.setDrawingCacheEnabled(true);
    recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    
    RecyclerView.LayoutManager mLayoutManager = new 
       LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    
    //for smooth recycler
    recyclerView.setNestedScrollingEnabled(false);  // Main Line
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.smoothScrollBy(0, 10);