android Swipe event in listview inside tab widget

江枫思渺然 提交于 2019-12-03 17:16:11
Arpit Garg
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;
        }

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!