简体   繁体   中英

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.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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