Custom Viewpager sliding events for Android development

Source: Internet
Author: User

Tomorrow will have to work overtime, the bitter force of the procedural ape, simply talk about recently encountered a problem it.
I briefly introduced the use of viewpager+fragment in the Viewpager+fragment study notes, which was not difficult, when I realized a result as shown:

However, in the actual development of this technology may not be enough, the project will have a variety of wonderful needs, I recently encountered a strange demand, after the deal has been solved, and today to chat with you.

Because of the company project, I am here to use a demo of my own to introduce with you.
The effects we want to achieve are as follows:

There are a total of three fragment, of which two are displayed by sliding left and right, the third one to be displayed by clicking on the + in the top-top corner, and on the third fragment we go back to the first fragment by clicking on a button on the fragment. In fact, this to do is very simple, is to rewrite the Viewpager Ontouch event, in the Ontouch to determine the current item, if it is 1 cancel the left swipe action. A general idea is this. Look at the code:

 Public  class myviewpager extends viewpager {    Private intStartX;Private floatCurrentX; Public Myviewpager(Context context) {Super(context); } Public Myviewpager(context context, AttributeSet attrs) {Super(context, attrs); }@Override     Public Boolean ontouchevent(Motionevent ev) {Switch(Ev.getaction ()) { CaseMotionEvent.ACTION_DOWN:startX = (int) Ev.getx (); LOG.I ("Lenve","Action_down"); Break; CaseMOTIONEVENT.ACTION_MOVE:LOG.I ("Lenve","Action_move"); CurrentX = Ev.getx (); Break; CaseMOTIONEVENT.ACTION_UP:LOG.I ("Lenve","Action_up"); Break; }if(Getcurrentitem () = =1&& CurrentX < StartX) {return false; }Else if(Getcurrentitem () = =2) {return false; }Else{return Super. ontouchevent (EV); }    }}

I have rewritten the Onintercepttouchevent method in my project. But here rewrite onintercepttouchevent no matter use, still have to rewrite Ontouchevent event, the reason think of Sunday to study well, tomorrow also work overtime, today said so much. There are questions to welcome the discussion.

SOURCE download

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

Custom Viewpager sliding events for Android development

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.