【问题标题】:RecyclerView inside Dialog is truncated bottomDialog里面的RecyclerView被截断底部
【发布时间】:2020-04-27 01:51:32
【问题描述】:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/popupCategoriesLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list_categories"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nestedScrollingEnabled="true"
        android:paddingTop="8dp"
        android:scrollbars="vertical"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/popu_category_info"

        />

    <ImageView
        android:id="@+id/imageViewPopupCategoryInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="@+id/popu_category_info"
        app:layout_constraintEnd_toStartOf="@+id/popu_category_info"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_info_outline_orange_24dp" />

    <TextView
        android:id="@+id/popu_category_info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="48dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@color/popup_warning_category"
        android:paddingStart="8dp"
        android:text="@string/warning_category_info"
        android:textColor="@color/white"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

所以我想要在对话框顶部的信息标签 + 可滚动列表。显示对话框的代码:

CategoryGroupAdapter gropupAdapter = new CategoryGroupAdapter(categoryGroups);


        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

        View convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_category, null);

        RecyclerView list = convertView.findViewById(R.id.list_categories);
        list.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        list.setHasFixedSize(false);

        list.setAdapter(gropupAdapter);

        gropupAdapter.collapseAll();

        AlertDialog d = alertDialog.setView(convertView).setCancelable(true).setTitle(getString(R.string.category_dialog_title)).show();

因此,如果整个列表没有空间显示所有项目,我将在底部截断列表。似乎它被截断了信息标签高度(但我无法设置固定的信息标签高度,因为它需要在具有各种 dpi 的所有设备上显示所有文本)。

任何想法如何正确显示我的对话框?

截图:

【问题讨论】:

  • 请您发布实现的屏幕截图以改进您的问题。
  • 添加截图
  • 对话框布局文件的根ViewGroup是什么?
  • 我更正了我的帖子,我明白你在问什么,它是ConstraintLayout

标签: java android layout android-recyclerview android-dialog


【解决方案1】:

创建一个自定义对话框扩展对话框片段,并通过构造函数传递所需的数据,并根据您的需要在对话框 UI 上设置它们。

    public class AlertDialogFragment extends DialogFragment {
    Context context;

    public AlertDialogFragment(){
    }

    //Create a multiple constructor on your need
    onStart(){
        //Set your logic if any like button color or UI on flag
    }

     @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.customalertstyle)
                // set dialog icon
                // set Dialog Title
                .setTitle(title)
                // Set Dialog Message
                .setItems(items, itemClickListner)
                .setMessage(msg)
                // positive button
                .setCancelable(false)
                .setPositiveButton(positiveBtnText, positiveClickListner)
                .setOnKeyListener(keyListener)
                .setNegativeButton(negativeBtnText, negativeClickListner);

    }


     @Override
    public void show(FragmentManager manager, String tag) {
        //to catch the exception while app is in background and dialog
        try {
            super.show(manager, tag);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

     @Override
    public void dismiss() {
        super.dismiss();
    }

}

不要忘记使用支持对话框片段,因为另一个已弃用。

【讨论】:

  • 什么是setItems?我需要我的自定义布局和自定义回收站视图。您将在哪里设置对话框布局?
  • @user1209216 setItem 方法由两个参数组成,分别是 item 和 click listener,其中 items 是一些可以作为数组传递的值,click listener 是可以传递的事件,否则不要如果需要,请使用它们
  • 好吧,但据我了解,您的方法没有任何改变。您只将一些样板文件打包到自定义类中。我的问题是对话框布局,它以某种我不理解的方式缩放。这不是第一次 Android 对话框让我感到困惑
  • @user1209216 然后尝试根据需要自动调整文本大小
  • 我不知道如何扩展它。如果放在 Activity 中,相同的布局也可以正常工作。你有什么建议吗?据我所知,我的代码中的所有内容都正确缩放。
猜你喜欢
  • 2011-01-28
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 1970-01-01
相关资源
最近更新 更多