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

时间:2019-07-02 02:02:52

标签: android kotlin android-databinding android-snackbar

我试图显示一个Snackbar,但在片段中不可见,它可以找到协调器的布局,如FAB向上移动所证明的那样(请参见下面的屏幕截图),但它似乎显示在视图的后面。

基本上发生的是添加任务时,消息将捆绑包中的消息发送回仪表板片段,此时将显示该消息。

请记住,小吃栏消息在整个应用程序中以相同的方式显示,而不会出现问题。

Empty Dashboard No Tasks Found

After task is added, snackbar needs to be displayed

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

用于将快餐栏消息发送到仪表板的代码

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

显示小吃店代码(onResume)

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

扩展功能,用于显示小吃栏

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

有想法吗?

1 个答案:

答案 0 :(得分:0)

我逐渐进行了更改,在回收者视图中增加了边距(无论如何,它们都应该存在),但没有任何效果。唯一有效的更改是确保根视图是“协调器布局”。

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

完成此操作后,所有操作均按预期进行。我希望我知道真正的原因,但是坦率地说,我花了太多时间试图解决这个问题。简而言之,解决方案是从该视图中删除layout(databinding),这对于该视图来说很好,因为数据绑定在这里没有实际用途。