Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
667 views
in Technique[技术] by (71.8m points)

android - RecyclerView Scrolling not work proper with BottomSheetDialogFragment

enter image description here

I added one UserListFragment inside BottomSheetDialogFragment all looks good data is displayed in list but problem is RecyclerView content not scroll properly. Below my code is given

 val sheet = ListBottomSheetFragment()
                sheet.show(supportFragmentManager,"tag_bsdf")

BottomSheetDialogFragment

class ListBottomSheetFragment : BottomSheetDialogFragment() {
private var behaviour: BottomSheetBehavior<*>? = null

val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback() {
    override fun onSlide(bottomSheet: View, slideOffset: Float) {

    }

    override fun onStateChanged(bottomSheet: View, newState: Int) {
        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
            dialog?.dismiss()
        }
    }

}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val d: BottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog

    d.setOnShowListener {
        val d = dialog as BottomSheetDialog

        val bottomSheet: FrameLayout? = d.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
      
       
        bottomSheet?.let {
            behaviour = BottomSheetBehavior.from<View?>(bottomSheet)
            behaviour?.addBottomSheetCallback(bottomSheetCallback)
            behaviour?.state = BottomSheetBehavior.STATE_EXPANDED
        }
    }
    return d;
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_bottom_sheet_dialog, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
   switchFragment(UserListFragment(),"tag_user_list")

}

private fun switchFragment(fragment: Fragment, tag: String) {
    val transaction = childFragmentManager.beginTransaction()
        transaction.add(R.id.container, fragment, tag)
    transaction.commit()
 
}

companion object {
    fun newInstance(): ListBottomSheetFragment {
        return ListBottomSheetFragment()
    }
}

fragment_bottom_sheet_dialog.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tabLayout" />
</LinearLayout>

UserListFragment

 class UserListFragment : BaseFragment() {
    private var adapter: UserListAdapter? = null
    private var data = mutableListOf<UserModel>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_user_list, container, false)
        return view
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            recycler_view.layoutManager = LinearLayoutManager(requireContent())

           data.addAll(Pref.getSavedUserList())

            adapter = UserListAdapter(requireContext(), data)
            recycler_view.adapter = adapter           
}
    }

fragment_user_list.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_sticker"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
question from:https://stackoverflow.com/questions/65857440/recyclerview-scrolling-not-work-proper-with-bottomsheetdialogfragment

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...