简体   繁体   English

Android-Snackbar在片段/协调器布局中不可见

[英]Android - Snackbar not visible in fragment/coordinator layout

I am attempting to display a Snackbar but is not visible within the fragment, it can find the coordinator layout as evidenced by the FAB moving up (see screenshots below), but it is seemingly displaying behind the view. 我试图显示一个Snackbar,但在片段中不可见,它可以找到协调器布局,如FAB向上移动所证明的那样(请参见下面的屏幕截图),但它似乎显示在视图的后面。

Basically what happens is when a task is added, a message is sent back in the bundle to the dashboard fragment at which point it would be displayed. 基本上发生的事情是添加任务时,消息将在捆绑包中发送回仪表板片段,此时将显示该消息。

Keep in mind that snackbar messages are displayed in this same manner throughout the application without issue. 请记住,小吃栏消息在整个应用程序中以相同的方式显示,没有问题。

空仪表板未找到任务

添加任务后,需要显示小吃栏

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
<data>
    <variable name="taskViewModel" type="com.pomodorocentral.task.dashboard.TaskViewModel"/>
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/task_coordinator_layout"
        tools:context="com.pomodorocentral.task.dashboard.TaskFragment"
        android:theme="@style/MyTheme.DayNight.NoActionBar"
        android:background="?android:windowBackground"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="50dp">
    <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/ToolBarStyle"
            app:popupTheme="@style/ToolBarStyle.Popup"
            android:id="@+id/task_toolbar"
            android:elevation="4dp"
            app:subtitle="Show Tasks"
            android:layout_marginTop="50dp">
        <com.google.android.material.chip.ChipGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/task_status_filter_group"
                android:visibility="visible"
                app:chipSpacing="4dp"
                android:layout_margin="16dp">
            <com.google.android.material.chip.Chip
                    style="@style/CustomChip"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/active_status_label"
                    android:id="@+id/active_status_chip"
                    app:checkedIconEnabled="true"
                    app:checkedIcon="@drawable/ic_check_white_24dp"
                    app:chipEndPadding="10dp"
                    app:chipStartPadding="10dp"
                    android:includeFontPadding="false"
                    android:elegantTextHeight="false"/>
            <com.google.android.material.chip.Chip
                    style="@style/CustomChip"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/completed_status_label"
                    android:id="@+id/completed_status_chip"
                    app:checkedIconEnabled="true"
                    app:checkedIcon="@drawable/ic_check_white_24dp"
            />
        </com.google.android.material.chip.ChipGroup>
    </androidx.appcompat.widget.Toolbar>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/task_empty_view"
            android:paddingTop="70dp"
            android:paddingBottom="15dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/addTaskButton"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            app:layout_constraintHorizontal_bias="0.0"
            android:theme="@style/MyTheme.DayNight"
            android:visibility="invisible"
            android:layout_marginTop="50dp">
        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:layout_editor_absoluteY="30dp"
                tools:layout_editor_absoluteX="121dp"
                android:id="@+id/task_empty_heading"
                tools:text="@string/task_empty_heading" android:textAppearance="@style/TextAppearance.MaterialComponents.Headline5"
                android:gravity="center"
                android:visibility="visible"
                android:text="@string/task_empty_heading"
        />
        <TextView
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:id="@+id/task_empty_text"
                android:textAppearance="@style/TextAppearance.MaterialComponents.Caption"
                tools:text="@string/task_empty_text"
                android:visibility="visible"
                android:text="@string/task_empty_text"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:gravity="center_vertical|center"/>
        <ImageView
                app:srcCompat="@drawable/ic_pomodoro_shrug"
                android:layout_width="match_parent"
                android:layout_height="340dp"
                android:id="@+id/task_empty_image"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:cropToPadding="true"/>
    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="100dp">
        <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/task_recycler_view"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
        </androidx.recyclerview.widget.RecyclerView>
    </LinearLayout>
    <com.google.android.material.floatingactionbutton.FloatingActionButton
            app:srcCompat="@drawable/ic_add_black_24dp"
            android:layout_width="wrap_content"
            android:layout_height="78dp"
            android:clickable="true"
            android:id="@+id/addTaskButton"
            android:layout_gravity="end|bottom"
            android:focusable="true"
            android:layout_marginBottom="70dp"
            android:layout_marginRight="16dp"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

Code for sending snackbar message to dashboard 用于将点心栏消息发送到仪表板的代码

binding.taskViewModel?.task?.value?.let {
    val bundle = Bundle()
    bundle.putParcelable("message", SnackbarMessage(R.string.new_task_save, Snackbar.LENGTH_SHORT))

    if (binding.taskViewModel?.task?.value?.id == null || binding.taskViewModel?.task?.value?.id == 0) {
        binding.taskViewModel?.create(it)
        navigation.navigate(R.id.action_addTaskFragment_to_action_tasks, bundle)
    } else {
        binding.taskViewModel?.update(it)
        navigation.navigate(R.id.action_editTaskFragment_to_action_tasks, bundle)
    }
}

Code for displaying snackbar (onResume) 显示小吃店的代码(onResume)

arguments?.getParcelable<SnackbarMessage>("message")?.let {
    showSnackbar(task_coordinator_layout, it.resourceId, it.duration)
    arguments?.remove("message")
}

Extension Function which is called to display the snackbar 扩展功能,用于显示小吃栏

fun Fragment.showSnackbar(view: CoordinatorLayout?, @StringRes resId: Int, 
@Snackbar.Duration duration: Int) {
    view?.let {
        Snackbar.make(it, it.resources.getString(resId), duration).show()
    }
}

Thoughts? 有什么想法吗?

I incrementally made changes, adding margins to the recycler view (they should probably be present regardless), it had no effect. 我逐步进行了更改,在回收者视图中增加了边距(无论如何,它们都应该存在),但没有任何效果。 The only change that had any effect was ensuring that the root view was the Coordinator Layout. 唯一有效的更改是确保根视图是“协调器布局”。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/task_coordinator_layout"
    tools:context="com.pomodorocentral.task.dashboard.TaskFragment"
    android:theme="@style/MyTheme.DayNight.NoActionBar"
    android:background="?android:windowBackground"
    android:layout_marginTop="50dp"
    android:layout_marginBottom="50dp">
<androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ToolBarStyle"
        app:popupTheme="@style/ToolBarStyle.Popup"
        android:id="@+id/task_toolbar"
        android:elevation="4dp"
        app:subtitle="Show Tasks">
    <com.google.android.material.chip.ChipGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/task_status_filter_group"
            android:visibility="visible"
            app:chipSpacing="4dp"
            android:layout_margin="16dp">
        <com.google.android.material.chip.Chip
                style="@style/CustomChip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/active_status_label"
                android:id="@+id/active_status_chip"
                app:checkedIconEnabled="true"
                app:checkedIcon="@drawable/ic_check_white_24dp"
                app:chipEndPadding="10dp"
                app:chipStartPadding="10dp"
                android:includeFontPadding="false"
                android:elegantTextHeight="false"/>
        <com.google.android.material.chip.Chip
                style="@style/CustomChip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/completed_status_label"
                android:id="@+id/completed_status_chip"
                app:checkedIconEnabled="true"
                app:checkedIcon="@drawable/ic_check_white_24dp"
        />
    </com.google.android.material.chip.ChipGroup>
</androidx.appcompat.widget.Toolbar>
<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/task_empty_view"
        android:paddingTop="70dp"
        android:paddingBottom="15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/addTaskButton"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        app:layout_constraintHorizontal_bias="0.0"
        android:theme="@style/MyTheme.DayNight"
        android:visibility="invisible"
        android:layout_marginTop="50dp">
    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:layout_editor_absoluteY="30dp"
            tools:layout_editor_absoluteX="121dp"
            android:id="@+id/task_empty_heading"
            tools:text="@string/task_empty_heading"
            android:textAppearance="@style/TextAppearance.MaterialComponents.Headline5"
            android:gravity="center"
            android:visibility="visible"
            android:text="@string/task_empty_heading"
    />
    <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:id="@+id/task_empty_text"
            android:textAppearance="@style/TextAppearance.MaterialComponents.Caption"
            tools:text="@string/task_empty_text"
            android:visibility="visible"
            android:text="@string/task_empty_text"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:gravity="center_vertical|center"/>
    <ImageView
            app:srcCompat="@drawable/ic_pomodoro_shrug"
            android:layout_width="match_parent"
            android:layout_height="340dp"
            android:id="@+id/task_empty_image"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:cropToPadding="true"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_marginBottom="?attr/actionBarSize"
        android:id="@+id/task_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
        app:srcCompat="@drawable/ic_add_black_24dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:id="@+id/addTaskButton"
        android:layout_gravity="end|bottom"
        android:focusable="true"
        android:layout_margin="16dp"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

After doing this everything worked as expected. 完成此操作后,一切都按预期进行。 I wish I knew the true reason why, but quite frankly I've burnt far too much time trying to get this fixed. 我希望我知道真正的原因,但是坦率地说,我花了太多时间试图解决这个问题。 In a nutshell the fix was removing the layout(databinding) from this view, which is fine for this view, as the databinding had no real use here. 简而言之,修复方法是从该视图中删除layout(databinding),这对于该视图来说是很好的,因为databinding在这里没有实际用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM