Android中片段/对话框之间的通信

7
我有一个包含两个Fragment的活动:一个用于以网格视图显示产品,另一个用于显示用户添加到订单中的产品(ListFragment)。当用户在网格视图中点击一个产品时,我需要显示一个对话框(DialogFragment),以询问所需产品的数量。然后,当用户在对话框中点击“接受”时,我希望产品出现在ListFragment中。
一方面,我必须将产品对象传递给对话框,以便将其名称作为对话框的标题显示(例如)。因此,我这样做:
public static class ProductDialog extends DialogFragment {

        static ProductDialog newInstance(ProductVO product) {
            ProductDialog f = new ProductDialog();

            Bundle args = new Bundle();
            args.putSerializable("product", product);
            f.setArguments(args);

            return f;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            ProductVO product = (ProductVO) getArguments().getSerializable("product");

            return new AlertDialog.Builder(getActivity())
                    .setIcon(R.drawable.ic_dialog_add)
                    .setTitle(R.string.add_product)

                    ...

                    .setPositiveButton(R.string.accept,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {

                            }
                        }
                    )
                    .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                            }
                        }
                    )
                    .create();
        }
    }

我认为这是可以的,如果我错误请纠正。但是,在正按钮的onClick事件中,我需要检索对话框中输入的数量,并将其传递到其他片段(ListFragment),此时它应该立即显示在列表中。

我应该如何做呢?

提前致谢。

1个回答

10

推荐的方法是使用接口从DialogFragment向Activity通信,然后从Activity向Fragment通信。

在您的Activity中:

public class Main extends FragmentActivity implements OnQuantitySelectedListener {

    public interface OnQuantitySelectedListener {
        void onFinishEditDialog(String inputText);
    }


    @Override
    public void onFinishEditDialog(String inputText) {
        Toast.makeText(this, "Quantity: " + inputText, Toast.LENGTH_SHORT).show();
    }
}

然后是DialogFragment内部类。
public static class ProductDialog extends DialogFragment {

    static ProductDialog newInstance(ProductVO product) {
        ProductDialog f = new ProductDialog();

        Bundle args = new Bundle();
        args.putSerializable("product", product);
        f.setArguments(args);

        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        ProductVO product = (ProductVO) getArguments().getSerializable("product");

        LayoutInflater factory = LayoutInflater.from(getActivity());
        final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
        mEditText = (EditText) textEntryView.findViewById(R.id.txt_your_name);

        return new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.ic_dialog_add)
                .setTitle(R.string.add_product)
                .setView(textEntryView)
                .setPositiveButton(R.string.accept,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                             OnQuantitySelectedListener listener = (OnQuantitySelectedListener) getActivity();
                             listener.onFinishEditDialog(mEditText.getText().toString());
                        }
                    }
                )
                .setNegativeButton(R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        }
                    }
                )
                .create();
    }
}

R.layout.alert_dialog_text_entry的XML来自API Demos。它不适合您从用户那里获取数量的用例,但它演示了使用自定义布局从用户那里获取值的方法。
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView 
        android:id="@+id/username_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="@string/alert_dialog_username"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/username_edit"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/password_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="@string/alert_dialog_password"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/password_edit"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:password="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

1
导致循环继承? - Muhammad Riyaz
我强烈感觉这个答案更多地解释了Fragment (DialogFragment)和Activity(Main)之间的通信。请查看类似问题的以下讨论: https://dev59.com/22Ml5IYBdhLWcg3wP08a - Edward Quixote

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接