Viewpager can be said to be a part of the Android app that uses a much larger component. It can help us to be very
Convenient to achieve sliding the effect of changing the view. Just a recent project has a need to use this, with
to be able to respond when the home page and last page are sliding , rather than the default, no matter what action
with a little research , I hope to be able to help you.
After analyzing the requirements. Found this should be with the viewpager of the page to monitor has a connection, so did a small
Demo. Experiment to find the solution to the problem.
First, a few public methods for the paging listener and the meaning of the state number in the first method, such as the following
The picture is seen.
here we focus on the first two methods, which are related to the scroll action. Detailed experimental code such as the following:
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 right:. Can be found to start with 1, ending with 0
(meaning of the numbers Please take a look at the diagram above. about state. when you swipe right in the middle page, the log information
For example, as seen below, this time from 1 has undergone 2 of the state. Finally to the 0 state. When the last page continues to slide to the right .
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzgvuz2jvzgi=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
log information such as right:, and Case 1 similar. the state changed from 1 to 0, but the second line was 2 .
, and the case 1 is the beginning of 0 (the test example is a total of 3 pages. that is, the first number on the second line represents
is the number of the page ), and the experiment is over.
On top of that, let's say that the response action can be written as follows if you want to swipe left on the first page.
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 responds to the action when it slides to the right, only the "curpage = = 0" In the inferred condition of the third method should be changed to
"Curpage = = View Size-1" can be.
In this experiment we use only part of the data, and other data is actually very practical, for example, if you want to
implement sliding to the page of a Half-time response action, the second parameter can be detected in the Onpagescrolled method
changes and so on. So move hands. Can learn the East West is still quite a lot of.
"Android Tricks 6" Viewpager sliding action response for home and last