FloatingActionButton 库中的多个按钮

Multiple Buttons in FloatingActionButton library

您好,我正在尝试实现 makovkastar 的 [FloatingActionButton][1] 库。 (因为它是向后兼容的。它适用于单个 FAB,但是当添加 2 个按钮时,"List" 似乎只连接到一个按钮。

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

这与 that library 中提供的代码示例相同:

滚动列表时,只有一个按钮(fab2在这种情况下)在向下滚动时不可见。我希望两个按钮都对滚动做出反应。

有人知道如何做到这一点吗?

提前致谢。

您可以自己完成此操作,方法是让您的片段实现 com.melnykov.fab.ScrollDirectionListener(也来自与 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();
        }
    }
}

我确定这应该有效,但在这种情况下,您应该 open an issue 向开发人员提出这个可能的错误。

备注

Android 现在有自己的 FloatingActionButton (and much more) in the design library 实现。将它包含在您的项目中:

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

这是一个 excellent guide for using the new FAB with lists; it even includes use of the new CoordinatorLayout,它应该取代对滚动侦听器等的需求