프로젝트에 BottomSheet를 사용하고 있는데, 레이아웃이 님이 구성한 것과 다르 긴 합니다만, 그렇게 복잡한 세팅을 필요없습니다. 제가 사용하는 레이아웃입니다.
BottomSheetFragemt를 호출하는 프레그먼트 쪽의 레이아웃:
<com.cmcmarketsstockbroking.android.ui.widget.scrollview.LockableNestedScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_fragment"
android:paddingTop="@dimen/spacing_4x"
android:paddingBottom="@dimen/spacing_4x">
//다른 커스텀뷰들이 여기에 들어갑니다.
</androidx.constraintlayout.widget.ConstraintLayout>
</com.cmcmarketsstockbroking.android.ui.widget.scrollview.LockableNestedScrollView>
LockableNestedScrollView은 NestScrollView와 같습니다. 내부적으로 변수값을 하나 사용해서 스크롤이 막아져 있는지 체크합니다.
BottomSheetFragment layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/headerTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif"
android:padding="@dimen/spacing_4x"
android:text="@string/exchange_selection_title"
android:textColor="@color/black80"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/containerLnl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_2x"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/headerTxt" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
BottomSheetFragment:
class ExchangeListBottomSheet : BottomSheetDialogFragment() {
companion object {
const val TAG = "ExchangeModalBottomSheet"
}
private val exchangeItems = arrayListOf<StockRowItem>()
override fun onAttach(context: Context) {
super.onAttach(context)
requireNotNull(targetFragment)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? =
inflater.inflate(R.layout.modal_bottom_sheet, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupArguments()
setupViews()
}
private fun setupArguments() {
val items =
requireNotNull(requireArguments().getParcelableArrayList<StockRowItem>(EXTRA_BUNDLE_KEY))
exchangeItems.clear()
exchangeItems.addAll(items)
}
private val exchangeItemClickListener by lazy {
SimpleItemClickListener<StockRowItem> { item ->
setResult(item)
dismiss()
}
}
private fun setupViews() {
containerLnl.removeAllViews()
exchangeItems.forEach {
containerLnl.addView(
ExchangeModalViewItem(requireContext()).apply {
setData(it)
itemClickListener = exchangeItemClickListener
}
)
}
}
private fun setResult(selectedItem: StockRowItem) {
val intent = Intent().apply {
putExtra(EXTRA_RESULT_KEY, selectedItem)
}
targetFragment?.onActivityResult(targetRequestCode, Activity.RESULT_OK, intent)
}
}