-->

轻扫屏幕的某些部分(swipe some part of the screen)

2019-07-17 11:34发布

我试图设计一个屏幕,该屏幕包含一些擦拭部。 我有一个屏幕,MapView类,ListView和一些文字。 我的MapView的是滑动的主要部分,如果我在左侧滑动接触然后将示出和,如果我向右滑动,然后地址将被显示。
这里是我的形象。 我显示的圆形部分,我需要刷卡。
我如何能实现我的应用程序这个功能。 我走通过与一个教程 ,但它并没有帮助我。 本教程教如何刷卡全屏幕,但我需要滑动屏幕的某些部分。
可能吗。 给我一些参考和提示。 我不明白viewPager

Answer 1:

最后我得到了答案轻扫屏幕的某些部分。 在的onCreate(),你需要添加下面的行

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

我在类扩展PagerAdapter类。

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;
}

这里是我的xml文件

?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>


Answer 2:

创建这样的一个类:

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() {
}

}

然后添加OnSwipeTouchListener到您MapView



文章来源: swipe some part of the screen