Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将RecyclerView(RecyclerFragment)添加到对话框_Android_Android Fragments_Android Alertdialog_Android Recyclerview - Fatal编程技术网

Android 将RecyclerView(RecyclerFragment)添加到对话框

Android 将RecyclerView(RecyclerFragment)添加到对话框,android,android-fragments,android-alertdialog,android-recyclerview,Android,Android Fragments,Android Alertdialog,Android Recyclerview,我有自定义的RecyclerView来创建ListView。 当我试图在布局的id中填充列表视图时,它非常有效 FragmentTransaction ft = getFragmentManager().beginTransaction(); Bundle bundle = new Bundle(); bundle.putBoolean("enablePullToRefresh", false); GridValues gridValues = new GridValues(); gridVal

我有自定义的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只是另一个片段,像处理任何其他片段一样膨胀自定义视图

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();
    }
}
公共类AppDialogs扩展DialogFragment{
私有AlertDialog.Builder;
公共静态AppDialogs newInstance(int dialogNo、字符串标题、字符串消息)
{
AppDialogs片段=新建AppDialogs();
Bundle args=新Bundle();
args.putInt(“dialogNo”,dialogNo);
args.putString(“title”,title);
args.putString(“msg”,msg);
fragment.setArguments(args);
返回片段;
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态)
{

if(android.os.Build.VERSION.SDK_INT接受的答案是可行的,但它需要额外的努力才能让它更像标准对话框

下面是另一种可能的方法,它允许您保留所有对话框功能(例如标题、图标、正/负/中性按钮)。其思想是覆盖
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();
    }
}

只需应用与普通
片段应用相同的逻辑,为什么不使用AlertDialog.Builder方法?在那里可以调用addView()@Blackbelt,我尝试过使用FragmentTransaction,但不幸的是它不起作用。我不太了解核心概念,我还是一名学习者。请发布什么是
CustomGridView
@bGorle只是一个RecyclerView,InSpred(!)from.。嗨,当我添加recycler视图并启用滚动条时,滚动条不可见(即使在XML中设置了scrollbars=vertical,我也尝试了设置drawable)。我认为这可能与主题有关。你知道为什么吗?