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>