【问题标题】:snackbar not appearing on top of fragment layout/recyclerview within viewpager小吃栏没有出现在 viewpager 中的片段布局/回收器视图的顶部
【发布时间】:2016-01-24 14:43:58
【问题描述】:

我正在尝试将小吃栏放在 3 个标签以下。我已经膨胀了一个片段并在其中实现了协调器布局。我无法在标签下方的顶部显示小吃栏。getView() 在浮动按钮上方的底部显示小吃栏,这代码How to show Snackbar at top of the screen 在顶部显示我不想要的小吃吧。如何让snackbar在fragment中的tab下方显示?

代码:

 Snackbar snackbar = Snackbar
                        .make(coordinatorLayout_snackbar, “Snackbar”, Snackbar.LENGTH_INDEFINITE)
                        .setAction(“click”, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Log.e(TAG, "button clicked");
                            }
                        });

                snackbar.show();

xml:

<?xml version="1.0" encoding="utf-8"?>


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout_snackbar”
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">
</android.support.design.widget.CoordinatorLayout>


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />
    </android.support.v4.widget.SwipeRefreshLayout>

</FrameLayout>

【问题讨论】:

    标签: android android-snackbar


    【解决方案1】:

    您需要将 SwipeRefreshLayout 包装在 CoordinatorLayout 中,如下所示:

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:fab="http://schemas.android.com/apk/res-auto"
        android:id="@+id/coordinatorLayout_snackbar”
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical" />
        </android.support.v4.widget.SwipeRefreshLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    在您当前的布局中,SwipeRefreshLayout 绘制在高度为 0 的 CoordinatorLayout 上,因为它没有子级并且高度设置为 wrap_content。

    【讨论】:

    • 我怎样才能将它设置在顶部。它目前甚至在我向下滚动之前都不会显示。它有点隐藏。
    • 你要把CoordinatorLayout放到Activity布局中,这样整个fragment就包含在CoordinatorLayout中了。
    • 能用代码解释一下吗?我试过了,它仍然显示在布局下方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多