[Android Tricks 6] sliding action response of the ViewPager homepage and the last page

Source: Internet
Author: User

ViewPager is a widely used component in Android applications. It can help us a lot

It is convenient to achieve the effect of sliding and changing the View, which is used in a recent project.

The system must be able to respond when the homepage and the last page are sliding, rather than having no action as the default one.

I have studied it a little and hope it will help you.

After analyzing the requirements, we found that this should be related to the page feed listener of ViewPager, so we made a small

Demo, through experiments to find a solution to the problem.

First, paste several public methods of the page feed listener and the meaning of the state number in the first method, as shown below:

Figure.

 

 

Here we focus on the first two methods, which are related to the scroll action. The specific experiment code is as follows:

 

private  OnPageChangeListener pageChangeListener = new OnPageChangeListener() {@Overridepublic void onPageSelected(int arg0) {// TODO Auto-generated method stub}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {// TODO Auto-generated method stubLog.d("TestActivity", arg0+","+arg1+","+arg2);}@Overridepublic void onPageScrollStateChanged(int arg0) {// TODO Auto-generated method stubLog.d("TestActivity", arg0+"");}};

When the homepage slides to the left, the log information is shown as right:. It can be found that it starts with 1 and ends with 0.

(For more information about the number, see the state description in the preceding figure .)

As shown in the following figure, the status changes from 1 to 2 and finally to 0. When the last page continues sliding to the right,

Log information, such as right:, is similar to Case 1. The status changes from 1 to 0, but the second line is based on 2

Header, and Case 1 starts with 0 (the test case is a total of three pages, that is, the number at the beginning of the second line represents

Is the number of the page.

In summary, if you want to slide to the left of the home page, the response action can be written as follows. Similarly, if you want

 

private  OnPageChangeListener pageChangeListener = new OnPageChangeListener() {@Overridepublic void onPageSelected(int arg0) {// TODO Auto-generated method stub}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {// TODO Auto-generated method stub//Log.d("TestActivity", arg0+","+arg1+","+arg2);curPage = arg0;}@Overridepublic void onPageScrollStateChanged(int arg0) {// TODO Auto-generated method stub//Log.d("TestActivity", arg0+"");if (preState == 1 && arg0 == 0 && curPage == 0) {your action...}preState = arg0;}};

When the page slides to the right, you only need to change "curPage = 0" in the judgment condition of the third method

"CurPage = view size-1.

 

In this experiment, we only use part of the data. Other data is actually very useful. For example, if you want

The second parameter can be detected in the onPageScrolled method.

So we can learn a lot of things.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.