安卓---右滑返回

向右滑动返回,对于屏幕过大的手机来说,在单手操作时,是一个不错的用户体验,用户不必再费力的或者用另一个手去点击屏幕左上角的返回按钮或者,手机右下角的返回按钮,轻轻向右滑动屏幕即可返回上一页,这个功能如今大部分APP都已经支持啦

不多说上代码

主函数

package com.example.myfragment;

import com.rain.xiaoguo.yhfh;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class xxzx extends Activity{
    
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xxzx);

        /* 显示App icon左侧的back键 */
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        final TextView tv = (TextView) findViewById(R.id.tv_xxzx);
        Button btn = (Button) findViewById(R.id.btn_xxzx);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                tv.setText("点击反映了");
            }
        });
                           
     }
     //顶部返回键
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
//    //这里的类调用有问题
//    yhfh yh = new yhfh(this);
//
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent event) {        
//        return yh.dispatchTouchEvent(event);
//        //return super.dispatchTouchEvent(event);
//    }
     
    // 手指上下滑动时的最小速度
    private static final int YSPEED_MIN = 1000;
    // 手指向右滑动时的最小距离
    private static final int XDISTANCE_MIN = 50;
    // 手指向上滑或下滑时的最小距离
    private static final int YDISTANCE_MIN = 100;
    // 记录手指按下时的横坐标。
    private float xDown;
    // 记录手指按下时的纵坐标。
    private float yDown;
    // 记录手指移动时的横坐标。
    private float xMove;
    // 记录手指移动时的纵坐标。
    private float yMove;
    // 用于计算手指滑动的速度。
    private VelocityTracker mVelocityTracker;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        createVelocityTracker(event);
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDown = event.getRawX();
            yDown = event.getRawY();
            break;
        case MotionEvent.ACTION_MOVE:
            xMove = event.getRawX();
            yMove = event.getRawY();
            // 滑动的距离
            int distanceX = (int) (xMove - xDown);
            int distanceY = (int) (yMove - yDown);
            // 获取顺时速度
            int ySpeed = getScrollVelocity();
            // 关闭Activity需满足以下条件:
            // 1.x轴滑动的距离>XDISTANCE_MIN
            // 2.y轴滑动的距离在YDISTANCE_MIN范围内
            // 3.y轴上(即上下滑动的速度)<XSPEED_MIN,如果大于,则认为用户意图是在上下滑动而非左滑结束Activity
            if (distanceX > XDISTANCE_MIN
                    && (distanceY < YDISTANCE_MIN && distanceY > -YDISTANCE_MIN)
                    && ySpeed < YSPEED_MIN) {
                finish();
            }
            break;
        case MotionEvent.ACTION_UP:
            recycleVelocityTracker();
            break;
        default:
            break;
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * 创建VelocityTracker对象,并将触摸界面的滑动事件加入到VelocityTracker当中。
     * 
     * @param event
     * 
     */
    private void createVelocityTracker(MotionEvent event) {
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);
    }

    /**
     * 回收VelocityTracker对象。
     */
    private void recycleVelocityTracker() {
        mVelocityTracker.recycle();
        mVelocityTracker = null;
    }

    /**
     * 
     * @return 滑动速度,以每秒钟移动了多少像素值为单位。
     */
    private int getScrollVelocity() {
        mVelocityTracker.computeCurrentVelocity(1000);
        int velocity = (int) mVelocityTracker.getYVelocity();
        return Math.abs(velocity);
    }

     
     
}
View Code
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/jbshape" >

    <TextView
        android:id="@+id/tv_xxzx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="消息中心" />
    
    <Button 
        android:id="@+id/btn_xxzx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮" />

</LinearLayout>
View Code

 这里我试着把他单独建一个类然后再调用,可是dispatchTouchEvent提前消耗了页面的ontouch,导致其他的触摸点击事件都不能如愿的完成,如果高手有好方法可以共享一下

 

下面介绍一种不要dispatchTouchEvent的方法

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/jbshape"
    android:id="@+id/bj_sbztxx" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设备状态信息" />

</LinearLayout>
View Code

主函数

package com.example.myfragment;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;

public class sbztxx extends Activity implements OnTouchListener{
    
    //手指向右滑动时的最小速度  
    private static final int XSPEED_MIN = 200;        
    //手指向右滑动时的最小距离  
    private static final int XDISTANCE_MIN = 150;        
    //记录手指按下时的横坐标。  
    private float xDown;       
    //记录手指移动时的横坐标。  
    private float xMove;        
    //用于计算手指滑动的速度。  
    private VelocityTracker mVelocityTracker;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sbztxx);

        /* 显示App icon左侧的back键 */
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        
        LinearLayout ll = (LinearLayout) findViewById(R.id.bj_sbztxx);
        ll.setOnTouchListener(this);

    }

    // 顶部返回键
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        createVelocityTracker(event);  
        switch (event.getAction()) {  
        case MotionEvent.ACTION_DOWN:  
            xDown = event.getRawX();  
            break;  
        case MotionEvent.ACTION_MOVE:  
            xMove = event.getRawX();  
            //活动的距离  
            int distanceX = (int) (xMove - xDown);  
            //获取顺时速度  
            int xSpeed = getScrollVelocity();  
            //当滑动的距离大于我们设定的最小距离且滑动的瞬间速度大于我们设定的速度时,返回到上一个activity  
            if(distanceX > XDISTANCE_MIN && xSpeed > XSPEED_MIN) {  
                finish();  
            }  
            break;  
        case MotionEvent.ACTION_UP:  
            recycleVelocityTracker();  
            break;  
        default:  
            break;  
        }  
        return true;  
    }
    
    /** 
     * 创建VelocityTracker对象,并将触摸content界面的滑动事件加入到VelocityTracker当中。 
     *  
     * @param event 
     *         
     */  
    private void createVelocityTracker(MotionEvent event) {  
        if (mVelocityTracker == null) {  
            mVelocityTracker = VelocityTracker.obtain();  
        }  
        mVelocityTracker.addMovement(event);  
    }  
      
    /** 
     * 回收VelocityTracker对象。 
     */  
    private void recycleVelocityTracker() {  
        mVelocityTracker.recycle();  
        mVelocityTracker = null;  
    }  
      
    /** 
     * 获取手指在content界面滑动的速度。 
     *  
     * @return 滑动速度,以每秒钟移动了多少像素值为单位。 
     */  
    private int getScrollVelocity() {  
        mVelocityTracker.computeCurrentVelocity(1000);  
        int velocity = (int) mVelocityTracker.getXVelocity();  
        return Math.abs(velocity);  
    }
    
}
View Code

到处借鉴,其实差不多方法。当然网上还有用SwipeBack方法的!自行百度吧

原文地址:https://www.cnblogs.com/rainday1/p/5484427.html