帶有ListView的介面左右滑動,切換介面。

來源:互聯網
上載者:User

 帶有ListView介面左右滑動,切換介面

 

相信大家在做OnGestureListener滑動切換視窗的時候,會遇到這樣的問題。就是當介面中含有listview的時候,OnGestureListener的左右觸屏滑動就被listview自己吃掉了。

翻看api協助文檔和自己的一些理解,決定從對listview重寫開始,開解決這個頭疼的問題。

 

測試的源碼:

http://download.csdn.net/detail/wanli_smile/4182584(推薦這個)

http://download.csdn.net/detail/wanli_smile/4178852

 

開始說明了:建立一個baseatvity實現Gesture滑動切換介面,讓其他的activity繼承它,這樣其他的activity也可以繼承它的滑動切換功能,提高代碼複用。

下面是BaseActivity:

package com.gesturetest;import android.app.Activity;import android.content.Intent;import android.util.Log;import android.view.GestureDetector;import android.view.GestureDetector.OnGestureListener;import android.view.MotionEvent;/** *  * @author liwan *所有activity的基類。繼承這個activity的都具有滑動功能哦 */public class BaseActivity extends Activity implements OnGestureListener{/** * 手勢監聽 */GestureDetector gestureDetector=new GestureDetector(this);/** * 標誌位 */public static int flag=0;@SuppressWarnings("rawtypes")/** * 所有切換的activity */static Class[] myClass=new Class[]{TestActivity.class,GestureTestActivity.class};@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubreturn gestureDetector.onTouchEvent(event);}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubreturn false;}@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}@Override/** * 滑動事件的處理 */public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {  Log.i("Fling", "baseactivity");    //左滑動        if (e1.getX() - e2.getX() > 80||e1.getY()-e2.getY()>80) {                          flag=(--flag+myClass.length)%myClass.length;            Intent intent=new Intent(getBaseContext(), myClass[flag]);            Log.d("flag",flag+"");            startActivity(intent);                       return true;          }         //右滑動        else if (e1.getX() - e2.getX() <-80||e1.getY()-e2.getY()<-80) {          Log.i("Fling", "baseactivity");                   flag=++flag%myClass.length;            Intent intent=new Intent(getBaseContext(),myClass[flag]);            startActivity(intent);            Log.d("flag",flag+"");            return true;          }          return true;  }}

繼承它的兩個activity。分別如下:

GestureTestActivity引入了我們自訂的gesturelist

GestureTestActivity

package com.gesturetest;import android.os.Bundle;/** *  * @author liwan *這裡的GestureList是在xml布局的 */public class GestureTestActivity extends BaseActivity {/** * 自訂的listview */GestureList gestureListView;/** * 自訂的adapter */MyBaseAdapter myBaseAdapter;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        myBaseAdapter=new MyBaseAdapter(this);        gestureListView=(GestureList)this.findViewById(R.id.list1);        gestureListView.setAdapter(myBaseAdapter);    }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="150dp"        android:text="第一個activity,在這裡滑動是調用activity的手勢處理哦,在listview裡面滑動式調用list自己的手勢" /><com.gesturetest.GestureList   android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/list1"></com.gesturetest.GestureList></LinearLayout>

TestActivity的代碼如下。它引用了gesturelist,但是它是使用addview()引入的。

package com.gesturetest;import android.os.Bundle;import android.widget.LinearLayout;/** * 這裡的GestureList是用代碼引入的,沒有在xml布局 * @author liwan * */public class TestActivity extends BaseActivity {/** * 自訂的listview */GestureList gestureListView;/** * 自訂的adapter */MyBaseAdapter myBaseAdapter;LinearLayout linearLayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.test);linearLayout=(LinearLayout)this.findViewById(R.id.li);gestureListView =new GestureList(TestActivity.this);myBaseAdapter=new MyBaseAdapter(TestActivity.this);gestureListView.setAdapter(myBaseAdapter);linearLayout.addView(gestureListView);}}

<?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:id="@+id/li">    <TextView        android:layout_width="fill_parent"        android:layout_height="100dp"        android:text="第二個activity,在這裡滑動是調用activity的手勢處理哦,在listview裡面滑動式調用list自己的手勢" /></LinearLayout>

下面說下核心了,也就是我們的GestureList,由於它的重寫,實現了它也可以捕獲手勢事件了。在onTouchEvent方法裡,將處理交給手勢監聽器。

在xml布局裡面使用GestureList,預設的會調用這個構造方法GestureList(Context context, AttributeSet attrs)。一定要重寫這個構造方法。

代碼如下:

package com.gesturetest;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.GestureDetector;import android.view.MotionEvent;import android.widget.ListView;/** *  * @author liwan *自訂的listview,帶有手勢 */class GestureList extends ListView {int flag=BaseActivity.flag;Context context;GestureDetector gestureDetector;/** * 在xml布局裡面使用GestureList,預設的會調用這個構造方法 * @param context * @param attrs */public GestureList(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubthis.context=context;gestureDetector=new GestureDetector(context,new Gesture(context));Log.d("12","2");}public GestureList(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubthis.context=context;gestureDetector=new GestureDetector(context,new Gesture(context));Log.d("12","3");}public GestureList(Context context) {super(context);// TODO Auto-generated constructor stubthis.context=context;gestureDetector=new GestureDetector(context,new Gesture(context));Log.d("12","1");}@Overridepublic boolean onTouchEvent(MotionEvent ev) {if(gestureDetector.onTouchEvent(ev)) return true;return super.onTouchEvent(ev);}}

package com.gesturetest;import android.content.Context;import android.content.Intent;import android.util.Log;import android.view.GestureDetector.OnGestureListener;import android.view.MotionEvent;public class Gesture implements OnGestureListener{/** * 得到全域的標誌位 */int flag=BaseActivity.flag;@SuppressWarnings("rawtypes")/** * 得到activity數組 */Class[] myClass=BaseActivity.myClass;Context context;public Gesture(Context context) {// TODO Auto-generated constructor stubthis.context=context;}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubreturn false;}@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}@Override/** * 滑動事件的處理 */public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {  Log.i("Fling", "baseactivity");    //左滑動        if (e1.getX() - e2.getX() > 80) {                          flag=(--flag+myClass.length)%myClass.length;          //改變BaseActivity,讓其知道標誌位改變了            BaseActivity.flag=flag;            Intent intent=new Intent(context, myClass[flag]);           //需要context才能啟動activity            context.startActivity(intent);                        return true;          }         //右滑動        else if (e1.getX() - e2.getX() <-80) {          Log.i("Fling", "baseactivity");                   flag=++flag%myClass.length;            //改變BaseActivity,讓其知道標誌位改變了            BaseActivity.flag=flag;            Intent intent=new Intent(context,myClass[flag]);            //需要context才能啟動activity            context.startActivity(intent);                       return true;          }          return true;  }}

推薦android等智能機開發QQ交流群:187651345

可以看看代碼,上面有代碼的。其實也很簡單的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.