Android slide to the right to close the page, android to close the page
Preface:
The simplest example is used to illustrate this problem.
1. Add the default Layout to the Activity.
2. Implement the right slide to close the Activity logic in the custom Layout.
Code directly!
Custom Layout HFFinishRelativeLayout!
Package com. huofar. widget; import android. content. context; import android. content. res. typedArray; import android. support. v4.app. fragmentActivity; import android. util. attributeSet; import android. view. motionEvent; import android. view. viewGroup; import android. widget. relativeLayout; import com. huofar. util. logUtil;/*** Created by zhangxiwei on 14/11/25. */public class HFFinishRelativeLayout extends RelativeLayout {private static final String TAG = LogUtil. makeLogTag (HFFinishRelativeLayout. class); public interface ScrollLeftFinishListener {public void finishPage ();} FragmentActivity; private ScrollLeftFinishListener scrollLeftFinishListener; public void listener (ScrollLeftFinishListener listener) {this. scrollLeftFinishListener = scrollLeftFinishListener;} // The sliding distance and coordinate private float xDistance, yDistance, xLast, yLast; public HFFinishRelativeLayout (Context context) {super (context);} public Context, attributeSet attrs) {super (context, attrs);} public void attachToActivity (FragmentActivity) {this. activity = activity; TypedArray a = activity. getTheme (). obtainStyledAttributes (new int [] {android. r. attr. windowBackground}); int background =. getResourceId (0, 0);. recycle (); ViewGroup decor = (ViewGroup) activity. getWindow (). getDecorView (); ViewGroup decorChild = (ViewGroup) decor. getChildAt (0); decorChild. setBackgroundResource (background); decor. removeView (decorChild); addView (decorChild); decor. addView (this) ;}@ Override public boolean onInterceptTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: break; case MotionEvent. ACTION_MOVE:} return super. onInterceptTouchEvent (event );}
@Override
public boolean dispatchTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: xDistance = yDistance = 0f; xLast = event.getX(); yLast = event.getY(); break; case MotionEvent.ACTION_MOVE: final float curX = event.getX(); final float curY = event.getY(); xDistance += Math.abs(curX - xLast); yDistance += Math.abs(curY - yLast); if (curX > xLast && xDistance > yDistance && xDistance > 300) { if(scrollLeftFinishListener != null){ xLast = curX; yLast = curY; scrollLeftFinishListener.finishPage(); return true; } } xLast = curX; yLast = curY; } return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: break; } <strong><span style="color:#ff6666;">return true;</span></strong> }}Focus on the above red bold fields.
Each time I slide to the right, I slide Px to close the operation. I just need to use the Activity implementation excuse to directly finish it!
Call in Activity:
Package com. huofar. activity; import android. OS. bundle; import android. OS. handler; import android. support. v4.app. fragmentActivity; import android. view. layoutInflater; import com. huofar. r; import com. huofar. widget. HFFinishRelativeLayout;/*** Created by zhangxiwei on 14/11/25. */public class HFBaseActivity extends FragmentActivity implements HFFinishRelativeLayout. scrollLeftFinishListener {private boolean isFinishScrollLeft; HFFinishRelativeLayout hfFinishRelativeLayout; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); isFinishScrollLeft = true; // delay in order to wait for the request to block the return slide accept parameter new Handler (). postAtTime (new Runnable () {@ Override public void run () {setFinishScrollLeft (isFinishScrollLeft) ;}}, 1000);} public void setFinishScrollLeft (boolean isFinishScrollLeft) {this. isFinishScrollLeft = isFinishScrollLeft; if (isFinishScrollLeft) {hfFinishRelativeLayout = (HFFinishRelativeLayout) LayoutInflater. from (this ). inflate (R. layout. activity_finish_base, null); hfFinishRelativeLayout. attachToActivity (this); hfFinishRelativeLayout. setScrollLeftFinishListener (this) ;}}@ Override public void finishPage () {finish ();}}
This is where all the code is. One second is delayed to accept the isFinishScrollLeft variable. I modified it in an existing project, this is to ensure that you do not need to close the transfer of this variable on some pages !. The method is a bit stupid. You can provide a good solution. Thank you!
The following is a headache. Why:
Anti-spoofing knowledge: 1. dispatchTouchEvent 2. onInterceptTouchEvent 3. onTouchEvent passing event directly gives you a learning address
Http://www.cnblogs.com/sunzn/archive/2013/05/10/3064129.html
In addition, you can write a demo to practice it or read more about it on google, that is, transfer the event, and then accept it at dispatchTouchEvent. Then close it directly.
In fact, there is nothing to help the children's shoes who want this function.
If you have any questions, follow the prompts.