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();
setAdapter(themeAdapter); recyclerView. setHasFixedSize(true); recyclerView. setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager. HORIZONTAL, false));
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;
}
}
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With