miguelhincapie / CustomBottomSheetBehavior

Custom BottomSheetBehavior for Android that mimic Google Maps behavior
Apache License 2.0
915 stars 184 forks source link

The best way dynamically add fragment to NestedScrollView? #22

Open ZherebtsovAlexandr opened 7 years ago

ZherebtsovAlexandr commented 7 years ago

For example, instead

   <include
                layout="@layout/bottom_sheet_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true" />

Use

            <FrameLayout
                 android:id="@+id/bottom_sheet_content"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:fitsSystemWindows="true" />

and commit fragment, for example by button click

TestFragment testFragment = (TestFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.bottom_sheet_content);

                FragmentTransaction transaction = getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.bottom_sheet_content, TestFragment.newInstance());

                transaction.commit();

Right now in sample it works incorrect.

miguelhincapie commented 7 years ago

Hello, We wanted to keep sample project easy to understand how to use the lib, but in real life you usually use a Fragment like you are trying.

You can use something like this:

            android:id="@+id/bottom_sheet_fragment"
            android:name="xx.xxx.BottomSheetFragment_"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:layout="@layout/bottom_sheet_content" />

the _ at the end of xx.xxx.BottomSheetFragment is because I took it from a real project in where I'm using Android Annotations.

ZherebtsovAlexandr commented 7 years ago

@miguelhincapie thank you for quick answer! Do yo mean use:

<fragment
            android:id="@+id/bottom_sheet_fragment"
            android:name="xx.xxx.BottomSheetFragment_"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:layout="@layout/bottom_sheet_content" />

But how can I pass some parameters to the fragment, for example?

BottomSheetFragment.newInstance(param1, param2)
miguelhincapie commented 7 years ago

Yeah! you can handle the fragment like any simple fragment, this Lib doesn't care about or don't touch anything about the content inside "include" or "fragment". I mean this library only cares about 3 things:

  1. BottomSheet got anchor_point state
  2. Parallax effect in the backdrop (image, view, viewpager, etc)
  3. The 2 appbarlayout the app shows depends on the current bottomsheet's state.

You can do what you said before: .replace(R.id.bottom_sheet_content, TestFragment.newInstance());

If you get stuck trying it I can upload a more detailed sample to the project showing it... but I trusth you can do it ;)

ZherebtsovAlexandr commented 7 years ago

Thank you! I'll try to make a fork of the library and show the problem. I think it is not related with the BottomSheetBehaviorGoogleMapsLike. The problem is that if NestedScrollView will be created with an empty View (container for fragment) and after add fragment to the View (by button click) then will not works correct (View doesn't render fragment content). But if add fragment in onViewCreated method then all works correct.

ZherebtsovAlexandr commented 7 years ago

I created fork and reproduced problem described above you can see my code to understand the problem.

Code: https://github.com/ZherebtsovAlexandr/CustomBottomSheetBehavior/blob/master/app/src/main/java/co/com/parsoniisolutions/custombottomsheetbehavior/sample/MainActivity.java#L108

See short giff:

device-2017-01-16-011608

miguelhincapie commented 7 years ago

ohhh I get it! I have same problem in a big project... I was thinking it was something wrong in my code (I'm using deep link) to open some views with this lib and I'm facing same problem.

I know about some bugs related to resizing original bottomsheet and the works around it, but I have not found anything for this one you are showing.

If you take a look when the components get "refreshed", you will see its happening when you hide/unhide any item. Next code is what I'm using when I'm changing the content of the bottomsheet cuz a weird bug related to original BottomSheet.

        ViewTreeObserver observer = bottom_sheet_principal_container.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                fixBottomSheetHeight();
                if (bottom_sheet_principal_container == null)
                    return;
                bottom_sheet_sucursal_principal_container.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
private void fixBottomSheetHeight() {
        if (reciclerview == null)
            return;
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int height = size.y;

        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) reciclerview.getLayoutParams();

        if (bottom_sheet_principal_container.getHeight() < height)
            p.setMargins(p.leftMargin, p.topMargin, p.rightMargin, p.bottomMargin + (height - bottom_sheet_principal_container.getHeight()));
        else
            p.setMargins(p.leftMargin, p.topMargin, p.rightMargin, getResources().getDimensionPixelSize(R.dimen.term_cond_text_margin));

        reciclerview.requestLayout();
    }

Let's find whats going on xD.

miguelhincapie commented 7 years ago

By the way, have you tried using

.post(new Runnable() {...

when you are swiping/adding new fragment?

ZherebtsovAlexandr commented 7 years ago

@miguelhincapie not tried, I added fragment in main thread. What give this?

ZherebtsovAlexandr commented 7 years ago

Issue related with onLayoutChild method, i have noticed that parent.onLayoutChild(child, layoutDirection); doesn't call after added fragment.

ZherebtsovAlexandr commented 7 years ago

This hack works http://stackoverflow.com/a/35804525/1890996

I using now

I click button for showing bottom sheet

btnShowBottomSheet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                FragmentManager fragmentManager = getSupportFragmentManager();

                if (fragmentManager.getBackStackEntryCount() > 0) {
                    fragmentManager.popBackStackImmediate();
                    fragmentManager
                            .beginTransaction()
                            .replace(R.id.bottom_sheet_fragment, TestFragment.newInstance("From runtime"))
                            .addToBackStack(null)
                            .commit();
                } else {
                    fragmentManager
                            .beginTransaction()
                            .replace(R.id.bottom_sheet_fragment, TestFragment.newInstance("From runtime"))
                            .commit();
                }

                behavior.setState(BottomSheetBehaviorGoogleMapsLike.STATE_COLLAPSED);
            }
        });

And mannually call onLayoutChild in callback :

 behavior.addBottomSheetCallback(new BottomSheetBehaviorGoogleMapsLike.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                switch (newState) {
                     ...
                    case BottomSheetBehaviorGoogleMapsLike.STATE_COLLAPSED:
                        Log.d("bottomsheet-", "STATE_COLLAPSED");
                        bottomSheet.requestLayout();
                        behavior.onLayoutChild(coordinatorLayout, bottomSheet, ViewCompat.LAYOUT_DIRECTION_LTR);
                        break;
                     ...
                }
            }

        });
miguelhincapie commented 7 years ago

Well being hack or not if it works I will use it too hahahaha. I need it for the current app I'm finishing.

miguelhincapie commented 7 years ago

Have you seen Recent Support Library Revisions? they are talking about Fragment transactions. I'm gonna take a look at it to see if that can help us

MelnykovOleksii commented 7 years ago

Place this solution in the example! This will greatly help those who will use this library.

miguelhincapie commented 7 years ago

At this I'm full time with some personal project, I will do it when I can or if you can do it, I appreciate it.