FloatingActionButton OnClickListener无法在API 17上运行

时间:2015-11-12 14:16:01

标签: java android floating-action-button

我的FAB onClick不支持API 17,但是在API 21上工作正常。

我正在使用:

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

FAB布局:

<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" />

活动:

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 个答案:

答案 0 :(得分:10)

解决!

在我的布局上,FloatingActionButton低于RecyclerView,当我点击Fab时,我点击了RecyclerView(那是空的)

在:     

    <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>

后:

    <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>