【问题标题】:Nested CoordinaterLayout hides snackbar嵌套 CoordinaterLayout 隐藏快餐栏
【发布时间】:2015-12-30 17:28:20
【问题描述】:

我有一个父视图,其根视图是CoordinatorLayout。父布局包含TabLayoutViewPagerViewPager 包含两个子片段。子片段也有一个CoordinatorLayout 作为它的根视图并包含一个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>

孩子:

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

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

如何设置布局以显示SnackBar

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

【问题讨论】:

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


    【解决方案1】:

    我想通了。这与我上面链接的 FAB 问题基本相同。我需要在父活动/片段中显示它。所以我解决问题的方法是获取父级的根视图。

    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)
    

    【讨论】:

      猜你喜欢
      • 2015-03-07
      • 1970-01-01
      • 2018-03-14
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      相关资源
      最近更新 更多