Android Studio之多个Activity的滑动切换

Posted brucemengbm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio之多个Activity的滑动切换相关的知识,希望对你有一定的参考价值。

1、因为android界面上的全部控件一般都位于Layout控件(比方RelativeLayout)之上,而布局控件能够设置响应touch事件,所以能够通过布局控件的setOnTouchListen来截获touch事件。做进一步的处理。


2、关于界面滑动。涉及到gesture的处理,而gesture(手势)是touch事件的更高一层的事件,能够将touch事件传入GestureDetector对象进行处理,而创建GestureDetector对象,要首先创建OnGestureListener对象,在OnGestureListener的OnFling函数中能够进行手势识别。


3、详细流程。

实现OnTouchListen和OnGestureListen两个抽象类,同一时候实现当中的抽象函数就可以。

(1)IntellisenseActivity中继承抽象类



(2)创建布局控件RelativeLayout的id为relativelayout



(3)代码编写

<span style="font-size:14px;">package com.design.cjc.intellisense;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;


public class IntellisenseActivity extends ActionBarActivity implements View.OnTouchListener,GestureDetector.OnGestureListener{

    private RelativeLayout rl;
    private GestureDetector gd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intellisense);

        rl=(RelativeLayout)findViewById(R.id.relativelayout);
        rl.setOnTouchListener(this);
        rl.setLongClickable(true);          //非常重要
        gd=new GestureDetector((GestureDetector.OnGestureListener)this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_intellisense, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        //手势识别
        //e1:起点信息
        //e2:终点信息
        //velocityX:x方向移动速度
        //velocityY:y方向移动速度
        final int FLING_MIN_DISTANCE=100;
        final int FLING_MIN_VELOCITY=200;

        if(e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY){
            Intent intent = new Intent(IntellisenseActivity.this, PCCtrlActivity.class);
            startActivity(intent);
        }

        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gd.onTouchEvent(event);         //touch事件交给GestureDetector处理
        //return false;
    }
}
</span>


(3)PCCtrlActivity中做与IntellisenseActivity相近的处理

package com.design.cjc.intellisense;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;


public class PCCtrlActivity extends ActionBarActivity implements View.OnTouchListener,GestureDetector.OnGestureListener {

    private RelativeLayout rl;
    private GestureDetector gd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pcctrl);

        rl=(RelativeLayout)findViewById(R.id.relativelayout);
        rl.setOnTouchListener(this);
        rl.setLongClickable(true);          //非常重要
        gd=new GestureDetector((GestureDetector.OnGestureListener)this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_pcctrl, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        final int FLING_MIN_DISTANCE=100;
        final int FLING_MIN_VELOCITY=200;

        if(e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY){
            Intent intent = new Intent(PCCtrlActivity.this,IntellisenseActivity.class);
            startActivity(intent);
        }

        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gd.onTouchEvent(event);
    }
}

參考文献

參考文献1

參考文献2

以上是关于Android Studio之多个Activity的滑动切换的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio基础-Activity生命周期与多个Activity跳转

Android Studio基础-Activity生命周期与多个Activity跳转

Android Studio 之 Activity 的生命周期

Android studio怎么创建一个activity文件

Android Studio基础项目-两个Activity的Intent跳转与传值,并onActivityResult回传一个/多个值,与回传消息内容。

Android Studio基础项目-两个Activity的Intent跳转与传值,并onActivityResult回传一个/多个值,与回传消息内容。