使用RecyclerView的“自定义”对话框未显示“确定”取消按钮

时间:2019-09-29 07:01:52

标签: java android dialog

我有一个简单的自定义多选项目对话框。如果项目数量很少,则对话框可以正常工作,并在底部显示“确定”和“取消”按钮。但是,如果有很多项目(因此您必须滚动列表),则不会显示任何按钮。我已经在SO中搜索了自己的问题,但没有运气。我已经在Android API 27..29上测试了对话框-相同。也许我在布局属性等方面缺少了一些重要内容...

这是我的代码和xml:

package ru.vitvlkv.myapp.gui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.Set;

import ru.vitvlkv.myapp.R;
import ru.vitvlkv.myapp.playlists.Exercise;

public class SelectExercisesDialog extends DialogFragment {
    private static final String TAG = SelectExercisesDialog.class.getSimpleName();

    private final String message;
    private final List<Exercise> exercises;
    private RecyclerView recyclerView;
    private final SelectExercisesDialog.OnOKClickListener onOKButtonClick;
    private final Set<String> selectedExercisesIds;

    public interface OnOKClickListener {
        void onClick(Set<String> exerciseIds);
    }

    public SelectExercisesDialog(String message, List<Exercise> exercises, Set<String> selectedExercisesIds,
                                 final SelectExercisesDialog.OnOKClickListener onButtonOKClick) {
        this.message = message;
        this.exercises = exercises;
        this.selectedExercisesIds = selectedExercisesIds;
        this.onOKButtonClick = onButtonOKClick;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.select_exercises_dialog, null);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(new AllExercisesAdapter(exercises, selectedExercisesIds));

        setRetainInstance(true);

        return new AlertDialog.Builder(getActivity())
            .setView(view)
            .setMessage(message)
            .setPositiveButton(R.string.dialog_button_ok, (DialogInterface dialog, int id) -> {
                onOKButtonClick.onClick(selectedExercisesIds);
            })
            .setNegativeButton(R.string.dialog_button_cancel, (DialogInterface dialog, int id) -> {
            })
            .create();
    }

    private static class AllExercisesAdapter extends RecyclerView.Adapter<ExerciseViewHolder> {
        private final List<Exercise> exercises;
        private final Set<String> checkedExercisesIds;

        public AllExercisesAdapter(List<Exercise> exercises, Set<String> checkedExercisesIds) {
            this.exercises = exercises;
            this.checkedExercisesIds = checkedExercisesIds;
        }

        @NonNull
        @Override
        public ExerciseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.select_exercise_item_view, parent, false);

            return new ExerciseViewHolder(view, checkedExercisesIds);
        }

        @Override
        public void onBindViewHolder(@NonNull ExerciseViewHolder holder, int position) {
            Exercise exercise = exercises.get(position);
            holder.setExercise(exercise);
        }

        @Override
        public int getItemCount() {
            return exercises.size();
        }
    }


    private static class ExerciseViewHolder extends RecyclerView.ViewHolder {
        private Exercise exercise = null;
        private final TextView textView;
        public final CheckBox checkBox;
        private final Set<String> checkedExercisesIds;

        public ExerciseViewHolder(@NonNull View view, Set<String> checkedExercisesIds) {
            super(view);
            this.checkedExercisesIds = checkedExercisesIds;
            textView = view.findViewById(R.id.textView);
            checkBox = view.findViewById(R.id.checkBox);
            checkBox.setOnCheckedChangeListener((compoundButton, isChecked) -> {
                if (isChecked) {
                    checkedExercisesIds.add(exercise.getId());
                } else {
                    checkedExercisesIds.remove(exercise.getId());
                }
            });
        }

        public void setExercise(Exercise exercise) {
            this.exercise = exercise;
            textView.setText(exercise.getName());
            checkBox.setChecked(checkedExercisesIds.contains(exercise.getId()));
        }
    }
}

布局/select_exercises_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

布局/select_exercise_item_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="@dimen/list_item_height"
    android:layout_marginLeft="@dimen/margin_medium"
    android:layout_marginRight="@dimen/margin_medium"
    android:gravity="center_vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/margin_medium"
            android:text="MyExercise"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

Exercise类只是一个POJO:

package ru.vitvlkv.myapp.playlists;

public class Exercise {
    private final String id;

    private final String name;

    private Exercise(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }
}

如何解决此问题并使对话框在所有情况下均显示“确定取消”按钮? 任何帮助表示赞赏。

谢谢!

P.S。对话框的屏幕截图:

Dialog without buttons

3 个答案:

答案 0 :(得分:1)

如果使用对话框片段,则为对话框样式的片段,其中包含一些对话框规范,如果要添加确定并取消,则有两种选择

  1. 从AlertDialog.Builder扩展该类,并在创建时添加肯定按钮和否定按钮处理程序以及文本

有关代码的示例如下所示

class ExampleDialog(context: Context) : AlertDialog.Builder(context) {


private val view by lazy {
    LayoutInflater.from(context).inflate(R.layout.dialog_exmaple, null, false)
}

override fun setView(layoutResId: Int): AlertDialog.Builder {

    // do the recylceview init from the view code here 

    return super.setView(view)
}


override fun setPositiveButton(
    text: CharSequence?,
    listener: DialogInterface.OnClickListener?
): AlertDialog.Builder {
    return super.setPositiveButton(
        context.getText(R.string.action_ok)
    ) { p0, p1 ->


    }
}


override fun setNegativeButton(
    text: CharSequence?,
    listener: DialogInterface.OnClickListener?
): AlertDialog.Builder {
    return super.setNegativeButton(
        context.getText(R.string.action_cancel)
    ) { p0, p1 ->


    }
}

}

  • 请注意,我不喜欢这种分配方式

    1. 您可以将xml中的两个按钮添加到回收视图的底部,然后像确定一样向其中添加文本,并通过高阶函数或接口取消和处理onClick,这取决于您,我可以显示您和下面的示例

我希望这段代码对我来说更干净,并在dialogFragment类中添加单击侦听器

<androidx.constraintlayout.widget.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">


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/listData"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@id/actionOk"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<com.google.android.material.button.MaterialButton
    android:id="@+id/actionOk"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="@string/action_ok"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/listData" />

<com.google.android.material.button.MaterialButton
    android:id="@+id/actionCancel"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="@string/action_cancel"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/actionOk"
    app:layout_constraintTop_toBottomOf="@id/listData" />


  override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = super.onCreateDialog(savedInstanceState)
    val root = ConstraintLayout(activity)
    root.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setContentView(root)
    dialog.window?.let {
        it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        it.setWindowAnimations(R.style.DialogAnimationNormal)
    }
    dialog.setCanceledOnTouchOutside(true)
    return dialog
}

答案 1 :(得分:0)

我即兴解决了@radesh建议的解决方案,并得出了结论(仍然不是我真正需要的):

<?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"
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="150dp">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        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_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

此布局至少显示“确定取消”按钮和带有项目的列表。但是仍然在这种情况下,我的对话框并不想占用设备屏幕的所有可用空间。列表recyclerView非常有限(高度仅为150dp)。当然,我希望对话框占用尽可能多的空间。

enter image description here

答案 2 :(得分:0)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
android:minHeight= "200dp">


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 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="wrap_content"
        android:layout_marginLeft="@dimen/margin_medium"
        android:layout_marginRight="@dimen/margin_medium"
        android:gravity="center_vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin_medium"
                android:text="MyExercise"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <CheckBox
                android:id="@+id/checkBox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </FrameLayout>