Android UI development Article 8-sliding effect between the left and right sides of viewflipper

Source: Internet
Author: User

How to achieve the drag between the left and right sides of the android homepage. In fact, it is easy to implement, that is, to use viewflipper to put the view you want to drag back and forth together, and then interact with the gesturedetector Gesture Recognition class to determine which view to display and add a little animation effect.

Java

public class TestFlip extends Activity implements OnGestureListener {private ViewFlipper flipper;private GestureDetector detector;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);detector = new GestureDetector(this);flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);flipper.addView(addView(R.layout.layout1));flipper.addView(addView(R.layout.layout2));flipper.addView(addView(R.layout.layout3));flipper.addView(addView(R.layout.layout4));}private View addView(int layout) {LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);View view = inflater.inflate(layout, null);return view;}@Overridepublic boolean onTouchEvent(MotionEvent event) {return this.detector.onTouchEvent(event);}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {if (e1.getX() - e2.getX() > 120) {this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));this.flipper.showNext();return true;} else if (e1.getX() - e2.getX() < -120) {this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));this.flipper.showPrevious();return true;}return false;}@Overridepublic void onLongPress(MotionEvent e) {}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {return false;}@Overridepublic void onShowPress(MotionEvent e) {}@Overridepublic boolean onSingleTapUp(MotionEvent e) {return false;}}

XML

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><ViewFlipper android:id="@+id/ViewFlipper01"android:layout_width="fill_parent" android:layout_height="fill_parent"></ViewFlipper></LinearLayout>

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.