【问题标题】:Android: Apply ActionMode to a DialogFragmentAndroid:将 ActionMode 应用于 DialogFragment
【发布时间】:2018-03-26 09:38:20
【问题描述】:

我在使用带有 DialogFragment 的 ActionMode 时遇到了一些问题,希望有人能指出我的解决方案。

当我在 DialogFragment 中启动 actionmode 时,actionmode 将应用于父片段/活动,而不是 DialogFragment 中的工具栏(下面的屏幕截图):

如何将动作模式应用于 DialogFragment 布局中的工具栏。

这是我的 DialogFragment 的代码:

class WorklistDialog : NetworkDialog(), WorklistDialogContract.View {

    private var actionModeCallback: WorklistDialogActionModeCallback? = null
    override var isInActionMode = actionModeCallback == null
    @Inject lateinit var presenter: WorklistDialogContract.Presenter

...

    override fun startActionMode() {
        actionModeCallback = WorklistDialogActionModeCallback()
        // dialog.window.decorView.startActionMode(actionModeCallback)
        mView?.multiselectview_recycler_view?.startActionMode(actionModeCallback)
    }

...

}

这是我的主要布局的 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/dialog_worklist_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/dialog_worklist_swiperefreshlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        app:layout_constraintTop_toBottomOf="@id/dialog_worklist_toolbar">

        <com.conhea.smartgfr.ui.examination.layouts.MultiSelectView
            android:id="@+id/dialog_worklist_multiselectview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </com.conhea.smartgfr.ui.examination.layouts.MultiSelectView>
    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.v7.widget.Toolbar
        android:id="@+id/dialog_worklist_toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:minHeight="?android:attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

这是自定义视图 MultiSelectLayout 的 xml:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:tools="http://schemas.android.com/tools">

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <CheckBox
            android:id="@+id/multiselectview_select_all_checkbox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:text="@string/multi_select_view_select_all"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/multiselectview_recycler_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/multiselectview_select_all_checkbox"
            tools:listitem="@layout/item_dialog_worklist" />

    </android.support.constraint.ConstraintLayout>
</merge>

【问题讨论】:

    标签: android android-layout android-dialogfragment dialogfragment


    【解决方案1】:

    答案很简单。只需对类似于 ActionMode 的工具栏应用不同的外观即可。

    WorklistDialog.kt:

    class WorklistDialog : NetworkDialog(), WorklistDialogContract.View {
    
        ...
    
        override fun startSelectionMode() {
            mView?.dialog_worklist_toolbar?.apply {
                setBackgroundColor(ResourcesCompat.getColor(resources, R.color.white, null))
                setNavigationIcon(ic_arrow_back_black_24dp)
                setNavigationOnClickListener { stopSelectionMode() }
                setTitleTextColor(ResourcesCompat.getColor(resources, R.color.black, null))
            }
        }
    
        override fun updateSelectedCount() {
            mView?.dialog_worklist_toolbar?.apply {
                title = adapter.getSelectedItems().size.toString()
            }
        }
    
        override fun stopSelectionMode() {
            setupToolbar()
        }
    
    ...
    
    }
    

    结果截图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      相关资源
      最近更新 更多