android - 使用 RecyclerView 时未显示 BottomSheet

标签 android kotlin android-recyclerview bottom-sheet

我有下面的 Bottom Sheet 代码

xml文件

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/titleTextview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:fontFamily="sans-serif"
        android:text="@string/choose_option"
        android:textColor="@android:color/darker_gray"
        android:textSize="14sp"
        android:textStyle="normal"
        app:layout_constraintBottom_toTopOf="@id/recyclerView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/titleTextview" />

</androidx.constraintlayout.widget.ConstraintLayout>

查看

class AttributeOptionsBottomSheet(val viewModel: AttributeOptionsViewModel) : BottomSheetDialogFragment() {

    override fun getTheme(): Int = R.style.AppBottomSheetDialogTheme

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = inflater.inflate(R.layout.bottom_sheet_attributes, container, false)
        .also { view ->
            view.findViewById<RecyclerView>(R.id.recyclerView).apply {
                context?.let {
                    layoutManager = LinearLayoutManager(Activity())
                    adapter = AttributeOptionsAdapter(viewModel)
                    viewModel.dismissView.observe({ lifecycle }) {
                        dismiss()
                    }
                }
            }
        }
}

查看模型

class AttributeOptionsViewModel(attributeId: Int) : BaseViewModel() {

    val optionsDao: ExtraAttributeOptionDao by inject()
    var options : List<ExtraAttributeOptionEntity> = ArrayList<ExtraAttributeOptionEntity>()
    val dismissView = LiveEvent<Unit>()
    var optionSelected = LiveEvent<ExtraAttributeOptionEntity>()

    init {
        getOptions(attributeId)
    }

    fun getOptionName(position: Int): String {
        return options[position].name
    }

    fun setSelectedOption(position: Int) {
        options[position].apply {
            optionSelected.postValue(this)
            dismissView.postValue(Unit)
        }
    }

    fun getOptions(attributeId: Int) {
        launch(Dispatchers.IO) {
            options = optionsDao.getExtraAttributeOptionsBuAttributeId(attributeId)
        }
    }
}

我的问题是当项目数量少时 Bottom Sheet 显示,项目数量多时不显示,屏幕变暗,没有任何反应

最佳答案

因为我之前没有使用过它,所以我不确定这是唯一的解决方案。

RecyclerView 高度更改为 wrap_content

并将下一行添加到您的 TextView

app:layout_constraintVertical_bias="0"
app:layout_constraintVertical_chainStyle="packed"

或者从 TextView 中移除 bottom constraint 并将相同的 bias 添加到 RecyclerView

它对我有用。

关于android - 使用 RecyclerView 时未显示 BottomSheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55026327/

相关文章:

android - 如何解决 setBackgroundDrawable 被弃用的问题

java - 如何将ArrayList从AsyncTask返回到另一个类?

java - 为什么 Hibernate 延迟加载在 Kotlin 中表现不同?

android - 由 : java. lang.ClassCastException 引起 : androidx. fragment.app.FragmentContainerView 无法转换为 androidx.navigation.fragment.NavHostFragment

java - 拉动刷新 RecyclerVIew 适配器不工作

java - 单选按钮 If 语句

使用 XML 的 Android fragment ListView

sqlite - 无法使用JOOQ更新SQLITE中的表行

Android Recycler (List) View 展开转场动画

android - View Pager 的 fragment 隐藏在 Tab Bar 后面