Recently, the project needs to implement the pull-up function. The first thing that comes with Android is the s0000ingdrawer, which is also implemented. However, the problem is to set the background color, handler and content are two different parts. As a result, the image is made into two parts, and the two parts are not a whole, and I pull them up, it needs to implement a horizontal scroll function, and SlidingDrawer has other restrictions. At the same time, after api 17 is officially provided, this function has been abandoned, so it is necessary to customize such a pull function.
Here, I inherit LinearLayout and implement GestureDetector. OnGestureListener to implement the pull-up function. The following code is used:
Package com. example. test; import android. content. context; import android. OS. asyncTask; import android. util. attributeSet; import android. view. gestureDetector; import android. view. motionEvent; import android. widget. linearLayout; import android. widget. relativeLayout; public class PanelBom extends LinearLayout implements GestureDetector. onGestureListener {GestureDetector mGesture = null; private boolean isScroll Ing = false; private int MAX_HEIGHT = 80; // The maximum drag height. The current layout is located at-80 under the parent layout. This is only a debugging parameter and this variable is dynamically set. Private float mScrollX; // slider sliding distance from public PanelBom (Context context) {super (context); init ();} public PanelBom (Context context, AttributeSet attrs) {super (context, attrs); init () ;}// initialize some public void init () {mGesture = new GestureDetector (this); mGesture. setIsLongpressEnabled (false); setBackgroundResource (R. drawable. button_bar) ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {if (MotionEvent. ACTIO N_UP = event. getAction () & isScrolling = true) {RelativeLayout. layoutParams layoutParams = (RelativeLayout. layoutParams) getLayoutParams (); // scale back if (layoutParams. bottomMargin <-MAX_HEIGHT/2) {new asynmove(cmd.exe cute (-20); // negative -- down} else {new asynmove(cmd.exe cute (20 ); // positive -- up} return mGesture. onTouchEvent (event);} // @ Overridepublic boolean onDown (MotionEvent e) {mScrollX = 0; isScrolling triggered when the Touch is down = False; // change it to true, otherwise the event will not be passed down. return true;} // triggered when sliding is not completed. @ Overridepublic void onShowPress (MotionEvent e) {}@ Overridepublic boolean onSingleTapUp (MotionEvent e) {RelativeLayout. layoutParams layoutParams = (RelativeLayout. layoutParams) getLayoutParams (); // The description is above, and you need to go down to if (layoutParams. bottomMargin> = 0) {new asynmove(cmd.exe cute (-20); // negative -- down} else {new asynmove(cmd.exe cute (20); // positive -- up} return true ;} // T @ Overridepublic boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {isScrolling = true; mScrollX + = distanceY; RelativeLayout. layoutParams layoutParams = (RelativeLayout. layoutParams) getLayoutParams (); layoutParams. bottomMargin + = mScrollX; if (layoutParams. bottomMargin> = 0) {isScrolling = false; // if it is too long, you do not need to execute AsynMove layoutParams. bottomMargin = 0;} else if (la YoutParams. bottomMargin <=-MAX_HEIGHT) {// if it is too long, you do not need to execute AsynMove. isScrolling = false; layoutParams. bottomMargin =-MAX_HEIGHT;} setLayoutParams (layoutParams); return false;} // triggered when Touch is down without moving @ Overridepublic void onLongPress (MotionEvent e) {} // triggered when up is triggered when a sliding distance is reached. @ Overridepublic boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {return false;} // asynchronously update the layout location class AsynMove extends AsyncTask <Integer, Integer, void >{@ Overrideprotected Void doInBackground (Integer... params) {int times = 0; int divi = Math. abs (params [0]); if (MAX_HEIGHT % divi = 0) // divisible by times = MAX_HEIGHT/Math. abs (params [0]); elsetimes = MAX_HEIGHT/divi + 1; // There is a remainder for (int I = 0; I <times; I ++) {publishProgress (params [0]); try {Thread. sleep (divi);} catch (InterruptedException e) {e. printStackTrace () ;}} return null ;}@ Overrideprotected void onProgressUpdate (Integer... values) {RelativeLayout. layoutParams layoutParams = (RelativeLayout. layoutParams) getLayoutParams (); if (values [0] <0) {layoutParams. bottomMargin = Math. max (layoutParams. bottomMargin + values [0],-MAX_HEIGHT);} else {layoutParams. bottomMargin = Math. min (layoutParams. bottomMargin + values [0], 0);} setLayoutParams (layoutParams); super. onProgressUpdate (values );}}}
Main Interface:
package com.example.test;import android.app.Activity;import android.os.Bundle;import android.widget.RelativeLayout;public class Main5 extends Activity{RelativeLayout Parent;PanelBom panelBom;RelativeLayout.LayoutParams parentParams;RelativeLayout.LayoutParams paneBomParams;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Parent = new RelativeLayout(getApplicationContext());panelBom = new PanelBom(getApplicationContext());parentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);paneBomParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 150);paneBomParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);paneBomParams.bottomMargin = -80;Parent.addView(panelBom, paneBomParams);setContentView(Parent, parentParams);}}
The implementation result is:
Drag and click to implement the pull up function, as shown below: