android 选项卡小部件内列表视图中的滑动事件

发布于 2024-11-03 03:29:37 字数 7690 浏览 11 评论 0原文

我创建了一个显示列表视图的活动,并且在滑动操作时使用 ViewFlipper 显示另一个列表。代码附在下面:

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    private Animation slideLeftIn;
    private Animation slideLeftOut;
    private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;
    private ListView lv;
    private String[] city = { "Indore", "Bhopal", "Khargone", "Ujjain",
        "Nasik", "Pune", "Delhi", "Mumbai", "Noida", "Hyderabad",
        "Banglore", "Ajmer", "Goa", "Jaipur", "Nagpur", "" };
    private String[] country = { "India", "Bhutan", "Kuwait", "USA", };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        lv = (ListView) findViewById(R.id.List01);
        ListView lv2 = (ListView) findViewById(R.id.List02);

        viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils
                .loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils
                .loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this,
                R.anim.slide_right_out);

        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, city));
        lv2.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, country));
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                    long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item Clicked",
                        Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });
        lv2.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                    long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item List2 Clicked",
                        Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);
                    viewFlipper.showNext();
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                    viewFlipper.showPrevious();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
            return true;
        else
            return false;
    }
}

它正在工作 f9...

但是当我使用以下代码将其放置在另一个选项卡活动中当将其作为内容(意图)放置在另一个选项卡活动(AppStart)的选项卡中时,视图翻转器不起作用

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TabHost;

public class AppStart extends TabActivity {
    /** Called when the activity is first created. */
    static Drawable myImage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources res = getResources();
        myImage = res.getDrawable(R.drawable.club);

        TabHost tabs = getTabHost();
        tabs.setup();
        TabHost.TabSpec spec = tabs.newTabSpec(null);

                Intent i = new Intent().setClass(this, MainActivity.class);
                spec = tabs.newTabSpec(null);
        spec.setContent(i);
        spec.setIndicator("Tab");
        tabs.addTab(spec);

    }

}

布局如下: main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flipper" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:id="@+id/List01"/>
    </LinearLayout>

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView  android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:id="@+id/List02" />
    </LinearLayout>

</ViewFlipper>

和 main.xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent">


        </FrameLayout>
    </LinearLayout>
</TabHost>

请提出为什么当相同的活动放置在选项卡中时滑动手势不起作用。

谢谢

I have created an Activity that shows a listview and on swipe action another list is shown using ViewFlipper. The code is attached below:

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    private Animation slideLeftIn;
    private Animation slideLeftOut;
    private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;
    private ListView lv;
    private String[] city = { "Indore", "Bhopal", "Khargone", "Ujjain",
        "Nasik", "Pune", "Delhi", "Mumbai", "Noida", "Hyderabad",
        "Banglore", "Ajmer", "Goa", "Jaipur", "Nagpur", "" };
    private String[] country = { "India", "Bhutan", "Kuwait", "USA", };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        lv = (ListView) findViewById(R.id.List01);
        ListView lv2 = (ListView) findViewById(R.id.List02);

        viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils
                .loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils
                .loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this,
                R.anim.slide_right_out);

        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, city));
        lv2.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, country));
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                    long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item Clicked",
                        Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });
        lv2.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                    long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item List2 Clicked",
                        Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);
                    viewFlipper.showNext();
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                    viewFlipper.showPrevious();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
            return true;
        else
            return false;
    }
}

It's working f9...

But when I place this within the another tab activity with the following code The View Flipper is not working when placed as content(intent) in tab of another tabactivity (AppStart)

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TabHost;

public class AppStart extends TabActivity {
    /** Called when the activity is first created. */
    static Drawable myImage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources res = getResources();
        myImage = res.getDrawable(R.drawable.club);

        TabHost tabs = getTabHost();
        tabs.setup();
        TabHost.TabSpec spec = tabs.newTabSpec(null);

                Intent i = new Intent().setClass(this, MainActivity.class);
                spec = tabs.newTabSpec(null);
        spec.setContent(i);
        spec.setIndicator("Tab");
        tabs.addTab(spec);

    }

}

The Layout is as follows:
main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flipper" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:id="@+id/List01"/>
    </LinearLayout>

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView  android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:id="@+id/List02" />
    </LinearLayout>

</ViewFlipper>

And main.xml as shown below:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent">


        </FrameLayout>
    </LinearLayout>
</TabHost>

Please suggest why the swipe gesture is not working when the same activity is placed in tab.

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂ガ小子 2024-11-10 03:29:37
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;

    private Animation slideLeftIn;
    private Animation slideLeftOut;

    private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;
    private ListView lv;

    private String[] city = {
        "Indore", "Bhopal", "Khargone", "Ujjain",
            "Nasik", "Pune", "Delhi", "Mumbai", "Noida", "Hyderabad",
            "Banglore", "Ajmer", "Goa", "Jaipur", "Nagpur", ""
    };

    private String[] country = {
        "India", "Bhutan", "Kuwait", "USA",
    };


    @
    Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        lv = (ListView) findViewById(R.id.List01);
        ListView lv2 = (ListView) findViewById(R.id.List02);

        viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils
            .loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils
            .loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this,
            R.anim.slide_right_out);

        ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(
            this);

        //View on which gesture should function This is image view at bottom of listView on which flip gesture is performed.

        flipBarBottom.setOnTouchListener(activitySwipeDetector);

        lv.setAdapter(new ArrayAdapter < String > (this,
            android.R.layout.simple_list_item_1, city));
        lv2.setAdapter(new ArrayAdapter < String > (this,
            android.R.layout.simple_list_item_1, country));
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item Clicked",
                    Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });

        lv2.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item List2 Clicked",
                    Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });
    }

    public class ActivitySwipeDetector implements View.OnTouchListener {

        static final String logTag = "ActivitySwipeDetector";
        private Activity activity;
        static final int MIN_DISTANCE = 100;
        private float downX, downY, upX, upY;

        public ActivitySwipeDetector(Activity activity) {
            this.activity = activity;
        }

        public void onRightToLeftSwipe() {
            Log.i(logTag, "RightToLeftSwipe!");
            // activity.doSomething();
            viewFlipper.setInAnimation(slideLeftIn);
            viewFlipper.setOutAnimation(slideLeftOut);
            viewFlipper.showNext();
            flipBarBottom.setBackgroundResource(R.drawable.flipstatus01);
        }

        public void onLeftToRightSwipe() {
            Log.i(logTag, "LeftToRightSwipe!");
            viewFlipper.setInAnimation(slideRightIn);
            viewFlipper.setOutAnimation(slideRightOut);
            viewFlipper.showPrevious();
            flipBarBottom.setBackgroundResource(R.drawable.flipstatus02);
            // activity.doSomething();
        }

        public void onTopToBottomSwipe() {
            Log.i(logTag, "onTopToBottomSwipe!");
            // activity.doSomething();
        }

        public void onBottomToTopSwipe() {
            Log.i(logTag, "onBottomToTopSwipe!");
            // activity.doSomething();
        }

        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                {
                    downX = event.getX();
                    downY = event.getY();
                    return true;
                }
            case MotionEvent.ACTION_UP:
                {
                    upX = event.getX();
                    upY = event.getY();

                    float deltaX = downX - upX;
                    float deltaY = downY - upY;

                    // swipe horizontal?
                    if (Math.abs(deltaX) > MIN_DISTANCE) {
                        // left or right
                        if (deltaX < 0) {
                            this.onLeftToRightSwipe();
                            return true;
                        }
                        if (deltaX > 0) {
                            this.onRightToLeftSwipe();
                            return true;
                        }
                    } else {
                        Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                    }
                    // swipe vertical?
                    if (Math.abs(deltaY) > MIN_DISTANCE) {
                        // top or down
                        if (deltaY < 0) {
                            this.onTopToBottomSwipe();
                            return true;
                        }
                        if (deltaY > 0) {
                            this.onBottomToTopSwipe();
                            return true;
                        }
                    } else {
                        Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                    }

                    return true;
                }
            }
            return false;
        }

    }
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;

    private Animation slideLeftIn;
    private Animation slideLeftOut;

    private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;
    private ListView lv;

    private String[] city = {
        "Indore", "Bhopal", "Khargone", "Ujjain",
            "Nasik", "Pune", "Delhi", "Mumbai", "Noida", "Hyderabad",
            "Banglore", "Ajmer", "Goa", "Jaipur", "Nagpur", ""
    };

    private String[] country = {
        "India", "Bhutan", "Kuwait", "USA",
    };


    @
    Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        lv = (ListView) findViewById(R.id.List01);
        ListView lv2 = (ListView) findViewById(R.id.List02);

        viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils
            .loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils
            .loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this,
            R.anim.slide_right_out);

        ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(
            this);

        //View on which gesture should function This is image view at bottom of listView on which flip gesture is performed.

        flipBarBottom.setOnTouchListener(activitySwipeDetector);

        lv.setAdapter(new ArrayAdapter < String > (this,
            android.R.layout.simple_list_item_1, city));
        lv2.setAdapter(new ArrayAdapter < String > (this,
            android.R.layout.simple_list_item_1, country));
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item Clicked",
                    Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });

        lv2.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item List2 Clicked",
                    Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });
    }

    public class ActivitySwipeDetector implements View.OnTouchListener {

        static final String logTag = "ActivitySwipeDetector";
        private Activity activity;
        static final int MIN_DISTANCE = 100;
        private float downX, downY, upX, upY;

        public ActivitySwipeDetector(Activity activity) {
            this.activity = activity;
        }

        public void onRightToLeftSwipe() {
            Log.i(logTag, "RightToLeftSwipe!");
            // activity.doSomething();
            viewFlipper.setInAnimation(slideLeftIn);
            viewFlipper.setOutAnimation(slideLeftOut);
            viewFlipper.showNext();
            flipBarBottom.setBackgroundResource(R.drawable.flipstatus01);
        }

        public void onLeftToRightSwipe() {
            Log.i(logTag, "LeftToRightSwipe!");
            viewFlipper.setInAnimation(slideRightIn);
            viewFlipper.setOutAnimation(slideRightOut);
            viewFlipper.showPrevious();
            flipBarBottom.setBackgroundResource(R.drawable.flipstatus02);
            // activity.doSomething();
        }

        public void onTopToBottomSwipe() {
            Log.i(logTag, "onTopToBottomSwipe!");
            // activity.doSomething();
        }

        public void onBottomToTopSwipe() {
            Log.i(logTag, "onBottomToTopSwipe!");
            // activity.doSomething();
        }

        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                {
                    downX = event.getX();
                    downY = event.getY();
                    return true;
                }
            case MotionEvent.ACTION_UP:
                {
                    upX = event.getX();
                    upY = event.getY();

                    float deltaX = downX - upX;
                    float deltaY = downY - upY;

                    // swipe horizontal?
                    if (Math.abs(deltaX) > MIN_DISTANCE) {
                        // left or right
                        if (deltaX < 0) {
                            this.onLeftToRightSwipe();
                            return true;
                        }
                        if (deltaX > 0) {
                            this.onRightToLeftSwipe();
                            return true;
                        }
                    } else {
                        Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                    }
                    // swipe vertical?
                    if (Math.abs(deltaY) > MIN_DISTANCE) {
                        // top or down
                        if (deltaY < 0) {
                            this.onTopToBottomSwipe();
                            return true;
                        }
                        if (deltaY > 0) {
                            this.onBottomToTopSwipe();
                            return true;
                        }
                    } else {
                        Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                    }

                    return true;
                }
            }
            return false;
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文