简体   繁体   English

在alertDialog中显示Recyclerview

[英]Display Recyclerview inside alertDialog

imgTest.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    final AlertDialog.Builder mBuilder  =   new AlertDialog.Builder(getApplicationContext());
                                    mBuilder.setTitle("Location Available!");
                                    LayoutInflater inflater =   getLayoutInflater();
                                    View convertView        =   inflater.inflate(R.layout.reclycer_data, null);

                                    RecyclerView list       =   convertView.findViewById(R.id.recView);
                                    list.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                                    list.setAdapter(adapter);
                                    AlertDialog dialog      =   mBuilder.create();
                                    dialog.show();
                                }
                            });

I want to show my Recyclerview in AlertDialog like the code, I'm pretty sure i already follow in the videos but its always force close when i open my alertDialog. 我想像代码一样在AlertDialog中显示我的Recyclerview,我敢肯定我已经在视频中关注了,但是当我打开alertDialog时,它总是强制关闭。 Can you guys help me? 你们能帮我吗? i'm new use android studio 我是新使用的android studio

Use custom layout dialog to add your custom layout to dialog. 使用自定义布局对话框将自定义布局添加到对话框。 Try using below given code. 尝试使用下面给出的代码。 Hope this will be a helpful answer. 希望这将是一个有用的答案。

final Dialog dialog = new Dialog(getApplicationContext(), R.style.FullHeightDialog);

    dialog.setContentView(R.layout.reclycer_data);
    dialog.setCancelable(false);

    if (dialog.getWindow() != null){
        dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    }

    RecyclerView list = dialog.findViewById(R.id.recView);
    list.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    list.setAdapter(adapter);

dialog.show();
  • Define dialog style in style.xml 在style.xml中定义对话框样式

     <style name="FullHeightDialog" parent="Base.Theme.AppCompat.Light.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowFullscreen">true</item> </style> 

Dialog context must Activity 对话框上下文必须活动

    final AlertDialog.Builder mBuilder = new AlertDialog.Builder(YouActivity.this);
    mBuilder.setTitle("Location Available!");
    LayoutInflater inflater = getLayoutInflater();
    View convertView = inflater.inflate(R.layout.reclycer_data, null);

    RecyclerView list = convertView.findViewById(R.id.recView);
    list.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    list.setAdapter(adapter);
    mBuilder.setView(convertView); // setView

    AlertDialog dialog = mBuilder.create();
    dialog.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM