ViewPager sets sliding page turning distance (pro-test valid!)

Keywords: Android

Foreword: The owner said that the viewpager of android project is not sensitive and needs a long sliding distance to turn the page. It is required to shorten the sliding distance and turn the page in a shorter distance.

This thing is really not easy to do. I checked a lot of information on the Internet and tried a lot of methods. Until the following article was reproduced, it solved my problem. Note that the source code of the viewpager needs to be modified, that is to say, the source code of the viewpager should be copied to the project for modification. (The source code of PageAdapter is also copied).

Kind testing, effective!!

From: http://blog.csdn.net/focuman/article/details/37528039

I've never had any big problems with ViewPager before, and it works well. Recently, in the company's new generation products (hardware products, resolution 2560*1440), ViewPager control is widely used for high resolution and horizontal screen application development, but the problem arises. In the process of turning pages, it needs to slide more than half of the resolution of the screen (2560/2=1280) to turn pages, which greatly affects the user experience.

First, the application scenario is briefly introduced.

1. Hardware: The company develops independently, the touch screen uses infrared (so the touch effect is not as smooth as the capacitive screen), and the display screen uses high-definition 2K display screen.

2. Software System: Android 4.2.2

Companies need to customize their own software for users to use. For the ViewPager control, Android Engineers never know how to deal with this problem. There are only two ways: 1. Customizing controls and 2. Modifying ViewPager. Today, I went to the source code of ViewPager, and finally found the point where I could change the problem. The following is the main point of view Pager's source code: ViewPager's source code, ViewPager's source code, ViewPager's source code, ViewPager's source code, View

  1. private int determineTargetPage(int currentPage, float pageOffset, int velocity, int deltaX) {  
  2.         int targetPage;  
  3.         if (Math.abs(deltaX) > mFlingDistance && Math.abs(velocity) > mMinimumVelocity) {  
  4.             targetPage = velocity > 0 ? currentPage : currentPage + 1;  
  5.         } else {  
  6.             final float truncator = currentPage >= mCurItem ? 0.4f : 0.6f;  
  7.             targetPage = (int) (currentPage + pageOffset + truncator);  
  8.         }  
  9.   
  10.         if (mItems.size() > 0) {  
  11.             final ItemInfo firstItem = mItems.get(0);  
  12.             final ItemInfo lastItem = mItems.get(mItems.size() - 1);  
  13.   
  14.             // Only let the user target pages we have items for  
  15.             targetPage = Math.max(firstItem.position, Math.min(targetPage, lastItem.position));  
  16.         }  
  17.   
  18.         return targetPage;  
  19.     }  

The determineTargetPage method calculates which page to slide to next. This method call is based on the event MotionEvent.ACTION_UP. Let's start with the meaning of the parameter:

CurrtPage: The page currently displayed by ViewPager

Page Offset: Page offset for user sliding
velocity: Sliding rate

DeltaX: The Distance of X-Direction Moving

After debug the release, the problem was found in the parameters of 0.4f and 0.6f. It is concluded that 0.6f represents the offset that users can turn pages when sliding, so it is not difficult to understand why they should slide half screen or more.

The modification method is as follows:

  1. /** Minimum switchable Page offset**/  
  2.     private float minPageOffset = 0.6f;  
  3.       
  4.     public void setMinPageOffset(float pageOffset){  
  5.         minPageOffset = pageOffset;  
  6.     }  
  7.     private int determineTargetPage(int currentPage, float pageOffset, int velocity, int deltaX) {  
  8.         int targetPage;  
  9.         if (Math.abs(deltaX) > mFlingDistance && Math.abs(velocity) > mMinimumVelocity) {  
  10.             targetPage = velocity > 0 ? currentPage : currentPage + 1;  
  11.         } else {  
  12.             final float truncator = currentPage >= mCurItem ? 1.0f - minPageOffset : minPageOffset;  
  13.             targetPage = (int) (currentPage + pageOffset + truncator);  
  14.         }  
  15.   
  16.         if (mItems.size() > 0) {  
  17.             final ItemInfo firstItem = mItems.get(0);  
  18.             final ItemInfo lastItem = mItems.get(mItems.size() - 1);  
  19.   
  20.             // Only let the user target pages we have items for  
  21.             targetPage = Math.max(firstItem.position, Math.min(targetPage, lastItem.position));  
  22.         }  
  23.   
  24.         return targetPage;  
  25.     }  
In this way, user sliding offset can be set and page can be switched, so as to solve the problem that the resolution of large screen sliding is not smooth.

If you have any questions, please ask me again. Thank you.

Posted by hippypink on Mon, 25 Mar 2019 22:15:29 -0700