简体   繁体   中英

FloatingActionButton over Fragment on SDK 33

After read various stackoverflow's questions about floatingactionbutton, like here Android changing Floating Action Button color which is one of most complete questions about it, and tried various suggestions, I am using fab over a Fragment in an app with compileSdkVersion 33 , targetSdkVersion 33 , implementation 'androidx.appcompat:appcompat:1.5.1' , plus I settled last version of Material component implementation 'com.google.android.material:material:1.7.0' , and in fragment xml I settled as:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    app:srcCompat="@android:drawable/ic_popup_sync"
    app:tint="@color/white"
    app:backgroundTint="@color/fab_enabled"/>

and in code I tried various ways, as you can see on commented code lines, but seems fab doesn't change background color as disabled even the state corresponds as disabled:

FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        fab.setEnabled(false);
        //fab.getBackground().set(getContext().getColor(R.color.fab_disabled));
        //fab.setBackgroundTintList(ColorStateList.valueOf(getContext().getColor(R.color.fab_disabled)));
        //fab.setBackgroundColor( getContext().getColor(R.color.fab_disabled) );
        fab.getBackground().mutate().setTint(getContext().getColor(R.color.fab_disabled));

does maybe that depend from the material version? Or is maybe related to some refresh needed to show background color changed? Or even because I used a standard java button ( @android:drawable/ic_popup_sync ), not a custom one? .... I also tried using a selector instead of color in backgroundTint too to make it changes automatically depending from the current state, but it didn't worked...

Edit 2023-01-11:

I tried to incorporate the listview and the floating action button inside a RelativeLayout to try to see listview cleared as like as I call adapter.clear() and adapter.notifyDataSetChanged():

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/fr_myfragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyFragment">

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

                <!-- TODO: Update blank fragment layout -->
                <ListView
                    android:id="@+id/myfragment_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_margin="5dp" />

                <com.google.android.material.floatingactionbutton.FloatingActionButton
                    android:id="@+id/fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentBottom="true"
                    android:layout_gravity="bottom|end"
                    android:layout_margin="16dp"
                    android:backgroundTint="@color/fab_colors"
                    android:src="@android:drawable/ic_popup_sync" />

        </RelativeLayout>

</FrameLayout>

but once I click fab button the listview won't clear before to do new search of items to add in listview, neither hide the fab button itself... I tried also to change setOnClickListener with setOnTouchListener for fab button to see if fab's behavior could change but doesn't work... And I'm really stuck on it..... any direction would be appreciate.... Thanks!

Try this code..

fab.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.fab_disabled))

Here this is context

At the end after some tests, I figured it out putting the code in a asynchronous runnable thread passing fab button so, once it finished, I can re-enable the button. Thanks to everyone anyway for suggests!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM