Пользовательский диалог с RecyclerView на самом деле очень маленький на экране - PullRequest
0 голосов
/ 18 февраля 2020

Я создал функцию диалога, которая создает диалоговое окно на экране, где вы можете выбрать что-то из RecyclerView (код адаптера RView внизу):

fun showSelectACategoryDialog(context: Context,allCategories: LiveData<List<NotesCategory>>, owner: Fragment,chosenCategoryID: (categoryID: Long) -> Unit) {

    val dialog = Dialog(context)

    val adapter = DialogCategoriesRecyclerViewAdapter(context, chosenCategoryID)

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(true)
    dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    dialog.setContentView(R.layout.dialog_select_category)

    dialog.dialog_select_a_category_recyclerView.adapter = adapter

    allCategories.observe(owner, Observer {
        adapter.submitList(it)
    })

// Click listeners

   dialog.show()

Функция диалога затем вызывается с помощью:

 binding.notesCategoryTextView.setOnClickListener {
            showSelectACategoryDialog(
                context!!,
                notesEditViewModel.allCategories,
                NotesEditFragment(),
                selectedCategory(notesCategory)
            )
        }

Но когда вызывается диалоговое окно, оно выглядит следующим образом: Really small-sized dialog shows up

Вот код адаптера представления окна переработчика диалогового окна, если он актуален :


class DialogCategoriesRecyclerViewAdapter(
    var context: Context,
    var chosenCategoryID: (categoryID: Long) -> Unit
) : ListAdapter<NotesCategory, DialogCategoriesRecyclerViewAdapter.ViewHolder>(SingleCategoryDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = ViewHolder.from(parent)

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.bind(getItem(position), context)

        holder.itemView.setOnClickListener {
            chosenCategoryID(getItem(position).categoryID)
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val categoryNameTextView: TextView = itemView.findViewById(R.id.dialog_select_a_category_textView)

        fun bind(item: NotesCategory, context: Context) {
            categoryNameTextView.text = item.categoryName
        }

        companion object {
            fun from(parent: ViewGroup): ViewHolder {
                val view = LayoutInflater.from(parent.context).inflate(
                    R.layout.row_dialog_select_category_all_categories, parent, false
                )
                return ViewHolder(view)
            }
        }
    }
}

Вот макет XML диалога , который не работает:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_margin = "16dp"
    android:background = "@drawable/dialog_rounded_background">

    <TextView
        android:id = "@+id/dialog_select_a_category_title"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_marginTop = "16dp"
        android:text = "@string/dialog_categories_title"
        android:textColor = "@color/Black"
        android:textSize = "25sp"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id = "@+id/create_new_category_button"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "16dp"
        android:layout_marginEnd = "16dp"
        android:background = "@drawable/border_square_transparent"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_select_a_category_title"
        tools:ignore = "RtlSymmetry">

        <ImageView
            android:id = "@+id/_imageview"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:layout_marginStart = "16dp"
            app:layout_constraintBottom_toBottomOf = "parent"
            app:layout_constraintStart_toStartOf = "parent"
            app:layout_constraintTop_toTopOf = "parent"
            app:srcCompat = "@drawable/dialog_plus"
            tools:ignore = "ContentDescription" />

        <TextView
            android:id = "@+id/_textview"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:layout_margin = "16dp"
            android:layout_marginStart = "56dp"
            android:text = "@string/dialog_create_new_category_button"
            android:textColor = "@color/Black"
            android:textSize = "20sp"
            app:layout_constraintBottom_toBottomOf = "parent"
            app:layout_constraintEnd_toEndOf = "parent"
            app:layout_constraintStart_toEndOf = "@+id/_imageview"
            app:layout_constraintTop_toTopOf = "parent"
            app:layout_constraintVertical_bias = "0.506" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id = "@+id/dialog_select_a_category_recyclerView"
        android:layout_width = "0dp"
        android:layout_height = "0dp"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "16dp"
        android:layout_marginEnd = "16dp"
        android:layout_marginBottom = "20dp"
        android:background = "@drawable/border_square_transparent"
        app:layoutManager = "androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toBottomOf = "@+id/create_new_category_button"
        tools:listitem = "@layout/row_dialog_select_category_all_categories" />
</androidx.constraintlayout.widget.ConstraintLayout>

А вот XML макет диалога, который работает:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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 = "wrap_content"
    android:layout_margin = "16dp"
    android:background = "@drawable/dialog_rounded_background">

    <ImageButton
        android:id = "@+id/dialog_create_category_confirm"
        android:layout_width = "50dp"
        android:layout_height = "50dp"
        android:layout_marginStart = "208dp"
        android:layout_marginTop = "64dp"
        android:layout_marginEnd = "16dp"
        android:layout_marginBottom = "16dp"
        android:background = "@android:color/transparent"
        android:src = "@drawable/dialog_check"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintEnd_toEndOf = "@+id/dialog_create_category_category_name"
        app:layout_constraintStart_toEndOf = "@+id/dialog_create_category_cancel"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_create_category_category_name"
        tools:ignore = "ContentDescription" />

    <ImageButton
        android:id = "@+id/dialog_create_category_cancel"
        android:layout_width = "50dp"
        android:layout_height = "50dp"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "64dp"
        android:layout_marginBottom = "16dp"
        android:background = "@android:color/transparent"
        android:src = "@drawable/dialog_cancel"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintStart_toStartOf = "@+id/dialog_create_category_category_name"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_create_category_category_name"
        tools:ignore = "ContentDescription" />

    <EditText
        android:id = "@+id/dialog_create_category_category_name"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "32dp"
        android:layout_marginEnd = "16dp"
        android:ems = "10"
        android:hint = "@string/dialog_create_category_hint"
        android:importantForAutofill = "no"
        android:inputType = "textPersonName|textCapSentences"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.487"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_create_category_title" />

    <TextView
        android:id = "@+id/dialog_create_category_title"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_marginTop = "16dp"
        android:text = "@string/dialog_create_a_category_title"
        android:textColor = "@color/Black"
        android:textSize = "30sp"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Второй диалог вызывается нажатием «@ + id / create_new_category_button» (который является единственным который отображается на экране) и заполняет ширину экрана минус поле просто отлично

1 Ответ

0 голосов
/ 18 февраля 2020

Вы должны определить фиксированный размер для макета диалога или просто создать диалоговое окно полной ширины.

Попробуйте это:

dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
...