android - 带有 RecyclerView 的 Activity 中的浮动操作按钮未收到 onClick
问题描述
浮动操作按钮上的点击操作不起作用。
我正在学习 android RecyclerView,我能够用数据填充活动。我在活动的底部也有 FloatingActionButton,它不响应触摸事件。尽管侦听器已附加到按钮,但当我点击它时没有任何反应。
活动活动.kt
class EventsActivity : AppCompatActivity() {
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_events)
setSupportActionBar(toolbar)
//MY FAB BUTTON IS ATTACHED WITH LISTENER HERE
addEvent.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
...
setupRecyclerAdapter()
}
}
活动事件.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".events.EventsActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<include android:id="@+id/content_events"
layout="@layout/content_events"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/addEvent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_input_add"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
解决方案
addEvent.setOnClickListener(this)
override fun onClick(v: View?)
{
when(v!!.id)
{
R.id. addEvent ->
{
Log.i("FABCLICK","Click on Fab icon")
}
}
}
推荐阅读
- html - 如何在 Jquery 中通过属性“0”减少像素
- google-cloud-firestore - Firestore 在一个字段上查询 whereGreater,在不同字段上查询 OrderBy
- javascript - 我在使用 v-bind 时遇到了一些问题,它不起作用
- javascript - 在单页应用程序中,我们应该在内存中缓存多少数据?
- node.js - 第二次尝试请求失败
- excel - 如何在 VBA 中获取标签的内部文本,不包括嵌套标签中的文本?
- apache-spark - 如何为火花结构化流指定kafka消费者的组ID?
- javascript - 为什么在 WebStorm 中看不到 addEventListener 方法的文档?
- django - django: how to include parent category URL in child category URL (Category is a nested model)
- popover - Filemaker Pro 弹出窗口显示一个列表,其中包含可供选择的值或可搜索的下拉列表