gpt4 book ai didi

android - 使用 RecyclerView 时未显示 BottomSheet

转载 作者:行者123 更新时间:2023-11-29 23:17:43 29 4
gpt4 key购买 nike

我有下面的 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com