简体   繁体   English

在ActionBarActivity中触摸事件

[英]Touch Events Inside ActionBarActivity

I have three tabs inside a support7 ActionBarActivity which contains a support4 FragmentTabHost. 我在support7 ActionBarActivity内有三个选项卡,其中包含support4 FragmentTabHost。 It is largely working and tapping on the tab titles switches to the correct Fragment. 它在很大程度上起作用,点击选项卡标题将切换到正确的片段。

I'm trying to add a GestureDetector so that swiping left/right will change tabs. 我正在尝试添加GestureDetector,以便向左/向右滑动将更改选项卡。 (I know there are more modern containers for this but lets stick with FragmentTabHost for now.) (我知道有更多现代容器可以使用,但现在让我们继续使用FragmentTabHost。)

class MainGestureDetector extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            final int current = tabHost.getCurrentTab();
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                if (current < tabHost.getTabWidget().getTabCount() - 1) {
                    tabHost.setCurrentTab(current + 1);
                }
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                if (current > 0) {
                    tabHost.setCurrentTab(current - 1);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
}

and instantiated as such: 并实例化为:

    gestureDetector = new GestureDetector(this, new MainGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };

    tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    tabHost.setOnClickListener(this);
    tabHost.setOnTouchListener(gestureListener);

It works! 有用! But only on the very first tab. 但仅在第一个选项卡上。 Once swiping (or tapping) switches to a tab other than the first, swiping doesn't work. 滑动(或轻击)切换到除第一个选项卡以外的其他选项卡时,滑动将不起作用。 If I tap on the first tab again to go back to the beginning, the gesture recognizer will again work... once. 如果我再次点击第一个选项卡以返回到开头,则手势识别器将再次运行...一次。

What do I have to do to make the GestureListener work on all tabs? 要使GestureListener在所有选项卡上都能工作,我该怎么办?

I guess I should have realized. 我想我应该意识到的。 The "touch" events were getting directed to child views within the tab rather than propagate up to the parent and the GestureListener I had defined there. “触摸”事件被定向到选项卡中的子视图,而不是传播到父级和我在此处定义的GestureListener。 My first tab didn't (yet) have a child view spanning the screen so that is why it worked there. 我的第一个标签还没有(但是)在整个屏幕上都有子视图,所以这就是它在这里起作用的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Horizo​​ntalScrollView内部悬挂触摸事件 - Hangling touch events inside HorizontalScrollView ViewPager中的TextView拦截触摸事件 - TextView inside ViewPager intercepts touch events 是否可以在RecyclerView内外合并触摸事件? - Is it possible to merge touch events outside and inside the RecyclerView? 在NestedScrollV中禁用RecyclerView的触摸事件 - disable touch events for RecyclerView inside NestedScrollV 在Android中的ViewPager中禁用ScrollView的触摸事件 - Disable touch events for ScrollView inside ViewPager in Android 尝试在actionBarActivity内部启动片段 - Trying to launch fragment inside actionBarActivity 是否可以在ViewGroup中但不在其范围内为View启用触摸事件? - Is it possible to enable touch events for a View inside a ViewGroup but outside its bounds? 一键点击TextInputLayout中的EditText会生成两个或更多事件 - One touch on EditText inside TextInputLayout generate two or more events GoogleMap在ViewPager中的片段内,将所有触摸事件保留在GoogleMap中 - GoogleMap inside of a Fragment in a ViewPager, keep all touch events in the GoogleMap Android在ActionBarActivity中添加TabListener已停止 - Android add TabListener inside ActionBarActivity has stopped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM