限制用于滑动手势检测的屏幕区域

时间:2013-11-04 07:17:58

标签: android gesture

我正在使用手势检测在包含覆盖在我的应用屏幕上的搜索栏等对象的HUD视图之间滑动。

是否有办法限制或设置屏幕的一部分以侦听检测手势。

我正在使用看起来像这样的自定义ViewPager。

主要课程。

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends Activity {
static GLSurfaceView mView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mView = (GLSurfaceView)findViewById(R.id.glview);


    mView.setZOrderOnTop(false);
    mView.setEGLContextClientVersion(2);
    mView.setRenderer(new GLLayer(this));
    mView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);
    mView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // use with  mView.requestRender() for each user change


    // Create and set adapter
    CustomPagerAdapter adapter = new CustomPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);

    // Add page change listener to pager component
    myPager.setOnPageChangeListener(new CustomPageChangeListener(this));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

customPageChangeListener

public class CustomPageChangeListener implements OnPageChangeListener, OnClickListener {

private Button buttonBlue   =   null;
private Button buttonYellow  =   null;
private Button buttonRed    =   null;
private ColorPicker colorPicker;

private Activity context    =   null;

/**
 * Constructor
 */
public CustomPageChangeListener(Activity context) {
    this.context = context;
}


@Override
public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void onPageSelected(int position) {

    switch (position) {
        case 0 : {
            buttonBlue = (Button) context.findViewById(R.id.buttonBlue);
            buttonBlue.setOnClickListener(this);
            colorPicker = (ColorPicker) context.findViewById(R.id.colorPicker1);
            // other controls
            break;
        }
        case 1 : {
            buttonYellow = (Button) context.findViewById(R.id.buttonYellow);
            buttonYellow.setOnClickListener(this);
                    //other controls
            break;
        }
        case 2 : {
            buttonRed = (Button) context.findViewById(R.id.buttonRed);
            buttonRed.setOnClickListener(this);
                    // other controls
            break;
        }
    }   
}

和适配器......

public class CustomPagerAdapter extends PagerAdapter {

public Object instantiateItem(final View collection, int position) {

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId = 0;
    switch (position) {
    case 0: {
        resId = R.layout.page_1;
        break;
    }
    case 1: {
        resId = R.layout.page_2;
        break;
    }
    case 2: {
        resId = R.layout.page_3;
        break;
    }
    }

    View view = inflater.inflate(resId, null);



    ((ViewPager) collection).addView(view, 0);

    return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);

}

@Override
public Parcelable saveState() {
    return null;
}

@Override
public int getCount() {
    return 3;
}

非常感谢。

1 个答案:

答案 0 :(得分:2)

第一个想法。

尝试观察你的x和y值:

x = event.getX();
y = event.getY();

在horistaly上以100px切割手势:

if(x>100) return false;
else
    return viewPager.onTouchEvent(event);