swipe some part of the screen

前端 未结 2 1885
予麋鹿
予麋鹿 2020-12-28 21:50

I am trying to design one screen which contain some swipe part. I have one screen with mapview, listview and some text. My Mapview is main part of swipe, if i swipe at left

相关标签:
2条回答
  • 2020-12-28 22:04

    Finally i got answer to swipe some part of the screen. in onCreate() you need to add following line

    MyPagerAdapter adapter = new MyPagerAdapter();
         myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
         myPager.setAdapter(adapter);
         myPager.setCurrentItem(3);
    

    I extend PagerAdapter class in my class.

    private class MyPagerAdapter extends PagerAdapter {
    
        public int getCount() {
            return 3;
        }
    
        public Object instantiateItem(View collection, int position) {
    
            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            int resId = 0;
            switch (position) {
            case 0: 
                resId = showHotelContact();             
                break;
            case 1:
                resId = showHotelAddress();         
                break;              
            case 2:     
                resId = showHotelMap();             
                break;      
            }
    
            View view = inflater.inflate(resId, null);
            ((ViewPager) collection).addView(view, 0);
            return view;
        }
     }   
    public int showHotelMap()
    {
        int resId;
        resId = R.layout.hotelmap;
        return resId;
    }
    public int showHotelAddress()
    {
        int resId;
        resId = R.layout.hoteladdress;
        return resId;
    }
    public int showHotelContact()
    {
        int resId;
        resId = R.layout.hotelcontact;
        return resId;
    }
    

    Here is my xml file

    ?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <android.support.v4.view.ViewPager
    android:id="@+id/myfivepanelpager"
    android:layout_width="wrap_content"
    android:layout_height="186dp"
    android:fadingEdge="vertical" />
    
    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hotelName" />
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-28 22:25

    Create a class like this :

    public class OnSwipeTouchListener implements OnTouchListener {
    
    
    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
    
    public boolean onTouch(final View v, final MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
    
    private final class GestureListener extends SimpleOnGestureListener {
    
        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;
    
    
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            onTouch(e);
            return true;
        }
    
    
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                } else {
                   // onTouch(e);
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }
    public void onTouch(MotionEvent e) {
    }
    public void onDown(MotionEvent e) {
    }
    public void onSwipeRight() {
    }
    
    public void onSwipeLeft() {
    }
    
    public void onSwipeTop() {
    }
    
    public void onSwipeBottom() {
    }
    
    }
    

    and then add OnSwipeTouchListener to your MapView

    0 讨论(0)
提交回复
热议问题