我们如何在 android 的 RecyclerView 片段中使用 bottomSheet?

Posted

技术标签:

【中文标题】我们如何在 android 的 RecyclerView 片段中使用 bottomSheet?【英文标题】:How we can use bottomSheet in fragment for RecyclerView in android? 【发布时间】: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_
        android:layout_
        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_
            android:layout_
            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)

    

  

【问题讨论】:

【参考方案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 这么多,但我不明白如何在片段中实现适配器

以上是关于我们如何在 android 的 RecyclerView 片段中使用 bottomSheet?的主要内容,如果未能解决你的问题,请参考以下文章

Android 自定义View:时间轴效果实现

ViewPager 系列之 打造一个通用的 ViewPager

ViewPager 系列之 打造一个通用的 ViewPager

回收站视图仅在再次启动活动后才显示更新列表

我们如何在 Android 中使用 runOnUiThread?

我们如何在服务器端验证 Android 应用内计费收据?