将 RecyclerView(RecyclerFragment) 添加到对话框
Add RecyclerView(RecyclerFragment) to a Dialog
我有自定义 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);
现在,我想在对话框中填充一个新列表。
我该怎么做?
我试过了,我的网格是静态的
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();
假设您有一个名为 mygrid 的静态 normal 片段,您的 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.
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;
}
}
在对话框片段中显示 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();
}
}
并从您的片段中调用它或 activity 您不需要任何容器 ID 只需调用这些行
AppDialogs appDialogs = AppDialogs.newInstance(0, title, msg);
appDialogs.setCancelable(false);
appDialogs.show(getFragmentManager(), null);
它应该完成你的工作,如果没有请告诉我。
已接受的答案有效,但需要额外努力才能使其更像标准对话。
下面是另一种可能的方法,它允许您保留所有对话框功能(例如标题、图标、positive/negative/neutral 按钮)。这个想法是覆盖 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();
}
}
我有自定义 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);
现在,我想在对话框中填充一个新列表。 我该怎么做?
我试过了,我的网格是静态的
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();
假设您有一个名为 mygrid 的静态 normal 片段,您的 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.
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;
}
}
在对话框片段中显示 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();
}
}
并从您的片段中调用它或 activity 您不需要任何容器 ID 只需调用这些行
AppDialogs appDialogs = AppDialogs.newInstance(0, title, msg);
appDialogs.setCancelable(false);
appDialogs.show(getFragmentManager(), null);
它应该完成你的工作,如果没有请告诉我。
已接受的答案有效,但需要额外努力才能使其更像标准对话。
下面是另一种可能的方法,它允许您保留所有对话框功能(例如标题、图标、positive/negative/neutral 按钮)。这个想法是覆盖 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();
}
}