为什么我的 FloatingActionButton 在 Kotlin 中的 Fragment 上不起作用?

Why is my FloatingActionButton doesn't work on Fragment in Kotlin?

我创建了一个 FloatingActionButton 来在 fragment 中的 recyclerViewgridViewlistView 之间切换视图。我只是想确保 FloatingActionButton 正常工作,所以我只是添加了一个`toast。但是,当我按下按钮时,吐司没有出现。

fragment_dashboard.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"
    android:background="@color/colorOffWhite"
    tools:context=".ui.fragments.DashboardFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_dashboard_items"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fb_dashboard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="60dp"
        android:backgroundTint="@color/colorAccent"
        android:clickable="true"
        android:focusable="true"
        android:src="@drawable/ic_grid_list_view"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/tv_no_dashboard_items_found"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="@string/no_dashboard_item_found"
        android:textAlignment="center"
        android:textSize="@dimen/no_data_found_textSize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

DashboardFragment.kt

    class DashboardFragment : BaseFragment() {

    private var binding: FragmentDashboardBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding?.fbDashboard?.setOnClickListener { view ->

            Toast.makeText(requireContext(), "You clicked FA Button", Toast.LENGTH_LONG).show()

        }
        val root = inflater.inflate(R.layout.fragment_dashboard, container, false)

        return root

    }
}

当我使用第一个答案中的代码时出现以下错误。

您应该使用 DatabindingUtil inflate 分配绑定变量

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
       
        binding  = DataBindingUtil.inflate(
            inflater, R.layout.fragment_dashboard, container, false
        )
binding?.fbDashboard?.setOnClickListener { view ->

            Toast.makeText(requireContext(), "You clicked FA Button", Toast.LENGTH_LONG).show()

        }

return binding.root
}

此外,将布局文件 fragment_dashboard 包装在布局标记中。

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
      >
</layout>

这就是我使用的并解决了问题。

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

    binding = FragmentDashboardBinding.inflate(inflater, container, false)

    binding.fbDashboard.setOnClickListener {

        Toast.makeText(requireContext(), "You clicked FA Button", Toast.LENGTH_LONG).show()

    }
    return binding.root
}