Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FragmentSheet #46

Closed
SamThompson opened this issue Aug 8, 2015 · 12 comments
Closed

FragmentSheet #46

SamThompson opened this issue Aug 8, 2015 · 12 comments

Comments

@SamThompson
Copy link
Contributor

This commons component should take a fragment and show it in the bottomsheet.

@evant
Copy link
Contributor

evant commented Sep 22, 2015

As a workaround, I'm giving the bottom sheet a view that's just a FrameLayout with an id. And then adding a fragment to the fragment manager with that id.

@markrietveld
Copy link
Contributor

That sounds right. That should be the canonical solution

@evant
Copy link
Contributor

evant commented Oct 21, 2015

Actually, can you reopen this? Fragment support has turned out to be nontrivial. In addition to knowing how to add the fragment correctly for the bottom sheet to measure correctly, you need to save and restore state across rotation in order to not have the app crash when trying to re-attach the fragment to a now missing id.

@markrietveld
Copy link
Contributor

Sure

@markrietveld markrietveld reopened this Oct 21, 2015
@efung
Copy link

efung commented Oct 26, 2015

Also, handling the Back button press. I tried adding a fragment to the Bottom Sheet, and pressing Back did not cause the bottom sheet to be dismissed, but caused the Activity to get finished.

@Zeliret
Copy link

Zeliret commented Nov 5, 2015

Thats how I do it in my code for now:

private void doShowBottomSheet(final Fragment fragment) {
        View view = new FrameLayout(getContext());
        view.setId(R.id.bottom_sheet_fragment);
        view.setBackgroundResource(R.color.background);

        BottomSheetLayout layout = getBase().getBottomSheetLayout();
        layout.setOnSheetStateChangeListener(state -> {
            if (state == BottomSheetLayout.State.PREPARING) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                fm.beginTransaction()
                  .replace(view.getId(), fragment)
                  .commit();
            }

            switch (state) {
                case PEEKED:
                    sheetShown = true;
                    sheetExpanded = false;
                    break;
                case EXPANDED:
                    sheetShown = true;
                    sheetExpanded = true;
                    break;
                default:
                    sheetShown = false;
                    sheetExpanded = false;
                    break;
            }
        });

        layout.showWithSheetView(view, null, bottomSheetLayout -> {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            fm.beginTransaction()
              .remove(fragment)
              .commit();
        });
}

But there is an annoying problem that at the first attempt the fragment is not showing. Only second time and so on. :(
I think the first time it can't measure the view. If I swipe to expand the sheet it shows.

I'm sorry for my bad english. I did my best to explain the situation :)

@evant
Copy link
Contributor

evant commented Nov 5, 2015

That's probably because fm.commit() is not immediate. Try doing fm.executePendingTransactions() right after adding the fragment.

@Zeliret
Copy link

Zeliret commented Nov 5, 2015

@evant Well, if I use fm.executePendingTransactions(), the fm throws an exception that a container view is not found.

@evant
Copy link
Contributor

evant commented Nov 5, 2015

Oh right you have to do it after the view is attached to the window. If I get some time, I'll write up a proper way to do this. We are doing it in our app but I'm not particularly happy with the solution.

@evant
Copy link
Contributor

evant commented Nov 5, 2015

Ok, created a pull request with a solution that's based on DialogFragment #79

@Zeliret
Copy link

Zeliret commented Nov 9, 2015

@evant I think you have done a great job! I look forward when the PR will be accepted.

@ZacSweers
Copy link
Contributor

Done via #79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants