将RecyclerView(RecyclerFragment)添加到对话框

我有自定义的RecyclerView来创建ListView。 当我尝试在我的布局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); 

现在,我想在对话框中填充一个新列表。 我怎样才能做到这一点?

我试过这个,让mygrid成为静态的

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

接着,

 FragmentTransaction ft = getFragmentManager().beginTransaction(); DialogFragment newFragment = MyDialogFragment.newInstance(); ft.add(R.id.MyGridContainer, newFragment); //getView().findViewById(R.id.MyGridContainer).setVisibility(View.VISIBLE); ft.commit(); 

  • RecyclerView适配器notifyDataSetChanged不起作用
  • 如何使用Espresso计算RecyclerView项目
  • Android RecyclerView最后一项删除(运行时)错误
  • Firebase安卓分页
  • Android - 如何动态更改Recyclerview高度?
  • RecyclerView针对不同项目的不同itemDecoration
  • 如何select性地装饰RecyclerView项目
  • 在主题级别的RecyclerView造型
  • DialogFragment只是另一个片段,像对待任何其他片段一样,为您的自定义视图充气。

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

    已接受的答案有效,但需要额外的努力才能使其更像标准对话框。

    下面是另一种可能的方法,它允许您保留所有对话function(例如标题,图标,正/负/中性按钮)。 我们的想法是覆盖onCreateDialog并使用AlertDialog.Builder#setView()方法

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

    假设你有一个名为mygrid的静态正常片段,这里是你的DialogFragment应该是这样的:

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

    以下是您应该如何展示它:

     DialogFragment fragment = MyDialogFragment.newInstance(); fragment.show(getSupportFragmentManager(), "some tag"); // please refer to DialogFragment#show() method in documentations. 

    在对话框片段中显示RecyclerView就像在普通片段中一样简单。 但要在对话框片段中显示,您需要创建一个对话框,如:

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

    并从你的片段或活动中调用它你不需要任何容器ID只需调用这些行AppDialogs appDialogs = AppDialogs.newInstance(0, title, msg); appDialogs.setCancelable(false); appDialogs.show(getFragmentManager(), null); AppDialogs appDialogs = AppDialogs.newInstance(0, title, msg); appDialogs.setCancelable(false); appDialogs.show(getFragmentManager(), null);

    它应该做你的工作,如果没有请告诉我。