我正在尝试继承 MediaController 以使其完全可定制(我不采用vidtry方法,因为我有一些无法更改的遗留代码)。
我的代码大致如下:
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 被调用了在调用堆栈中看到:
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
问题是我需要更换装饰窗口触摸监听器还是有更好的方法而我错过了什么?
ps我正在使用lv。9 API。
谢谢!