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