android - 带有 RecyclerView 的 AlertDialog
问题描述
我想在我的应用程序中创建包含一些项目列表的自定义对话框。这是我的适配器代码
上下文上下文;数组列表状态列表;
public MaritalStatusAdapter(Context context, ArrayList<String> statusList){
this.context = context;
this.statusList = statusList;
Logger.msg("Reg", ":" + statusList.get(0));
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.spinner_list_item, parent, false);
Logger.msg("Reg", "here");
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.item.setText(statusList.get(position));
Logger.msg("Reg", "here");
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return statusList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.item)
TextView item;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
Logger.msg("Reg", "heee555y");
}
@OnClick(R.id.item)
public void onClick(){
Logger.msg("Reg", "heeey");
}
}
我认为这里所有的代码都很好,它应该可以按我的意愿工作。这是我的对话框生成器代码
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = getLayoutInflater();
View dialogView = (View) inflater.inflate(R.layout.custom_alert_dialog, null);
builder.setView(dialogView);
RecyclerView rv = (RecyclerView) dialogView.findViewById(R.id.rv);
MaritalStatusAdapter adapter = new MaritalStatusAdapter(getActivity(), maritalStatusList);
rv.setAdapter(adapter);
AlertDialog dialog = builder.create();
dialog.show();
因此,这段代码向我显示了我的自定义对话框视图和我的编辑文本,该文本已添加到 xml 文件中,但在 recyclerView 中不显示任何内容。我确定我的 xml 文件没问题。但是我不明白为什么我在我的列表中看不到我的项目。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@null"
android:drawableLeft="@drawable/search"
android:drawablePadding="10dp"
android:hint="@string/search"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="@color/vertical_line"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/search" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view1" />
</android.support.constraint.ConstraintLayout>
在顶部我的 custom_alert_dialog.xml 文件,但我怎么说我认为这里没有错误
有人可以帮助我或分享一些关于此类活动的好教程。顺便说一句,这可能是一个小错误。))
解决方案
我认为你应该为你的 recyclerView 设置一个 LayoutManager。在设置适配器之前添加这一行:
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
推荐阅读
- java - 我应该在 Intellij 上“导入更改”后启动“全新安装”吗
- javascript - 有没有一种方法可以在多行中随机编号?
- php - 如何从现有且复杂的 Magento2 项目在本地开发环境中运行
- kotlin - 变量赋值时的 Kotlin“return”语句
- python - 在 python 中创建 JSON 模型
- python-3.x - 为什么 beautifulsoup4 没有导入我的脚本?
- java - 我不知道如何解决以下问题:无法推断 MyIterator<> 的类型参数
- typescript - 打字稿函数,其中对象具有由另一个参数确定的字符串类型属性
- forms - 检索复选框输入(Groovy Html MarkupBuilder + SpringBoot)
- php - php 为 RDS Mysql 实例抛出 [caching_sha2_password] 身份验证错误,即使身份验证方法是 mysql_native_password