Floating Action Button does not responds when it c

2019-01-23 17:21发布

I am using a floating action button(FAB) in my application to show Dialogs, everything is worked just fine when I tested my app in Xperia Z with Lopllipop 5.1.1.
However, the problem is when tested my app in ASUS Zenfone 6 with KitKat 4.4.2 and in Xperia C with Jelly Bean 4.2.2, the FAB loaded perfectly but the FAB does not show the Dialogs, seems like it does not responds when I touched it.
For the record, my min sdk version is 16.

What i want to ask is why this is happening? Is there anything wrong with my code, or maybe with the android version or API level?

Please take a look at my code. Here the XML code:

    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_add_account"
    android:visibility="invisible"
    android:clickable="true"/>

and here how I declare and set the listener for the FAB:

FloatingActionButton fab;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

   fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);

   ...
   fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
   });
   ...
}

The thing that made me confused is how it is worked in Xperia Z but it does not worked in ASUS Zenfone 6?

2条回答
混吃等死
2楼-- · 2019-01-23 17:45

The problem in here is that I declared the FAB before the declaration of listView. Somehow by relocating the declaration of FAB after the the declaration of listView it will solved the problem.
From:

<android.support.design.widget.FloatingActionButton
    ...
    ...
    .../>
<ListView
    ...
    ...
    .../>

To:

<ListView
    ...
    ...
    .../>
<android.support.design.widget.FloatingActionButton
    ...
    ...
    .../>

Just as simple as that.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-23 17:55

Programmatically bringing the FAB to front should also work:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.bringToFront();
fab.setOnClickListener... //etc
查看更多
登录 后发表回答