【问题标题】:BottomSheet not shown when use RecyclerView使用 RecyclerView 时未显示 BottomSheet
【发布时间】:2019-03-06 15:11:38
【问题描述】:

我有下面的底页代码

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

我的问题是物品数量少时显示底部表格,物品数量多时不显示,屏幕只是变暗,没有任何事情发生

【问题讨论】:

    标签: android kotlin android-recyclerview bottom-sheet


    【解决方案1】:

    由于我之前没有使用过它,我不确定这是唯一的解决方案。

    将您的 RecyclerView 高度更改为 wrap_content

    并将下一行添加到您的TextView

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

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

    它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2022-01-26
      相关资源
      最近更新 更多