onCreateDialog 中带有 RecyclerView 的 DialogFragment 问题
DialogFragment with RecyclerView issue in onCreateDialog
我需要在我的 DialogFragment positive/negative 按钮中添加。
这是没有这些按钮的对话框:
这是我用来实现它的代码:
public class RecyclerColorsDialogFragment extends DialogFragment
{
RecyclerView recyclerView;
RecyclerColorsDialogAdapter adapter;
ArrayList<Boolean>colorChecked;
ArrayList<String>items;
ArrayList<String>colors;
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog dialog = new Dialog(getActivity());
dialog.setCancelable(true);
return dialog;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_recycler_colors_dialog, container, false);
colorChecked = new ArrayList<>(12);
colors = new ArrayList<>(12);
String[] cItems = {getString(R.string.red),getString(R.string.green), getString(R.string.blue),getString(R.string.yellow),
getString(R.string.azur),getString(R.string.black),getString(R.string.white),getString(R.string.gray),getString(R.string.brown),
getString(R.string.pink),getString(R.string.purple)};
items = new ArrayList<>(Arrays.asList(cItems));
//inizializzo i check a false
for (int i = 0; i< items.size(); i++)
colorChecked.add(true);
recyclerView = (RecyclerView)rootView.findViewById(R.id.recColors);
adapter = new RecyclerColorsDialogAdapter(getActivity(),colors,colorChecked,items);
final Dialog dialog = getDialog();
dialog.setCancelable(true);
dialog.setTitle("Add a picture to your aircraft:");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
recyclerView.setAdapter(adapter);
return rootView;
}
@Override
public void onCancel(DialogInterface dialog)
{
super.onCancel(dialog);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
dialog.cancel();
}
}
而且是来自其他片段的调用:
final RecyclerColorsDialogFragment dialog = new RecyclerColorsDialogFragment();
dialog.setTargetFragment(AddAicraftFivePartFragment.this, 2);
logo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = getActivity().getSupportFragmentManager();
dialog.show(manager, "PATAG");
我在 onCreateDialog()
中尝试这样做时遇到问题:
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Dialog dialog = new Dialog(getActivity());
// dialog.setCancelable(true);
return new AlertDialog.Builder(getActivity())
.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do something...
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
).create();
// return dialog;
}
因为它显示了它:
没有 recyclerview 与项目。
我该如何解决?
谢谢
将代码从 onCreate 放到 onCreateDialog 中。然后将 rootView 设置为对话框视图。并删除 onCreate() 方法
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(getActivity()).
setTitle("Add a picture to your aircraft:").setMessage(mMessage).
setPositiveButton(getString(R.string.discard), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getActivity().finish();
}
}).setNegativeButton(getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
Inflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.fragment_recycler_colors_dialog, container, false);
colorChecked = new ArrayList<>(12);
colors = new ArrayList<>(12);
String[] cItems = {getString(R.string.red), getString(R.string.green), getString(R.string.blue), getString(R.string.yellow),
getString(R.string.azur), getString(R.string.black), getString(R.string.white), getString(R.string.gray), getString(R.string.brown),
getString(R.string.pink), getString(R.string.purple)};
items = new ArrayList<>(Arrays.asList(cItems));
//inizializzo i check a false
for (int i = 0; i < items.size(); i++)
colorChecked.add(true);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recColors);
adapter = new RecyclerColorsDialogAdapter(getActivity(), colors, colorChecked, items);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
recyclerView.setAdapter(adapter);
dialog.setView(rootView)
return dialog.create();
}
我需要在我的 DialogFragment positive/negative 按钮中添加。
这是没有这些按钮的对话框:
这是我用来实现它的代码:
public class RecyclerColorsDialogFragment extends DialogFragment
{
RecyclerView recyclerView;
RecyclerColorsDialogAdapter adapter;
ArrayList<Boolean>colorChecked;
ArrayList<String>items;
ArrayList<String>colors;
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog dialog = new Dialog(getActivity());
dialog.setCancelable(true);
return dialog;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_recycler_colors_dialog, container, false);
colorChecked = new ArrayList<>(12);
colors = new ArrayList<>(12);
String[] cItems = {getString(R.string.red),getString(R.string.green), getString(R.string.blue),getString(R.string.yellow),
getString(R.string.azur),getString(R.string.black),getString(R.string.white),getString(R.string.gray),getString(R.string.brown),
getString(R.string.pink),getString(R.string.purple)};
items = new ArrayList<>(Arrays.asList(cItems));
//inizializzo i check a false
for (int i = 0; i< items.size(); i++)
colorChecked.add(true);
recyclerView = (RecyclerView)rootView.findViewById(R.id.recColors);
adapter = new RecyclerColorsDialogAdapter(getActivity(),colors,colorChecked,items);
final Dialog dialog = getDialog();
dialog.setCancelable(true);
dialog.setTitle("Add a picture to your aircraft:");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
recyclerView.setAdapter(adapter);
return rootView;
}
@Override
public void onCancel(DialogInterface dialog)
{
super.onCancel(dialog);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
dialog.cancel();
}
}
而且是来自其他片段的调用:
final RecyclerColorsDialogFragment dialog = new RecyclerColorsDialogFragment();
dialog.setTargetFragment(AddAicraftFivePartFragment.this, 2);
logo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = getActivity().getSupportFragmentManager();
dialog.show(manager, "PATAG");
我在 onCreateDialog()
中尝试这样做时遇到问题:
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Dialog dialog = new Dialog(getActivity());
// dialog.setCancelable(true);
return new AlertDialog.Builder(getActivity())
.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do something...
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
).create();
// return dialog;
}
因为它显示了它:
没有 recyclerview 与项目。
我该如何解决?
谢谢
将代码从 onCreate 放到 onCreateDialog 中。然后将 rootView 设置为对话框视图。并删除 onCreate() 方法
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(getActivity()).
setTitle("Add a picture to your aircraft:").setMessage(mMessage).
setPositiveButton(getString(R.string.discard), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getActivity().finish();
}
}).setNegativeButton(getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
Inflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.fragment_recycler_colors_dialog, container, false);
colorChecked = new ArrayList<>(12);
colors = new ArrayList<>(12);
String[] cItems = {getString(R.string.red), getString(R.string.green), getString(R.string.blue), getString(R.string.yellow),
getString(R.string.azur), getString(R.string.black), getString(R.string.white), getString(R.string.gray), getString(R.string.brown),
getString(R.string.pink), getString(R.string.purple)};
items = new ArrayList<>(Arrays.asList(cItems));
//inizializzo i check a false
for (int i = 0; i < items.size(); i++)
colorChecked.add(true);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recColors);
adapter = new RecyclerColorsDialogAdapter(getActivity(), colors, colorChecked, items);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
recyclerView.setAdapter(adapter);
dialog.setView(rootView)
return dialog.create();
}