简体   繁体   中英

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. Can you guys help me? i'm new use 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 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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