我有一个父 View ,其 Root View 是一个 CoordinatorLayout
。父布局包含一个 TabLayout
和一个 ViewPager
。 ViewPager
包含两个子 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
是完全隐藏的。我认为这是因为 TabLayout
和 SnackBar
具有相同的高度。
如何设置布局以便显示 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/