带有RecyclerView的AlertDialog

9

我想在我的应用程序中创建一个自定义对话框,其中包含一些项目的列表。以下是我的适配器代码:

Context context; ArrayList statusList;

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文件顶部,但是我认为这里没有错误。有人能帮助我或分享一些关于这种活动的好教程吗?顺便说一下,可能只是小错误。))

@JosefAdamcik,那么你的意思是我应该将LayoutManager放入我的代码中吗? - Asset Bekbossynov
@V-rundPuro-hit 我之前尝试过,但没有帮助。 - Asset Bekbossynov
@AssetBekbossynov 我想说的是,即使你认为它是正确的,也应该将你的对话框布局xml添加到问题中。是的,尝试设置layoutmanager(已经有Ali的回复了)。 - Josef Adamcik
@JosefAdamcik 好的,请再检查一下我的问题。下次我会注意的。 - Asset Bekbossynov
你在哪里初始化了 maritalStatusList?而且,你的 RecyclerView 上也没有附加 LayoutManager - Reaz Murshed
显示剩余3条评论
2个回答

6
我认为您应该为recyclerView设置一个LayoutManager。 在设置适配器之前添加这一行代码:
rv.setLayoutManager(new LinearLayoutManager(getActivity()));

我之前考虑过,但不幸的是它没有起作用。 - Asset Bekbossynov

2

如果有人发现这篇文章有用,我的问题在于RecyclerView的高度为0dp,所以我将其更改为wrap_content。根据ConstraintLayout属性,我认为它应该会扩展到其父容器的大小。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接