Android Studio之多個Activity的滑動切換(二)

來源:互聯網
上載者:User

標籤:布局   分享   center   androi   touch   響應   int   man   函數   

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的滑動切換(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.