Adds left and right gesture recognition and activity gesture recognition for the activity.

Source: Internet
Author: User

Adds left and right gesture recognition and activity gesture recognition for the activity.

Added left and right gesture recognition for the activity in android development, for example, sliding right to close the current page

/** For left and right gestures * 1. copy the following content to the target Activity * 2. the onCreate () of the target Activity calls initGesture () * 3. the target Activity must be implements OnTouchListener, OnGestureListener */private listener; private int verticalMinDistance = 180; private int minVelocity = 0; private void initGesture () {listener = new listener) this);} public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {if (e1.getX ()-e2.getX ()> verticalMinDistance & Math. abs (velocityX)> minVelocity) {// switch Activity // Intent intent = new Intent (ViewSnsActivity. this, UpdateStatusActivity. class); // startActivity (intent); // Toast. makeText (this, "gesture to the left", Toast. LENGTH_SHORT ). show ();} else if (e2.getX ()-e1.getX ()> verticalMinDistance & Math. abs (velocityX)> minVelocity) {// switch Activity // Intent intent = new Intent (ViewSnsActivity. this, UpdateStatusActivity. class); // startActivity (intent); // Toast. makeText (this, "gesture to the right", Toast. LENGTH_SHORT ). show (); finish (); overridePendingTransition (R. anim. push_right_in, R. anim. push_right_out);} return false;} @ Override public void onLongPress (MotionEvent arg0) {// TODO Auto-generated method stub} @ Override public boolean onScroll (MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {// TODO Auto-generated method stub return false;} @ Override public void onShowPress (MotionEvent arg0) {// TODO Auto-generated method stub} @ Override public boolean onSingleTapUp (MotionEvent arg0) {// TODO Auto-generated method stub return false ;} @ Override public boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stub return mGestureDetector. onTouchEvent (event) ;}@ Override public boolean onDown (MotionEvent arg0) {// TODO Auto-generated method stub return false;} @ Override public boolean dispatchTouchEvent (MotionEvent ev) {mGestureDetector. onTouchEvent (ev); return super. dispatchTouchEvent (ev );}



How can multiple activities jump back and forth after a gesture sliding switch is attached ??

ViewPager + Fragment is used to replace all three activities A, B, and C with Fragment. N times have been used
 
In android, how does one implement a sliding activity? How does one slide left and right to implement activity switching?

Do you want to slide the activity or the page? If it is an activity, you can add a gesture to the current page, implement activity switching during sliding, and then add a switchover animation to the activity. If it is a qq effect, you can use a tab to do it, the implementation is very good, or activitygroup, you can also use fragment to do, these implementations are good. For page slide switching, you can use viewpager. This effect is very good. If the code is strong, you can use viewgroup scrolling to achieve the effect.

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.