gpt4 book ai didi

java - 在 alertDialog 中显示 Recyclerview

转载 作者:行者123 更新时间:2023-11-29 18:32:30 27 4
gpt4 key购买 nike

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();
}
});

我想像代码一样在 AlertDialog 中显示我的 Recyclerview,我很确定我已经关注了视频但是当我打开我的 alertDialog 时它总是强制关闭。你们能帮帮我吗?我是 android studio 新手

最佳答案

使用自定义布局对话框将自定义布局添加到对话框。尝试使用下面给出的代码。希望这将是一个有用的答案。

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();
  • 在 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>

关于java - 在 alertDialog 中显示 Recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55643947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com