【问题标题】:How we can use bottomSheet in fragment for RecyclerView in android?我们如何在 android 的 RecyclerView 片段中使用 bottomSheet?
【发布时间】:2022-01-15 20:05:04
【问题描述】:

我有一个简单的项目,我想在片段中使用底部表作为列表项,一切看起来都不错,但是在构建项目后,应用程序崩溃了,

row_list.setOnLongClickListener 的空对象引用

我不知道问题出在哪里以及如何解决这个问题,也许我的方法不正确,任何想法将不胜感激。

fragment_list:

<FrameLayout
        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:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:fitsSystemWindows="true"
        tools:context=".view.ListFragment">


    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/row_list"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="60dp">
    </androidx.recyclerview.widget.RecyclerView>

   </FrameLayout>

列表片段:

class ListFragment : Fragment() {

    override fun onCreate(@Nullable savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        row_list.setOnLongClickListener {

            val bottomSheetFragment: BottomSheetDialogFragment = RowItemMenuFragment()
            bottomSheetFragment.show(requireFragmentManager(), bottomSheetFragment.tag)

            true
        }}
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view = inflater.inflate(R.layout.fragment_list, container, false)

        return view

    }}}

RowItemMenuFragment:

class RowItemMenuFragment() : BottomSheetDialogFragment()  {


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_row_item_menu, container, false)

    }

    override fun onCreate(@Nullable savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.BottomSheetDialogTheme)

    }

  }

【问题讨论】:

    标签: android kotlin android-bottomsheetdialog


    【解决方案1】:

    onCreate() 期间,视图未完全初始化或处于模棱两可的状态。

    简单的答案是将视图逻辑移动到onViewCreated()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            row_list.setOnLongClickListener {
    
                val bottomSheetFragment: BottomSheetDialogFragment = RowItemMenuFragment()
                bottomSheetFragment.show(requireFragmentManager(), bottomSheetFragment.tag)
    
                true
            }
    }
    

    【讨论】:

    • tnx 这么好用,但是当我长按列表项时,底部工作表不起作用,知道吗?
    • 尝试使用supportFragmentManager
    • 我用它是可行的,但同样的问题,row_list 是回收站视图的 id,我不确定我需要哪个 id,是否可以使用 row item id?
    • recyclerView 上添加onClickListner,参见this 和impl here
    • tnx 这么多,但我不明白如何在片段中实现适配器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多