简体   繁体   English

片段中的 OnClickListener 不再起作用

[英]OnClickListener in fragment does not work anymore

I want to implement a little chat app and therefore i need a send button which clears the input field and sends the message (for now just adding an element to a recycler view).我想实现一个小聊天应用程序,因此我需要一个发送按钮来清除输入字段并发送消息(现在只向回收站视图添加一个元素)。 I've implemented the OnClickListener Interface and added my fragment as Listener to the button but somehow onClick is never called.我已经实现了 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()
    }
}

Thats the code from my Fragment and my layout looks like this这就是我的 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>

This should not be so difficult as this is a basic Android mechanic but I dont have a clue why this isnt working.这应该不难,因为这是一个基本的 Android 机制,但我不知道为什么这不起作用。 What am I missing?我错过了什么?

You should notify adapter about data changes:您应该通知适配器有关数据更改的信息:

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

Try setting your on click listener this way:尝试以这种方式设置您的点击监听器:

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

//...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM