我有以下底部代码
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)
}
}
}
我的问题是当有少量商品时显示底部工作表,并且它没有显示大量商品,屏幕只是变暗而且没有发生任何事情
因为我还没有使用它之前我不确定这是唯一的解决方案。
将你的RecyclerView
身高改为wrap_content
并将下一行添加到TextView
app:layout_constraintVertical_bias="0"
app:layout_constraintVertical_chainStyle="packed"
或者从bottom constraint
中移除TextView
并将相同的bias
添加到RecyclerView
它对我有用。