1

我正在使用手势检测在 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;
}

非常感谢。

4

1 回答 1

3

第一个想法。

试着观察你的 x 和 y 值:

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

在 horisontaly 上剪切 100px 的手势:

if(x>100) return false;
else
    return viewPager.onTouchEvent(event);
于 2013-11-04T07:31:06.450 回答