【问题标题】:OnClickListener in fragment does not work anymore片段中的 OnClickListener 不再起作用
【发布时间】:2020-04-03 13:10:27
【问题描述】:

我想实现一个小聊天应用程序,因此我需要一个发送按钮来清除输入字段并发送消息(现在只是向回收站视图添加一个元素)。我已经实现了 OnClickListener 接口并将我的片段作为侦听器添加到按钮,但不知何故永远不会调用 onClick。

class MainFragment : Fragment(), View.OnClickListener  {

    private val messageList = ArrayList<MessageData>()
    val interactor = MainInteractor()

    private lateinit var viewModel: MainViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        initRecycler()
        InputButton.setOnClickListener(this)
        return inflater.inflate(R.layout.main_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
    }

    private fun initRecycler(){
        messageList.add(MessageData("Das ist die erste Nachricht", 0))
        messageList.add(MessageData("Das ist die zweite Nachricht", 1))
        messageList.add(MessageData("Das ist die dritte Nachricht,\nwelche über 2 Zeilen geht", 0))

        val recyclerView: RecyclerView = recycler
        val manager: RecyclerView.LayoutManager = LinearLayoutManager(requireActivity())
        val adapter: RecyclerView.Adapter<*> = MessageAdapter(messageList)

        recyclerView.layoutManager = manager
        recyclerView.adapter = adapter
    }

    private fun sendMessage(){
        messageList.add(MessageData(input_field.text.toString(), 0))
        input_field.setText("")
    }

    override fun onClick(v: View?) {
        Toast.makeText(context,"Button Pressed",Toast.LENGTH_SHORT).show()
        sendMessage()
    }
}

这是我的 Fragment 中的代码,我的布局看起来像这样

<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gray"
    tools:context=".ui.main.MainFragment">

    <TextView
        android:id="@+id/ueberschrift"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Chat"
        android:textColor="@color/yellow"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@color/yellow"

        app:layout_constraintTop_toBottomOf="@+id/ueberschrift"
        tools:layout_editor_absoluteX="1dp" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:scrollbars="vertical"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        app:layout_constraintTop_toBottomOf="@+id/divider"
        app:layout_constraintBottom_toTopOf="@+id/textInputLayout">

    </androidx.recyclerview.widget.RecyclerView>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginBottom="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/InputButton"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_editor_absoluteX="1dp"
        android:textColorHint="#FFFFFF">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/input_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:textColor="#FFFFFF"
            android:hint="Your Message"

            />
    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/InputButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="10dp"
        android:text="Send"
        android:textColor="#2E2E2E"
        android:background = "@color/yellow"
        app:layout_constraintStart_toEndOf="@id/textInputLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/textInputLayout"
        app:layout_constraintBottom_toBottomOf="@id/textInputLayout"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

这应该不难,因为这是一个基本的 Android 机制,但我不知道为什么这不起作用。我错过了什么?

【问题讨论】:

  • 我可能已经找出导致此问题的原因。为了解决另一个问题,我调用了 ``` setContentView(R.layout.main_fragment) ``` 而不是 R.layout.main_activity,所以我在尝试调用虚拟方法时不会收到 Nullpointer 异常。因此,不会调用 onCreateView 并且不会设置 ButtonListener。

标签: java android xml button kotlin


【解决方案1】:

尝试以这种方式设置您的点击监听器:

class MainFragment : Fragment(R.layout.main_fragment) {

//...

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        InputButton.setOnClickListener {
            InputButton.setOnClickListener(this)
            sendMessage()
        }
    }
}

【讨论】:

  • 这对我不起作用,但我想我找到了导致这种情况的原因。见下文。
  • 修复我的 setContentView 问题后,您的实现工作正常。谢谢。
  • @LukasSchüler 您可以通过避免覆盖onCreateView() 来保持它的美观和整洁。只需更改从class MainFragment :Fragment() 延伸的行。而是做class MainFragment : Fragment(R.layout.main_fragment)
【解决方案2】:

您应该通知适配器有关数据更改:

private fun sendMessage(){
    messageList.add(MessageData(input_field.text.toString(), 0))
    recycler.adapter.notifyDataSetChanged()
    input_field.setText("")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 2023-03-30
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    相关资源
    最近更新 更多