【问题标题】:DialogFragment with RecyclerView issue in onCreateDialogonCreateDialog 中带有 RecyclerView 问题的 DialogFragment
【发布时间】:2020-06-29 20:19:12
【问题描述】:

我需要在 DialogFragment 中添加正/负按钮。 这是没有这些按钮的对话框:

这是我用来实现它的代码:

public class RecyclerColorsDialogFragment extends DialogFragment
{

RecyclerView recyclerView;
RecyclerColorsDialogAdapter adapter;
ArrayList<Boolean>colorChecked;
ArrayList<String>items;
ArrayList<String>colors;

public Dialog onCreateDialog(Bundle savedInstanceState)
{
    Dialog dialog = new Dialog(getActivity());
    dialog.setCancelable(true);



    return dialog;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
   View rootView = inflater.inflate(R.layout.fragment_recycler_colors_dialog, container, false);

    colorChecked = new ArrayList<>(12);
    colors = new ArrayList<>(12);

    String[] cItems = {getString(R.string.red),getString(R.string.green), getString(R.string.blue),getString(R.string.yellow),
    getString(R.string.azur),getString(R.string.black),getString(R.string.white),getString(R.string.gray),getString(R.string.brown),
    getString(R.string.pink),getString(R.string.purple)};

    items = new ArrayList<>(Arrays.asList(cItems));

    //inizializzo i check a false
    for (int i = 0; i< items.size(); i++)
     colorChecked.add(true);

    recyclerView = (RecyclerView)rootView.findViewById(R.id.recColors);
    adapter = new RecyclerColorsDialogAdapter(getActivity(),colors,colorChecked,items);

    final Dialog dialog = getDialog();

    dialog.setCancelable(true);
    dialog.setTitle("Add a picture to your aircraft:");

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
    recyclerView.setAdapter(adapter);



    return rootView;
}

@Override
public void onCancel(DialogInterface dialog)
{
    super.onCancel(dialog);
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    dialog.cancel();
}
}

这是来自其他片段的调用:

final RecyclerColorsDialogFragment dialog = new RecyclerColorsDialogFragment();
    dialog.setTargetFragment(AddAicraftFivePartFragment.this, 2);


    logo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager manager = getActivity().getSupportFragmentManager();
            dialog.show(manager, "PATAG");

当我尝试在onCreateDialog() 中执行此操作时遇到问题:

public Dialog onCreateDialog(Bundle savedInstanceState)
{
 //        Dialog dialog = new Dialog(getActivity());
 //       dialog.setCancelable(true);

return new AlertDialog.Builder(getActivity())
            .setCancelable(true)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            // do something...
                        }
                    }
            )
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.dismiss();
                        }
                    }
            ).create();


   // return dialog;
}

因为它显示了它:

没有带有物品的回收站视图。
我该如何解决?

谢谢

【问题讨论】:

    标签: android android-recyclerview android-dialogfragment


    【解决方案1】:

    将代码从 onCreate 放到 onCreateDialog。然后将 rootView 设置为对话框视图。并移除 onCreate() 方法

    @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(getActivity()).
                    setTitle("Add a picture to your aircraft:").setMessage(mMessage).
                    setPositiveButton(getString(R.string.discard), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            getActivity().finish();
                        }
                    }).setNegativeButton(getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    
            Inflater inflater = getActivity().getLayoutInflater();
            View rootView = inflater.inflate(R.layout.fragment_recycler_colors_dialog, container, false);
    
            colorChecked = new ArrayList<>(12);
            colors = new ArrayList<>(12);
    
            String[] cItems = {getString(R.string.red), getString(R.string.green), getString(R.string.blue), getString(R.string.yellow),
                    getString(R.string.azur), getString(R.string.black), getString(R.string.white), getString(R.string.gray), getString(R.string.brown),
                    getString(R.string.pink), getString(R.string.purple)};
    
            items = new ArrayList<>(Arrays.asList(cItems));
    
            //inizializzo i check a false
            for (int i = 0; i < items.size(); i++)
                colorChecked.add(true);
    
            recyclerView = (RecyclerView) rootView.findViewById(R.id.recColors);
            adapter = new RecyclerColorsDialogAdapter(getActivity(), colors, colorChecked, items);
    
    
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
            recyclerView.setAdapter(adapter);
            dialog.setView(rootView)
            return dialog.create();
        }
    

    【讨论】:

    • 什么??如果你将onCreateView 中的所有代码都放入onCreateDialog,那么你应该如何扩展布局?根视图从何而来?取自什么inflater.inflate 变量?? container 来自哪里???这到底是如何被接受为解决方案的?
    • @busuu 现在编辑了答案。 inflater 和 container 来自 onCreateView 方法。
    • 无法从这个答案中理解任何内容
    • 你不能返回 dialog.create()。这有 void 作为返回类型,但需要查看
    猜你喜欢
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    相关资源
    最近更新 更多