The main idea is to constantly calculate the sliding distance of recyclerView when sliding, using the current sliding distance/total length is the sliding percentage of recyclerView, using progressBar (seekBar can also) to display the percentage, the main code is as follows:

private ProgressBar progressIndicator; private float scrollX; private float percentage; private int totalDistanceX; m_rv_scenic.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { int xOffset = m_rv_scenic.computeHorizontalScrollOffset(); int range = m_rv_scenic.computeHorizontalScrollRange(); int extent = m_rv_scenic.computeHorizontalScrollExtent(); totalDistanceX = range - extent; If (percentage == 0)// Record the last sliding percentage = xOffset * 1f/totalDistanceX; scrollX = percentage * totalDistanceX + dx; if (xOffset == 0) scrollX = 0; if (xOffset == totalDistanceX) scrollX = xOffset; percentage = scrollX / totalDistanceX; // progressIndicator. SetMax (totalDistanceX); progressIndicator.setProgress((int) scrollX + totalDistanceX / 8); super.onScrolled(recyclerView, dx, dy); }});Copy the code

ProgressBar style:

<ProgressBar style="? android:attr/progressBarStyleHorizontal" android:layout_width="60dp" android:layout_height="3dp" android:layout_marginTop="5dp" android:progressDrawable="@drawable/progress_indicator" /> @drawable/progress_indicator <? The XML version = "1.0" encoding = "utf-8"? > <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/progress_bg"> <shape> <corners android:radius="10dp" /> <solid android:color="#D8DAF3" /> </shape> </item> <item android:id="@+id/progress"> <clip> <shape> <corners android:radius="10dp" /> <gradient android:angle="0" android:endColor="#B48BED" android:startColor="@color/main_color" /> </shape> </clip> </item> </layer-list>Copy the code

Effect:

Of course, this method can accurately calculate the percentage when the amount of data is small (it can be displayed in one screen), but it cannot accurately calculate the percentage when the page needs to be turned (just as the length of the scrollBar changes constantly when the page is sliding). At this time, if accurate percentage is needed, You can calculate the width of the whole RecyclerView by calculating the width of each item and then get the percentage. My business scenario does not need to be so precise, so I will implement it this way first. Besides, I have not found the reason why I cannot accurately obtain the percentage