具有自定义视图的对话框片段

时间:2014-08-26 15:32:02

标签: android android-alertdialog android-dialogfragment

我尝试创建一个dialogfragment来显示一系列内容。由于列表必须以异步方式加载,因此我想在对话框中插入progressbar。加载过程结束后,我想从progressbar切换到listview。目前使用发布的代码,当对话框出现时,它是空的,当我点击中性按钮(在代码中添加只是为了模拟加载过程的结束,仅此而已),listview出现没有问题,所以最后我看不到我的progressbar,我也不知道为什么。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.app_select);
builder.setNegativeButton(android.R.string.cancel,
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
builder.setNeutralButton(android.R.string.ok, null);
builder.setPositiveButton(R.string.app_reset,
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                  //do something
            }
        });

//some code and create the adapter

builder.setAdapter(adapter, this);

dg = builder.create();
lv = dg.getListView();
//At beginning the list is invisible
lv.setVisibility(View.GONE);
dg.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        fl = (FrameLayout) dg.findViewById(android.R.id.custom);
        dg.getLayoutInflater().inflate(R.layout.progress, fl);

        /* debug code only to simulate the end of the process */
        Button b = ((AlertDialog) dialog)
                .getButton(AlertDialog.BUTTON_NEUTRAL);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //load end now the list must be visible
                lv.setVisibility(View.VISIBLE);
            }
        });
        /* end debug code */
    }
});
return dg;
}

编辑:布局progress.xml

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

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50" />

</FrameLayout>

0 个答案:

没有答案