Bottom Sheet Dialog中RecyclerView仅显示单个条目问题求助
BottomSheet中RecyclerView仅显示单个条目的问题及解决方法
我了解该问题此前已有提问,但现有解答未能帮到我,目前无法定位问题所在。我在一个Activity中展示Bottom Sheet,该Bottom Sheet包含标题和RecyclerView,但无论传入Adapter多少数据,RecyclerView始终仅显示一个条目,虽可滚动切换条目,但同一时间仅能看到一条。
Activity布局XML
<?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" tools:context=".features.order.automatedcheckout.view.AutomatedCheckoutQRCodeActivity" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/scanBarcodeInstruction" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="50dp" android:text="Scan the barcode to get started" android:textSize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <androidx.constraintlayout.widget.Guideline android:id="@+id/loadingGuideline" android:orientation="horizontal" app:layout_constraintGuide_percent="0.5" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ProgressBar android:id="@+id/progressIndicator" app:layout_constraintBottom_toTopOf="@+id/loadingGuideline" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/scanBarcodeInstruction" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/qrcodeImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/scanBarcodeInstruction"/> <TextView android:id="@+id/tenderName" app:layout_constraintTop_toBottomOf="@+id/qrcodeImageView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton android:id="@+id/changeTenderButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10dp" android:visibility="gone" android:layout_marginBottom="20dp" android:layout_marginRight="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" android:text = "Change Tender"/> </androidx.constraintlayout.widget.ConstraintLayout>
Bottom Sheet Fragment布局XML
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/bottomSheetLayout" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tenderTitle" android:layout_marginTop="20dp" android:layout_marginStart="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SELECT"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/tenderList" android:layout_width="match_parent" android:layout_height="wrap_content"/> </androidx.appcompat.widget.LinearLayoutCompat>
Bottom Sheet Fragment代码
class TenderBottomSheetFragment : BottomSheetDialogFragment() { private var _binding: FragmentTenderBottomSheetBinding? = null private val binding get() = _binding!! private lateinit var onTenderClicks:(tenderDetails: Pair<Int?, String?>) -> Unit lateinit var adapter: TenderAdapter override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { _binding = FragmentTenderBottomSheetBinding.inflate(inflater, container, false) adapter = TenderAdapter ( arguments?.parcelableArrayList(ARG_TENDERS)!!, onTenderClicks ) return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val layoutManager = LinearLayoutManager(requireContext()) binding.tenderList.layoutManager = layoutManager binding.tenderList.addItemDecoration(DividerItemDecoration(requireContext(), layoutManager.orientation)) binding.tenderList.adapter = adapter } companion object { fun newInstance( campusTenders: List<TenderKt>, onTenderClick: (tenderDetails: Pair<Int?, String?>) -> Unit ): TenderBottomSheetFragment { val tenderDialogFragment = TenderBottomSheetFragment() tenderDialogFragment.onTenderClicks = onTenderClick tenderDialogFragment.apply { arguments = Bundle().apply { putParcelableArrayList(ARG_TENDERS, ArrayList(tenders)) } } return tenderDialogFragment } } override fun onDestroyView() { super.onDestroyView() _binding = null } }
问题解决(更新内容)
原RecyclerView子项布局
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" xmlns:app="http://schemas.android.com/apk/res-auto"> <TextView android:id="@+id/campus_tender_name" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" tools:text ="Flex Account" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </androidx.constraintlayout.widget.ConstraintLayout>
修改后的子项布局
<TextView android:id="@+id/campus_tender_name" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:padding="10dp" tools:text = "Flex Account" android:textSize="16sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
问题根源在于RecyclerView子项的根布局使用了ConstraintLayout且设置android:layout_height="match_parent",导致每个子项都占据了RecyclerView的全部高度,因此同一时间只能看到一个条目。修改后子项高度自适应内容,即可正常显示所有条目。
内容的提问来源于stack exchange,提问作者DJ-DOO
相关产品推荐
相关产品推荐

