19

I have my custom RecyclerView to create a ListView. And it works great, when I am trying to populate a list view in my layout's id.

FragmentTransaction ft = getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putBoolean("enablePullToRefresh", false);
GridValues gridValues = new GridValues();
gridValues.rowViewLayout = R.layout.my_detail_row_view;

gridValues.delegate = this;

mygrid = new CustomGridView(gridValues, bundle);
mygrid.showAsGrid = true;
mygrid.spanCount = 2;
mygrid.layoutOrientation = LinearLayoutManager.VERTICAL;
mygrid.noRowColor = true;
mygrid.gridName = "mygrid";

mygrid.setArguments(mygrid.bundle);
ft.replace(R.id.MyGridContainer, mygrid);

Now, I would like to populate a new list inside a dialog. How can I do that?

I tried this, Having mygrid as static

public static class MyDialogFragment extends DialogFragment {
    static MyDialogFragment newInstance() {
        return new MyDialogFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return mygrid.getView();
    }
}

And then,

FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = MyDialogFragment.newInstance();
ft.add(R.id.MyGridContainer, newFragment);
//getView().findViewById(R.id.MyGridContainer).setVisibility(View.VISIBLE);
ft.commit();
Ziem
  • 6,579
  • 8
  • 53
  • 86
Maheswaran Ravisankar
  • 17,652
  • 6
  • 47
  • 69

4 Answers4

39

DialogFragment is just another Fragment, Inflate your custom view like you would do for any other fragment.

public class MyDialogFragment extends DialogFragment {
    private RecyclerView mRecyclerView;
    private MyRecyclerAdapter adapter;
    // this method create view for your Dialog
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          //inflate layout with recycler view
         View v = inflater.inflate(R.layout.fragment_dialog, container, false);
        mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //setadapter
        CustomAdapter adapter = new MyRecyclerAdapter(context, customList);
            mRecyclerView.setAdapter(adapter);
         //get your recycler view and populate it.
         return v;
    }
}
David
  • 37,109
  • 32
  • 120
  • 141
rahul.ramanujam
  • 5,608
  • 7
  • 34
  • 56
  • Hi when I add recycler view and enable the scrollbar, the scrollbar is not visible (even after setting scrollbars=vertical in the XML, I also tried setting a drawable). I think it could have something to do with the themes. Do you happen to know why? – hushed_voice Aug 09 '19 at 12:21
10

Accepted answer works, but it requires additional efforts to keep it more like standard dialog.

Below is another possible way, which will allow you to keep all the dialog functionality (for instance title, icon, positive/negative/neutral buttons). The idea is to override onCreateDialog and use AlertDialog.Builder#setView() method

public class MyDialogFragment extends DialogFragment {
    private RecyclerView mRecyclerView;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mRecyclerView = new RecyclerView(getContext());
        // you can use LayoutInflater.from(getContext()).inflate(...) if you have xml layout
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(/* your adapter */);

        return new AlertDialog.Builder(getActivity())
                .setTitle(/* your title */)
                .setView(mRecyclerView)
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                // do something
                            }
                        }
                ).create();
    }
}
GregoryK
  • 3,011
  • 1
  • 27
  • 26
4

Assume that you have a static normal Fragment named mygrid, here is how your DialogFragment should look like:

public class MyDialogFragment extends DialogFragment {
    static MyDialogFragment newInstance() {
        return new MyDialogFragment();
    }

    // this method create view for your Dialog
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return mygrid.getView();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        return dialog;
    }
}

And here is how you should show it:

DialogFragment fragment = MyDialogFragment.newInstance();
fragment.show(getSupportFragmentManager(), "some tag"); // please refer to DialogFragment#show() method in documentations.
Nguyễn Hoài Nam
  • 1,130
  • 1
  • 9
  • 20
2

Showing a RecyclerView in dialog fragment is as simple as you do in normal fragment. But to show in dialog fragment you need to create a dialog like:

public class AppDialogs extends DialogFragment {
private AlertDialog.Builder builder;

public static AppDialogs newInstance(int dialogNo, String title, String msg)
{
    AppDialogs fragment = new AppDialogs();
    Bundle args = new Bundle();
    args.putInt("dialogNo",dialogNo);
    args.putString("title", title);
    args.putString("msg", msg);
    fragment.setArguments(args);

    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
    if(android.os.Build.VERSION.SDK_INT<=android.os.Build.VERSION_CODES.KITKAT) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(0, 0, 0, 0)));
    }
    return null;
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    Bundle bundle = getArguments();
    int pos = bundle.getInt("dialogNo");
    switch (pos) {
        case 0:
            return  showList();


    }

    return super.onCreateDialog(savedInstanceState);

}


private Dialog showList() {
    builder = new AlertDialog.Builder(getActivity(), R.style.app_dialog_theme);
    builder.setTitle(title);


RecyclerView rView;
  builder.setView(rView);

        return builder.create();
    }
}

and to call it from your fragment or activity you dont need any container id just call these line AppDialogs appDialogs = AppDialogs.newInstance(0, title, msg); appDialogs.setCancelable(false); appDialogs.show(getFragmentManager(), null);

It should do your work if not please let me know.

Ankur Chaudhary
  • 2,709
  • 3
  • 19
  • 30