setOnClickListener不在片段中工作

时间:2019-07-04 06:24:14

标签: android kotlin onclicklistener android-toolbar

我正在尝试在包含自定义工具栏的片段中设置onclicklistener,并且在工具栏中有一个响铃图标,我试图将onclicklistener放在上面但无法正常工作

这是工具栏custom_toolbar.xml

<androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:title="@string/app_name">

    <RelativeLayout
            android:id="@+id/notification_bell"
            ..>
        <ImageView
               ..>
        <ImageView
                ..>
        <TextView
                ..>
    </RelativeLayout>
</androidx.appcompat.widget.Toolbar>

这是fragment.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".landing.ui.fragment.HomeFragment">

    <include android:id="@+id/custom_toolbar"
             layout="@layout/custom_toolbar"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

然后在Fragment.kt

class HomeFragment : Fragment() {

    private fun initbell(notificationCount:Int) {

        custom_toolbar.notification_bell.setOnClickListener {
            Log.e("Fragment","bell clicked")
        }

    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        init()
        .........
    }

    private fun init() {
        initComponent()
        ..........
    }

    private fun initComponent() {
        initbell(it)
        ..........
        }

    }

}

单击铃铛后,我想执行一些操作。目前,我应该能够显示日志。 而且,我也能够访问它并更改其可见性,因此它不是启动的问题

5 个答案:

答案 0 :(得分:0)

备用:-

  

像这样在片段类中将相对布局作为标头

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".landing.ui.fragment.HomeFragment">

<RelativeLayout
    android:id="@+id/header_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   >


    <ImageView
           ..>
    <ImageView
            ..>
    <TextView
            ..>


</RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

,并且在fragment类的onCreate()

header_bar.setOnClickListener {
            Log.e("Fragment","bell clicked")
        }

答案 1 :(得分:0)

尝试一下, 向工具栏添加ID

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/tb_toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="@string/app_name">

<RelativeLayout
    android:id="@+id/notification_bell"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:background="@color/colorAccent" />

然后在片段内

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    (context as AppCompatActivity).setSupportActionBar(tb_toolbar)

    notification_bell.setOnClickListener {
        Toast.makeText(context, "Yeaey", Toast.LENGTH_LONG).show()
    }
}

答案 2 :(得分:0)

在xml中的工具栏标签中添加一个ID,然后在kotlin文件中添加以下行 onViewCreated方法

(上下文为AppCompatActivity).setSupportActionBar(your_toolbar_id)

    your_toolbar_id.notification_bell.setOnClickListener {
        Log.d("TAG", "Your Log message here")
    }

答案 3 :(得分:0)

如果要让RelativeLayout处理单击,则应向其中添加android:clickable属性:

<RelativeLayout
   android:id="@+id/notification_bell"
   android:clickable="true"
..>

这是因为RelativeLayout将允许从其传递touch事件,以便子视图可以处理该事件。

答案 4 :(得分:0)

所以我正在调查它,发现有一个小错误,我必须使用AppBar Layout才能真正解决问题,因为fragment.xml无法获取appbar布局,因此它无法识别点击。之后,它就像一个魅力

在fragment.xml中

.replace()
相关问题