开发者社区> 问答> 正文

调用的AlertDialog构建器的按钮工作正常,直到列表需要滚动,然后它们消失

我看到在过去的几年里,有人问过这个问题,但是答案似乎都是命中注定或错失,甚至是偶然的:很少有相同的解决方案,而且没有一种解决方案对我有用。在几乎与我相同的设置中(DialogFragment按钮从屏幕API 24及更高版本中退出),唯一的建议是使用setTitle()而不是setMessage(),但是即使我都不使用它们,滚动时我的按钮仍然消失。但是,如果对话框内容永远不需要滚动,则一切看起来都会正常。

如果我有一个没有子级的ScrollView根,或者将FrameLayout甚至CoordinatorLayout用作子级,这似乎无关紧要。我试图将多汁的layout_marginBottom添加到我的ScrollView中,但这也没有任何改变。

因此,在绝望和沮丧中,我直接吸引了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();
            }
        }).setNeutralButton(mNeutralButtonLabel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dismiss();
            }
        });

        return builder.create();
    }

surface_dialog.XML也是裸露的骨头,因为似乎没有什么改变(我什至可以乘坐CoordinatorLayout而不会影响显示或行为-正面和负面的按钮将正常显示,直到对话框必须滚动为止,然后他们消失了。

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    android:layout_marginBottom="30dp">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</ScrollView>

展开
收起
垚tutu 2019-11-29 12:00:45 666 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载