【发布时间】:2021-06-20 08:44:47
【问题描述】:
我创建了一个 FloatingActionButton 来在 fragment 中的 recyclerView 的 gridView 和 listView 之间切换视图。我只是想确保 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