将 RecyclerView(RecyclerFragment) 添加到对话框
Posted
技术标签:
【中文标题】将 RecyclerView(RecyclerFragment) 添加到对话框【英文标题】:Add RecyclerView(RecyclerFragment) to a Dialog 【发布时间】:2015-08-07 06:55:06 【问题描述】:我有我的自定义 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();
【问题讨论】:
只需应用与 normalFragment
相同的逻辑
为什么不使用 AlertDialog.Builder 方法?在那里你可以调用 addView()
@Blackbelt ,我尝试使用 FragmentTransaction 但不幸的是它没有用。我对核心概念不太了解,我仍然是一个学习者。
请发布CustomGridView
是什么
@bGorle 只是一个 RecyclerView , Insipred(!) 来自Here..
【参考方案1】:
DialogFragment 只是另一个 Fragment,像对任何其他 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;
【讨论】:
嗨,当我添加回收器视图并启用滚动条时,滚动条不可见(即使在 XML 中设置滚动条=垂直后,我也尝试设置可绘制对象)。我认为这可能与主题有关。你知道为什么吗?【参考方案2】:接受的答案有效,但需要额外的努力才能使其更像标准对话。
下面是另一种可能的方式,它允许您保留所有对话框功能(例如标题、图标、正/负/中性按钮)。这个想法是覆盖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();
【讨论】:
【参考方案3】:假设您有一个名为 mygrid 的静态 normal Fragment,您的 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.
【讨论】:
【参考方案4】:在对话框片段中显示 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);
如果不是,它应该可以完成你的工作,请告诉我。
【讨论】:
以上是关于将 RecyclerView(RecyclerFragment) 添加到对话框的主要内容,如果未能解决你的问题,请参考以下文章
将水平 RecyclerView 作为项目放置在垂直 RecyclerView 中时的自动滚动问题
为什么我无法将数据从RecyclerView传递到另一个RecyclerView-AndroidX