【问题标题】:Snackbar is not working in SwipeRefreshLayout on backpress Android StudioSnackbar 在回压 Android Studio 上的 SwipeRefreshLayout 中不起作用
【发布时间】:2021-05-01 07:58:24
【问题描述】:

这是我在 xml 文件根布局中的 activity_main.xml 是 SwipeRefreshLayout 并且我在 backpress 上实现了一个快餐栏,但显示一个在 id activity_main_swipe_refresh_layout 中调用的错误是不支持的,那么我该怎么办。

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_main_swipe_refresh_layout"
    android:background="@color/background"
    tools:context=".CoronaTracker.CoronaTrackerActivity">
<....>

这是我的 java 代码

 boolean doubleBackToExitPressedOnce = false;
    @Override
        public void onBackPressed() {
            if (doubleBackToExitPressedOnce) {
                super.onBackPressed();
                return;
            }
    
            this.doubleBackToExitPressedOnce = true;
    
            Snackbar.make(activity_main_swipe_refresh_layout, "Please click BACK again to exit", Snackbar.LENGTH_SHORT).show();
    
            new Handler().postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    doubleBackToExitPressedOnce=false;
                }
            }, 2000);

这一行显示错误

Snackbar.make(activity_main_swipe_refresh_layout, "Please click BACK again to exit", Snackbar.LENGTH_SHORT).show();


activity_main_swipe_refresh_layout It is the root layout id of the my activity but stile not working.

【问题讨论】:

    标签: java android android-studio android-layout


    【解决方案1】:

    SnackBar 仅适用于 CoordinatorLayout。 尝试使用 CoordinatorLayout 作为 root elemnet

    【讨论】:

    • 没有兄弟,它可以与 relativeLayout 和 LinerLayout 一起使用,但不能与 SwipeRefreshLayout 一起使用。
    • 将根视图设为 CoordinatorLayout 或 relativeLayout 或 LinerLayout,并将子视图设为 RefreshLayout。它工作得很好。
    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 2021-08-02
    • 1970-01-01
    • 2018-06-18
    • 2020-11-03
    • 2019-08-09
    相关资源
    最近更新 更多