"Android Tricks 6" Viewpager sliding action response for home and last

Source: Internet
Author: User

Viewpager can be said to be a part of the Android application using the comparison of GF, it can help us very

Convenient to achieve sliding The effect of changing the view, just recently A project has a need to use this, with

to be able to respond when the homepage and last page are sliding , rather than the default, without any action,

to a little research , hope can be helpful to everyone.

After analyzing the requirement, we found that this should be related to the Viewpager, so I made a small

demo, by experiment to find a solution to the problem.

First, a few public methods to post a page-change listener and the meaning of the state number in the first method are as follows

shown in the figure.

here we focus on the first two methods, which are related to 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-gene Rated Method Stublog.d ("Testactivity", arg0+ "," +arg1+ "," +arg2 "); @Overridepublic void onpagescrollstatechanged (int arg0) {//TODO auto-generated method Stublog.d ("Testactivity", arg0+ " ");}};

When you swipe left on the home page, the log information is as right: you can find it starting with 1 and ending with 0

(meaning of the numbers please refer to the diagram above about state. when you swipe right in the middle page, the log information

As shown below, this time from 1 through 2 of the state, and finally to the 0 State; When you continue to swipe to the right on the last page ,


log information such as right:, similar to Case 1, The state changes from 1 to 0, but the second line is 2 .

And the Case 1 is the beginning of 0 (the test case is a total of 3 pages, which means the second the line at the beginning of the number represents

is the number of the page ), and the experiment is over.

In all, if you want to move to the left in the first page, the response action can be written as follows, if you want to in the last

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-gene Rated 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;}};

The page to the right to respond to the action, as long as the judgment in the third method of the condition "curpage = = 0" changed to

"Curpage = = View Size-1" can be.

In this experiment we used only a subset of the data, and other data is actually useful, for example, if you want to

implement sliding to the page of a Half-time response action, you can detect the second parameter in the Onpagescrolled method

change and so on, so move hands, can learn the East The West is still quite a lot.



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.