FloatingActionButton OnClickListener not working o

2019-07-07 05:38发布

问题:

my FAB onClick is not working on API 17, but on API 21 it's working fine.

I'm using:

compile 'com.android.support:design:23.+'

FAB layout:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/myFAB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="bottom"
    android:layout_marginBottom="26dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:backgroundTint="#FFC107"
    android:clickable="true"
    android:elevation="@dimen/design_fab_elevation"
    android:src="@drawable/ic_add_24dp"
    app:borderWidth="0dp" />

And on Activity:

myFAB = (FloatingActionButton) findViewById(R.id.myFAB);
callCadastroEncoemnda();

protected void callCadastroEncoemnda() {
    myFAB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, newPackage.class);
            MainActivity.this.startActivity(intent);
        }
    });
}

回答1:

SOLVED!

On my layout, the FloatingActionButton was bellow of a RecyclerView, and when I click on the Fab, I was clicking on the RecyclerView (that was empty)

Before:

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/primary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/myFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_gravity="bottom"
        android:layout_marginBottom="26dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:backgroundTint="#FFC107"
        android:clickable="true"
        android:elevation="@dimen/design_fab_elevation"
        android:src="@drawable/ic_add_24dp"
        app:borderWidth="0dp"
        android:layout_alignParentTop="false"
        android:layout_alignParentRight="false"
        android:layout_alignWithParentIfMissing="false"
        android:layout_alignParentLeft="false" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/my_awesome_toolbar">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/my_awesome_toolbar"
            android:layout_marginTop="?attr/actionBarSize" />
    </android.support.v4.widget.SwipeRefreshLayout>

After:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/primary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/my_awesome_toolbar">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/my_awesome_toolbar"
                android:layout_marginTop="?attr/actionBarSize" />
        </android.support.v4.widget.SwipeRefreshLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/myFAB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_gravity="bottom"
            android:layout_marginBottom="26dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:backgroundTint="#FFC107"
            android:clickable="true"
            android:elevation="@dimen/design_fab_elevation"
            android:src="@drawable/ic_add_24dp"
            app:borderWidth="0dp"
            android:layout_alignParentTop="false"
            android:layout_alignParentRight="false"
            android:layout_alignWithParentIfMissing="false"
            android:layout_alignParentLeft="false" />
</RelativeLayout>