gpt4 book ai didi

java - 当显示的列表需要滚动时,按钮从 DialogFragment 中消失

转载 作者:行者123 更新时间:2023-12-04 23:51:05 26 4
gpt4 key购买 nike

AlertDialog 完美地显示内容和按钮,直到内容增长到需要滚动才能显示所有内容的程度。此时按钮消失,即使列表完整显示。

我看到在过去的几年里有人问过这个问题(这个问题与我的基本相同(DialogFragment buttons pushed off screen API 24 and above),但答案似乎都是命中注定的,甚至是随意的:设置 layout_weight,从 setMessage() 切换到 setTitle(),最后从 AlertDialog 切换到 Dialog。

这些都不适用于我,避免 AlertDialog 不是一个吸引人的选项,因为我使用的 setMultiChoiceItems() 方法似乎(对我来说令人难以置信,也许考虑到我的知识极其有限)仅适用于 AlertDialog。

因此,在绝望和沮丧中,我直接求助于 stackoverflow 的集体大脑。

这是我的 DialogFragment 的精简版:

public class SurfaceDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.surface_dialog, null))
.setTitle(mMessage)
.setMultiChoiceItems(mSurfaceList, mCheckedSurfaces, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
mCheckedSurfacesPositions.add(which);
} else {
mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
}
}
});

// builder.setCancelable(false);
builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ProcessDialogSelections(mCheckedSurfacesPositions);
dismiss();
}

}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onSurfaceDialogSelectedSent(mUnitSurfaces);
dismiss();
}
});

return builder.create();
}

那不起作用,然后我编写了自己的按钮并显示它们:
    public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.surface_dialog, null);
builder.setView(dialogView)
.setTitle(mMessage)

.setMultiChoiceItems(mSurfaceList, mCheckedSurfaces, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
mCheckedSurfacesPositions.add(which);
} else {
mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
}
}
});

Button positiveButton = dialogView.findViewById(R.id.positive_button);
Button negativeButton = dialogView.findViewById(R.id.negative_button);

positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProcessDialogSelections(mCheckedSurfacesPositions);
dismiss();
}
});
negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onSurfaceDialogSelectedSent(mUnitSurfaces);
dismiss();
}
});

return builder.create();

}

这是我修改后的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ScrollView 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"
app:layout_constraintBottom_toBottomOf="parent"
android:orientation="vertical">

</ScrollView>


<Button
android:id="@+id/positive_button"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:text="Done"
app:layout_constraintBaseline_toBaselineOf="@+id/negative_button"
app:layout_constraintEnd_toStartOf="@+id/negative_button"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/negative_button"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:text="Cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/positive_button" />

</androidx.constraintlayout.widget.ConstraintLayout>


由于那不起作用,然后我听从了 Vikas Singh ( AlertDialog Buttons not visible when using Custom layout) 的建议。这就是新代码的样子(本质上是用 Java 而不是 XML 创建布局。
    public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// New code from Vikas Singh
LinearLayout rootLayout = new LinearLayout(getActivity());
rootLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView scrollView = new ScrollView(getActivity());
LinearLayout layout = new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.VERTICAL);

scrollView.addView(layout);
rootLayout.addView(scrollView);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("Go", null);
builder.setView(rootLayout);

builder//.setView(dialogView)
//.setTitle(mMessage)

.setMultiChoiceItems(mSurfaceList, mCheckedSurfaces, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
mCheckedSurfacesPositions.add(which);
} else {
mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
}
}
});
return builder.create();
}

当我必须滚动时仍然没有按钮。
为什么不呢,我能做些什么呢?

最佳答案

这是解决方案:

public class SurfaceDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//LayoutInflater inflater = requireActivity().getLayoutInflater();
builder
//.setView(inflater.inflate(R.layout.surface_dialog, null)) didn't work
// nor did setView with android.R.layout.select_dialog_multichoice
// nor android.R.layout.simple_list_item_multiple_choice work
.setTitle(mMessage)
.setMultiChoiceItems(mSurfaceList, mCheckedSurfaces, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
mCheckedSurfacesPositions.add(which);
} else {
mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
}
}
});

// builder.setCancelable(false);
builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ProcessDialogSelections(mCheckedSurfacesPositions);
dismiss();
}

}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onSurfaceDialogSelectedSent(mUnitSurfaces);
dismiss();
}
});

return builder.create();
}

multiChoiceItems DialogFragment 现在就像一个魅力!
我所要做的就是简单地注释掉上面的行。
结果,没有显式膨胀的布局,没有 View 集。
但它仍然有效。

现在,如果有人可以向我解释,我会很高兴的
  • 为什么这个工作的细节,
  • 为什么另一个没有,
  • 鉴于较早的失败,如何实现自定义布局以实现 View 滚动时不丢失操作按钮的结果
  • 关于java - 当显示的列表需要滚动时,按钮从 DialogFragment 中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59085660/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com