2012-07-16 95 views
0

我想要继承MediaController以使其完全可定制(我没有使用vidtry方法,因为我有一些遗留代码,我无法更改)。无法拦截自定义MediaController子类中的触摸事件

我的代码看起来大致是这样的:

mc_layout.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

    <LinearLayout 
     android:id="@+id/top" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     ...> 

     // Some buttons 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottom" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     ... > 

     // Some buttons 

    </LinearLayout> 

mc_subclass.java:

public class MCSubclass extends MediaController { 

private View mRoot; 

GestureDetector mGestureDetector = new GestureDetector(getContext(), new SimpleOnGestureListener() { 

    @Override 
    public boolean onDown(MotionEvent e) { 
    // Do something. 
     return true; 
    } 

    @Override 
    public boolean onSingleTapConfirmed(MotionEvent e) { 
     // Do something. 
     return true; 
    } 

    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
     // Do something. 
     return false; 
    } 
}); 

public MCSubclass(final Context context) { 
    super(context); 
} 

public boolean onTouchEvent(final MotionEvent event) { 
// NEVER CALLED! 
    return mGestureDetector.onTouchEvent(event); 
} 

public final void setAnchorView(final View view) { 
    super.setAnchorView(view); 
    FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.MATCH_PARENT 
    ); 

    removeAllViews(); 
    View v = makeControllerView(); 
    addView(v, frameParams); 
} 

private View makeControllerView() { 
    LayoutInflater inflate = (LayoutInflater) getContext().getSystemService(
     Context.LAYOUT_INFLATER_SERVICE); 

    mRoot = inflate.inflate(R.layout.mc_layout, null); 
    initControllerView(mRoot); 
    return mRoot; 
} 

private void initControllerView(View v) { 
    mPlayButton = (ImageButton) v.findViewById(R.id.playBtn); 
    if (mPlayButton != null) { 
     mPlayButton.requestFocus(); 
     mPlayButton.setOnClickListener(mOnClickListener); 
    } 
// Init other views... 
} 

}

现在,我得到了所有控件都可见,并且它们都响应了它们的点击/触摸操作,但是当我点击/触摸MC窗口而不是调用重写的onTouchEvent时,我期望MediaController的装饰窗口的onTouch被调用,因为它可以在callstack中看到:

MediaController$1.onTouch(View, MotionEvent) line: 149 
PhoneWindow$DecorView(View).dispatchTouchEvent(MotionEvent) line: 3762 
PhoneWindow$DecorView(ViewGroup).dispatchTouchEvent(MotionEvent) line: 897 
PhoneWindow$DecorView.dispatchTouchEvent(MotionEvent) line: 1862  
ViewRoot.handleMessage(Message) line: 1809 
ViewRoot(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 123 

问题是我需要更换装饰窗口触摸监听器还是有更好的方法,我错过了什么?

p.s.我正在与lv合作。 9 API。

谢谢!

回答

1

我找到了! 问题在于使用FrameLayout,它只能显示一个项目,其大小是最大的子项的大小。所以在我的情况下,它不足以覆盖整个屏幕,因为我预期。结果,装饰窗口最终处理了触摸事件,这非常合理。

反正工作布局现在看起来像:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:id="@+id/top" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ...> 

     // Some buttons 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottom" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ... > 

     // Some buttons 

    </LinearLayout> 
    .... 
</RelativeLayout> 

希望这将有助于有一天有人:)