【问题标题】:Why is my FloatingActionButton doesn't work on Fragment in Kotlin?为什么我的 FloatingActionButton 在 Kotlin 中的 Fragment 上不起作用?
【发布时间】:2021-06-20 08:44:47
【问题描述】:

我创建了一个 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

    }
}

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

【问题讨论】:

  • 绑定未在 onCreateView 中初始化

标签: kotlin floating-action-button android-viewbinding


【解决方案1】:

您应该使用 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>

【讨论】:

  • 如果可以的话,您应该避免使用DataBindingUtil.inflate,而是使用生成的绑定类(例如,FragmentDashboardBinding)。
  • 我用你的版本替换了我的onCreateView,我收到以下错误。我还添加了一个屏幕截图供您参考。如何解决? Logcat 不能使用提供的参数调用以下函数: public final val ViewDataBinding.root: View public final val FragmentDashboardBinding.root: ConstraintLayout
  • 我已经编辑了答案以包含布局标签。
  • 我的代码中已经有这些行,你能检查它是否与我的问题不同吗?
【解决方案2】:

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

    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
}

【讨论】:

    猜你喜欢
    • 2014-10-06
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    相关资源
    最近更新 更多