Android系统。如何从TabHost中的视图获取布局值?

时间:2011-06-18 19:02:28

标签: android layout android-tabhost

我有一个TabHost托管3个活动。除了标签之外,我还希望支持滑动手势来更改当前标签。我的问题是其中一个视图包含水平滚动视图,我无法弄清楚如何防止水平滚动视图中的触摸更改当前选项卡。

plateView是需要处理的水平滚动视图。使用plateView.getBottom()查找底部并且不计算上面的触摸,但没有TabHost,但现在它返回null并崩溃,无论它在何处被调用。

onTouchEvent计算各处的触摸,dispatchTouchEvent不计算任何小部件的触摸。似乎它们的某些组合会很棒,但它们一起产生的功能与onTouchEvent相同。任何地方的滑动都可以改变活动。我对这些的理解有点模糊。

为什么getBottom()返回null?我怎样才能让它发挥作用?

gestureDetector = new GestureDetector(new CalcGestureDetector());
gestureListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d(TAG,"onTOUCH");
        if(gestureDetector.onTouchEvent(event)){
        return true;
        }
        return false;
    }
};

//Takes the touch and interprets it. Handles it. Changes tabs on fling.
class CalcGestureDetector extends SimpleOnGestureListener{
    @Override
    public boolean onFling(MotionEvent eOne, MotionEvent eTwo, float velocityX, float velocityY){
        Log.d(TAG,"WTF "+plateView.getBottom());
        if(false){
        }
        else{
            try{
                if (Math.abs(eOne.getY() - eTwo.getY()) > flingMaxOffPath)
                    return false;   //Too much of an arc in the fling.
                // right to left swipe
                if(eOne.getX() - eTwo.getX() > flingMinDistance && Math.abs(velocityX) > flingMinVelocity) {
                    tabHost.setAnimation(slideLeftIn);
                    tabHost.setAnimation(slideLeftOut);
                    tabHost.setCurrentTab((tabHost.getCurrentTab()+1)%3);
                }  else if (eTwo.getX() - eOne.getX() > flingMinDistance && Math.abs(velocityX) > flingMinVelocity) {
                    tabHost.setAnimation(slideRightIn);
                    tabHost.setAnimation(slideRightOut);
                    tabHost.setCurrentTab((tabHost.getCurrentTab()+2)%3);
                }
            }
            catch(Exception e){
            }
        }
        return false;
    }
}

//This method alone keeps a touch in the weights from changing tabs, but won't register
//touches on ANY widget on any screen (like text views).
public boolean dispatchTouchEvent(MotionEvent event){
    super.dispatchTouchEvent(event);
    return gestureDetector.onTouchEvent(event); 
}

//This method alone counts touches for swipes anywhere and everywhere.
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event))
        return true;
    else
        return false;
} 

0 个答案:

没有答案