cymcsg / UltimateRecyclerView

A RecyclerView(advanced and flexible version of ListView in Android) with refreshing,loading more,animation and many other features.
Apache License 2.0
7.22k stars 1.43k forks source link

Can't set custom FloatingActionsMenu #194

Open kientux opened 9 years ago

kientux commented 9 years ago

I want to create my own FloatingActionsMenu, but there's no way to set it to recycler view. The only method is setDefaultFloatingActionButton, but it is for one button. This method is commented out in library code:

//    public void setCustomFloatingActionView(View customFloatingActionView) {
//        this.floatingActionMenu = floatingActionMenu;
//    }

Maybe you should add a public method to set mFloatingButtonView or mFloatingButtonViewStub?

kientux commented 9 years ago

Oh, very sorry, I just read README again and realized that I can set it using

app:recyclerviewFloatingActionView="@layout/custom_fab_view"

in layout file.

But I still hope there's a method for programmatically use :)

kientux commented 9 years ago

I reopen this because I can't find a "proper" way to setOnClickListener on each FloatingActionButton.

I'm currently using this:

FloatingActionsMenu floatMenu = (FloatingActionsMenu) ultimateRecyclerView.getCustomFloatingActionView();
for (int i = 0; i < floatMenu.getChildCount(); i++) {
    FloatingActionButton button = (FloatingActionButton) floatMenu.getChildAt(i);
    switch (button.getId()) {
        case R.id.buttonOne:
            button.setOnClickListener(new View.OnClickListener() {@Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this, "ONE PRESSED", Toast.LENGTH_SHORT).show();
                }
            });
            break;
        case R.id.buttonTwo:
            button.setOnClickListener(new View.OnClickListener() {@Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this, "TWO PRESSED", Toast.LENGTH_SHORT).show();
                }
            });
            break;
        case R.id.buttonThree:
            button.setOnClickListener(new View.OnClickListener() {@Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this, "THREE PRESSED", Toast.LENGTH_SHORT).show();
                }
            });
            break;
        default:
            break;
    }
}

which is a pretty bad way? Is there any easier way to do this? I tried extend FloatingActionsMenu, inflate custom view, set onclick in there, and use that custom class in layout. But it throws a bunch of runtime exception when runs.

jjhesk commented 9 years ago

I really want to know how to setup a single floating button on the URV. Would you like to post some more sample code if u got some time. @kientux @cymcsg

kientux commented 9 years ago

@jjhesk You should see in the example app. To add a button or a menu, you must create a layout custom_fab_view (or whatever name), then add this attribute:

app:recyclerviewFloatingActionView="@layout/custom_fab_view"

to recyclerview in xml, and call recyclerView.showFloatingButtonView().

<com.marshalchen.ultimaterecyclerview.UltimateRecyclerView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/recycler_view"
    app:recyclerviewClipToPadding="true"
    app:recyclerviewFloatingActionView="@layout/custom_fab_view"
    app:recyclerviewScrollbars="vertical"
    app:recyclerviewEmptyView="@layout/empty_view"
    app:recyclerviewDefaultSwipeColor="@array/google_colors"
    android:layout_marginTop="?attr/actionBarSize" />

If you add this scrollview callback, you will make it works with scrollable view, or else it will be static:

recyclerView.setScrollViewCallbacks(new ObservableScrollViewCallbacks() {
            @Override
            public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {

            }

            @Override
            public void onDownMotionEvent() {

            }

            @Override
            public void onUpOrCancelMotionEvent(ObservableScrollState observableScrollState) {
                if (observableScrollState == ObservableScrollState.UP) {
                    recyclerView.hideFloatingActionMenu();
                } else if (observableScrollState == ObservableScrollState.DOWN) {
                    recyclerView.showFloatingActionMenu();
                }
            }
        });

For how to create custom_fab_view, see floating_view.xml in example app.

chowaikong commented 8 years ago

But when I just set one FAB with the URV, it can't follow the scroll behavior, although I have try setScrollViewCallbacks to hide and show. Any solution?