本文介绍了在MediaController顶部添加视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MediaController始终显示在屏幕上所有视图的顶部,并且在隐藏之前不要将点击传递给下面的视图,我的问题是:

MediaController always shown on top of all views in the screen and do not pass clicks to below views until it is hidden, my issue is:

如何在MediaController顶部添加按钮视图,以便其处理单击事件?或者如何将点击事件传递到下面的视图?

How to add button view on top of MediaController so it handle click events ?ORHow to pass click events to below view?

推荐答案

尝试覆盖MediaController. dispatchTouchEvent()

Try overriding MediaController.dispatchTouchEvent()

它更适合您的任务,请在我的回答此处详细了解原因.

It suits your task better, see my answer here for a detailed explanation why.

代码将如下所示:

public class MyMediaController extends MediaController {
    ...
    private WeakReference<Button> mButton;

    // TODO You'll have to call this whenever the layout might have been rebuilt
    // i.e. in onCreate() of your activity (or in onConfigurationChanged() if you
    // handle configuration changes yourself)
    public void setButton(Button button) {
        mButton = new WeakReference<Button>(button);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int iX = (int) event.getX();
        int iY = (int) event.getY();

        Button button = mButton.get();
        if (button != null) {
            Rect buttonHitRect = new Rect();
            button.getHitRect(buttonHitRect);
            if (buttonHitRect != null && buttonHitRect.contains(iX, iY)) {
                // user hit the button, dispatch event to the button
                button.dispatchTouchEvent(event);
                return true;
            }
        }
        // button did not get hit, pass the touch through
        return super.dispatchTouchEvent(event)
    }
}

尝试一下,让我知道如何进行.

Try this, let me know how it goes.

UPD 有一个非常类似的问题,它可能有一个更好的解决方案:

UPD Theres's a very similar question on this, it may have a better solution: Android MediaController intercepts all other touch events

这篇关于在MediaController顶部添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 20:02