У меня есть простой пользовательский диалог выбора нескольких элементов.Если количество элементов невелико, то диалоговое окно работает нормально и показывает кнопки OK и Отмена внизу.Но если есть много элементов (поэтому вы должны прокрутить список) - никакие кнопки не отображаются.Я искал 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()));
}
}
}
layout / 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>
layout / 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;
}
}
Как я могу это исправить и сделать диалоговое окно для отображения OK Отмена кнопок ввсе дела?Любая помощь приветствуется.
Спасибо!
PS Скриншот диалога: