Aditya Parab
Aditya Parab

Reputation: 13

Multiple Buttons in FloatingActionButton library

Hi I an trying to implement [FloatingActionButton][1] Library by makovkastar. (As it is backward compatible. It works fine with single FAB, but when 2 Buttons are added, the "List" seems to get attached to only one buttons.

fragment_listview.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:fab="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@android:color/transparent"
        android:headerDividersEnabled="false"/>

    <com.melnykov.fab.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@drawable/ic_add_white_24dp"
        fab:fab_colorNormal="@color/accent"
        fab:fab_colorPressed="@color/accent_pressed"
        fab:fab_colorRipple="@color/ripple"/>

    <com.melnykov.fab.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|left"
        android:layout_margin="16dp"
        android:src="@drawable/ic_add_white_24dp"
        fab:fab_colorNormal="@color/accent"
        fab:fab_colorPressed="@color/accent_pressed"
        fab:fab_colorRipple="@color/ripple"/>
</FrameLayout>

MainActivity ListViewFragment Class

public static class ListViewFragment extends Fragment {

        @SuppressLint("InflateParams")
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View root = inflater.inflate(R.layout.fragment_listview, container, false);

            ListView list = (ListView) root.findViewById(android.R.id.list);
            ListViewAdapter listAdapter = new ListViewAdapter(getActivity(),
                    getResources().getStringArray(R.array.countries));
            list.setAdapter(listAdapter);

            FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.fab);
            fab.attachToListView(list);

            FloatingActionButton fab2 = (FloatingActionButton) root.findViewById(R.id.fab2);
            fab2.attachToListView(list);



            return root;
        }

This is same code sample provided in that library:

When the list is scrolled, only one button (fab2 in this case) becomes invisible when scrolled down. I want both the buttons to react to scroll.

Any idea to anyone how this can be done?

Thanks in advance.

Upvotes: 1

Views: 3561

Answers (1)

smac89
smac89

Reputation: 43078

You can do this yourself, by making your fragment implement com.melnykov.fab.ScrollDirectionListener (also from the same package as the fab).

public static class ListViewFragment extends Fragment implements
    com.melnykov.fab.ScrollDirectionListener {

    private FloatingActionButton fab, fab2;

    @SuppressLint("InflateParams")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_listview, container, false);

        ListView list = (ListView) root.findViewById(android.R.id.list);
        ListViewAdapter listAdapter = new ListViewAdapter(getActivity(),
                    getResources().getStringArray(R.array.countries));
        list.setAdapter(listAdapter);

        fab = (FloatingActionButton) root.findViewById(R.id.fab);
            // this method expects a ScrollDirectionListener as second arg
            // so use this for that parameter
            fab.attachToListView(list, this);

        fab2 = (FloatingActionButton) root.findViewById(R.id.fab2);
            fab2.attachToListView(list, this);

            return root;
        }

    @Override
    public void onScrollDown() {
        if (fab.isVisible() || fab2.isVisible()) {
            fab.hide();
            fab2.hide();
        }
    }

    @Override
    public void onScrollUp() {
        if (!fab.isVisible() || !fab2.isVisible()) {
            fab.show();
            fab2.show();
        }
    }
}

I'm sure this should work, but in the case it doesn't you should open an issue to propose this possible bug to the developer.

Note

Android now has it's own implementation of the FloatingActionButton (and much more) in the design library. Include it in your project with this:

compile 'com.android.support:design:22.2.0'

Here is an excellent guide for using the new FAB with lists; it even includes use of the new CoordinatorLayout which should replace need for scroll listeners and such

Upvotes: 2

Related Questions