带有RecyclerView的Android自定义对话框未显示OK取消按钮

问题描述 投票:0回答:2

我有一个简单的自定义多选项目对话框。如果项目数量很少,则对话框可以正常工作,并在底部显示“确定”和“取消”按钮。但是,如果有很多项目(因此您必须滚动列表),则不会显示任何按钮。我已经在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

java android dialog
2个回答
0
投票

我认为使用对话框时最好使用此recycelerView

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

0
投票

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

  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" />

© www.soinside.com 2019 - 2024. All rights reserved.