Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add RecyclerView(RecyclerFragment) to a Dialog

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();
like image 949
Maheswaran Ravisankar Avatar asked May 25 '15 18:05

Maheswaran Ravisankar


People also ask

How do I add recycler view to dialog?

setAdapter(themeAdapter); recyclerView. setHasFixedSize(true); recyclerView. setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager. HORIZONTAL, false));


2 Answers

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;
    }
}
like image 161
rahulrv Avatar answered Oct 02 '22 02:10

rahulrv


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();
    }
}
like image 43
GregoryK Avatar answered Oct 02 '22 01:10

GregoryK