在onCreateDialog中使用RecyclerView问题的DialogFragment

时间:2016-12-19 11:47:42

标签: android android-recyclerview android-dialogfragment

我需要添加我的DialogFragment正/负按钮。 这是没有这些按钮的对话框: enter image description here

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

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;
}

因为它显示了:    enter image description here

没有物品回收查看。
   我怎么能解决它?

谢谢

1 个答案:

答案 0 :(得分:0)

将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();
            }
        });

//custom view

    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);




        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
        recyclerView.setAdapter(adapter);
           dialog.setView(rootView)
            return dialog.create();
        }