Add view on top of MediaController

北城以北 提交于 2019-12-12 12:24:58

问题


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:

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


回答1:


Try overriding MediaController.dispatchTouchEvent()

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

The code will go something like this:

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 Theres's a very similar question on this, it may have a better solution: Android MediaController intercepts all other touch events




回答2:


When a view is overlapping another view then the hidden view will not get any touch event as the view on top consumes the touch event. If you want to the hidden view to receive the touch event then you have to manually pass the touch event from the top view to the hidden view. Here there are two possibilities:

  • You want the touch event to be shared by both the view: In this case after passing the touch event to the hidden view indicate android that touch event has not been consumed by returning false from the onTouch() method of the top view.
  • You want the touch event to be handled by only the hidden view: In this case after passing the touch event to the hidden view indicate android that the touch event been consumed by returning true from the onTouch() method of the top view.

Here is a sample code for this:

btn.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch (View v, MotionEvent event)
        {
            list.onTouchEvent(event);
            return true;       // return true if touch event is consumed else return false
        }
    });

XML for this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/shape" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000" >
    </ListView>

    <Button
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#88cc0000"
        android:layout_alignParentTop="true"
        android:onClick="showMe" />
</RelativeLayout>

Here Button is hiding the list view still the list will scroll as the touch event is passed to the below layout.
I hope this will help. :)



来源:https://stackoverflow.com/questions/12022117/add-view-on-top-of-mediacontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!