Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested CoordinaterLayout hides snackbar

I have a parent view whose root view is a CoordinatorLayout. The parent layout contains a TabLayout and a ViewPager. The ViewPager contains two child fragments. The child fragment also has a CoordinatorLayout as its root view and contains a RecyclerView. Here are my xml files:

Parent:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/explore_viewpager"
    android:layout_width="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_height="match_parent"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/main_activity_appbar"
    android:layout_width="match_parent"
    app:elevation="2dp"
    android:layout_height="wrap_content">
    <android.support.design.widget.TabLayout
        android:id="@+id/explore_tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        android:background="@color/tab_layout_bg"
        app:tabIndicatorColor="@color/tab_indicator_color"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>



</android.support.design.widget.CoordinatorLayout>

Child:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/basic_recycler_view_layout_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:scrollbars="vertical"
    android:clipToPadding="false"
    android:paddingTop="@dimen/single_margin"
    android:paddingBottom="@dimen/single_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</android.support.design.widget.CoordinatorLayout>

Now when I try to show a SnackBar from the child fragment, it doesn't show and is hidden offscreen. If I scroll the child fragment, the TabLayout in the parent collapses (as intended) and the SnackBar appears. I searched around and it is having a similar behavior to this: Android Design Support Library FAB in a ViewPager Fragment with Coordinator Layout except that the SnackBar is fully hidden. I think that is because the TabLayout and the SnackBar have the same height.

How do I set up the layouts so that the SnackBar will show?

This was a bit complicated to explain so let me know if I can clarify any parts.

like image 617
Sree Avatar asked Oct 19 '22 19:10

Sree


1 Answers

I figured it out. It was basically the same problem as the FAB issue I linked above. I needed to show it in the parent activity/fragment. So the way I fixed the issue was to get the root view of the parent.

private View getParentLayoutRootView(){

    if(getParentFragment() != null) { //if it has a parent fragment
       return getParentFragment().getView();
    }
    else {
        return getActivity().getWindow().getDecorView().getRootView();
    }
}

And then do something like this:

Snackbar.make(getParentLayoutRootView(), message, duration)
like image 108
Sree Avatar answered Nov 02 '22 23:11

Sree