Android Fragments 使用片段时按钮不起作用的OnClickcycle

svdrlsy4  于 2023-11-19  发布在  Android
关注(0)|答案(1)|浏览(108)

我有一个问题与按钮点击使用setOnClickButton
问题如下。我有一个由三个片段组成的菜单。从一个片段,你需要切换到数据输入布局。一个按钮用于转换。但逻辑上它不起作用。我不明白为什么。请帮助。
1.控制器至按钮

package com.example.client_ismpr_system

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.client_ismpr_system.adapters.equipment.EquipmentAdapter
import com.example.client_ismpr_system.models.Equipment

class MakingNewEquipmentFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val navButton: Button? = view?.findViewById(R.id.nav_button_to_new_order)
    navButton?.setOnClickListener(object : View.OnClickListener {
        override fun onClick(view: View) {
            Toast.makeText(requireContext(), "Button Clicked", Toast.LENGTH_SHORT).show()
            println("Test")
        }
    })
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    val view = inflater.inflate(R.layout.fragment_making_new_equipment, container, false)

    val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)

    val equipmentsList = listOf(
        Equipment(data), Equipment(data),  Equipment(data), Equipment(data),
    )

    val adapter = EquipmentAdapter(requireContext(), equipmentsList)

    recyclerView.adapter = adapter
    recyclerView.layoutManager = LinearLayoutManager(requireContext())

    return view
}

}

字符串

  1. XML中的按钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:background="@color/main_theme_color"
tools:context=".MakingNewEquipmentFragment">

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/recyclerView" />

<Button
    android:id="@+id/nav_button_to_new_order"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="12dp"
    android:layout_marginRight="32dp"
    android:layout_marginBottom="12dp"
    android:backgroundTint="#4CAF50"
    android:text="@string/text_making_new_order" />

</LinearLayout>


这里已经创建了Setonclicklistener处理程序,但它不起作用。此外,根据其他参与者的建议,我的View返回。但这些都不起作用。按钮不起作用。没有输出到LogCat。我希望按钮可以工作。

tag5nh1u

tag5nh1u1#

正如Fragment生命周期文档中所解释的,onCreate在**onCreateView之前运行,所以view在那里总是空的。
如果你想确保你的视图存在,你应该使用onViewCreated,它被传递给onCreateView返回的视图。
通过使用接受R.layout ID的Fragment构造函数,您实际上根本不需要重写onCreateView,因此您的Fragment应该如下所示:

class MakingNewEquipmentFragment : Fragment(R.layout.fragment_making_new_equipment) {

    override fun onViewCreated(
        view: View,
        savedInstanceState: Bundle?
    ) {
        val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)

        val equipmentsList = listOf(
            Equipment(data), Equipment(data),  Equipment(data), Equipment(data),
        )

        val adapter = EquipmentAdapter(requireContext(), equipmentsList)

        recyclerView.adapter = adapter
        recyclerView.layoutManager = LinearLayoutManager(requireContext())

        val navButton = view.findViewById<Button>(R.id.nav_button_to_new_order)
        navButton.setOnClickListener {
            Toast.makeText(requireContext(), "Button Clicked", Toast.LENGTH_SHORT).show()
            println("Test")
        }
    }
}

字符串

相关问题