【问题标题】:How to make floating action button consume click event?如何使浮动动作按钮消耗点击事件?
【发布时间】:2015-01-11 22:27:42
【问题描述】:

我目前正在我正在制作的应用程序中设置一个浮动操作按钮。 我的问题是,当我单击按钮时,按钮后面的列表视图也会收到单击事件。 我需要让按钮消耗整个点击事件。

我正在使用这个库来创建按钮:https://github.com/FaizMalkani/FloatingActionButton

如果有人能就如何实现这一目标向我指出正确的方向,我将不胜感激。 我在下面包含了我正在使用的 xml 布局。

谢谢 科里:)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:padding="4dip"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1">

        </android.support.v4.view.ViewPager>

    </LinearLayout>

    <com.bacon.corey.audiotimeshift.FloatingActionButton
        android:id="@+id/fabbutton"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        app:colour="@color/holo_red_light"
        app:drawable="@drawable/ic_content_new"

    />

</FrameLayout>

【问题讨论】:

    标签: java android material-design floating-action-button


    【解决方案1】:

    我不知道 FloatingActionButton 的实现细节,但是你可以设置一个 TouchListener 并消费从 onTouch 方法返回 true 的事件。像这样的:

    mFloatingActionButton.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP){
                // Do what you want
                return true;
            }
            return true; // consume the event
        }
    });
    

    【讨论】:

    • 嘿,太完美了,谢谢!它解决了我的问题:)
    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 2012-04-26
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    相关资源
    最近更新 更多