android - 嵌套的 CoordinatorLayout 隐藏 snackbar

标签 android android-fragments android-viewpager android-coordinatorlayout android-snackbar

我有一个父 View ,其 Root View 是一个 CoordinatorLayout。父布局包含一个 TabLayout 和一个 ViewPagerViewPager 包含两个子 fragment 。子 fragment 还有一个 CoordinatorLayout 作为其 Root View ,并包含一个 RecyclerView。这是我的 xml 文件:

家长:

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

现在,当我尝试显示子 fragment 中的 SnackBar 时,它没有显示,而是隐藏在屏幕外。如果我滚动子 fragment ,父 fragment 中的 TabLayout 会折叠(如预期的那样)并出现 SnackBar。我四处搜索,它的行为与此类似:Android Design Support Library FAB in a ViewPager Fragment with Coordinator Layout除了 SnackBar 是完全隐藏的。我认为这是因为 TabLayoutSnackBar 具有相同的高度。

如何设置布局以便显示 SnackBar

解释起来有点复杂,如果我能澄清任何部分,请告诉我。

最佳答案

我想通了。这与我上面链接的 FAB 问题基本上是同一个问题。我需要在父 Activity/fragment 中显示它。所以我解决这个问题的方法是获取父 View 的 Root View 。

private View getParentLayoutRootView(){

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

然后做这样的事情:

Snackbar.make(getParentLayoutRootView(), message, duration)

关于android - 嵌套的 CoordinatorLayout 隐藏 snackbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34534205/

相关文章:

android - 如何创建具有 ViewPager 功能的水平 ScrollView ?

android - 如何将tablayout与底部导航和viewpager一起使用?

java - 转换 JSON 字符串对象列表

java - 初始化类实例?

android - 方形奥托 : @Subscribe is not called in fragment (Android)

java - 我正在 fragment 中启动我的 Activity ,但变量变得空了?

android - NotificationManager.notify 不会在一个应用程序中创建通知,尽管在另一个应用程序中使用完全相同的编码

android - Android应用APK中HACKED-META-INF文件夹的用途是什么

Android:在 XML 中使用 StreetViewPanoramaView

android - 自定义 viewpager 页面转换