Maheswaran Ravisankar Maheswaran Ravisankar - 10 days ago 6
Android Question

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

r7v r7v
Answer

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