Android的,GridView和onTouchListenerAndroid、GridView、onTouchListener

2023-09-07 13:14:22 作者:深渊骑士

我的应用程序中有三个页面(三个选项卡),我想通过horizo​​ntaly移动手指beetween 2 GridView的切换。触摸code工作正常,但我不能再点击网格上的物品!我使用的方法onItemClickListener(onClickListener不工作在GridView的),但电网项目不点击。感谢您的帮助!

在code是:

  myGrid.setOnTouchListener(本);myGrid.setOnItemClickListener(本);....公共布尔onTouch(视图V,MotionEvent事件){    INT eventaction = event.getAction();    开关(eventaction){    案例MotionEvent.ACTION_DOWN:        XSTART = event.getX();        打破;    案例MotionEvent.ACTION_UP:        XEND = event.getX();        如果(XEND  -  XSTART→20){            //切换到previous标签        }        如果(XEND  -  XSTART< -20){            //切换到下一个标签        }        返回true;    默认:        打破;    }    返回true;} 

解决方案

什么观点是,onTouch code吗?你可以尝试改变,去年返回true 返回false 因此,如果动作不是motionevent,事件不受视图消耗

My application have three pages (three tabs) and I want to switch beetween two gridviews by moving finger horizontaly. The touch code works fine but I can't click anymore on the grid items! I use the method onItemClickListener (onClickListener don't works on Gridview) but the grid item is not clicked. Thanks for your help!

Android的gridview使用教程

The code is :

myGrid.setOnTouchListener(this);
myGrid.setOnItemClickListener(this);
....

public boolean onTouch(View v, MotionEvent event) {
    int eventaction = event.getAction();
    switch (eventaction) {
    case MotionEvent.ACTION_DOWN:
        xStart = event.getX();
        break;
    case MotionEvent.ACTION_UP:
        xEnd = event.getX();

        if (xEnd - xStart > 20){

            //switch to previous tab
        }
        if (xEnd - xStart < -20){
            //switch to next tab
        }
        return true;
    default:
        break;
    }
    return true;
}

解决方案

What view is that onTouch code in? You could try changing that last return true to return false so that if the action wasn't a motionevent, the event is not consumed by the view.