Android 查看寻呼机滑动后,MVVM 片段按钮点击不起作用

Android MVVM Fragment Button click doesn't work after view pager swipe

我有一个 activity,其中包含三个使用 ViewPager 导航的片段。起始片段有一个带有点击事件的按钮。当片段首次出现时,按钮有效,但当我滑动到最后一个片段并返回主片段时,按钮不起作用。它只用按钮做这个,没有别的......我知道这可能是相当明显的,温柔的人,哈哈!

按钮布局

        <Button
            android:id="@+id/login_button"
            android:layout_width="match_parent"
            android:textSize="@dimen/body_text_size"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/layout_margin"
            android:layout_marginEnd="@dimen/layout_margin"
            android:layout_marginBottom="@dimen/layout_margin"
            android:focusable="false"
            android:background="@drawable/button_pressed_state"
            android:text="@string/login"
            android:textColor="@color/white"
            android:textStyle="bold" />

它发生的片段代码。

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            binding = DataBindingUtil.inflate(inflater, R.layout.login_fragment, container, false)
            viewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
            loginView = binding.root
            initClickableLinks()
            launch {
                initButtonClick()
            }
            return loginView
        }


        private suspend fun initButtonClick(): String? {
            val edittextSize = binding.emailAddressEditText.textSize
            val textviewSize = binding.forgetPassTextview.textSize
            var message: String? = ""
            binding.loginButton.setOnClickListener {
                fun onClick(view: View?) {
                    try {
                        viewModel.setEmailAddress(binding.emailAddressEditText.text.toString())
                        viewModel.setPassword(binding.passwordEditText.text.toString())
                        //if nothing is entered this will do nothing but update text
                        val invalidString = requireActivity().getString(R.string.invalid_combo)
                        binding.authTextView.text = ""
                        if (binding.emailAddressEditText.text.toString()
                                .isBlank() || binding.passwordEditText.text.toString().isBlank()
                        ) {
                            binding.authTextView.text = invalidString
                            //exits listener because authentication failed
    //                        return@setOnClickListener
                        }
                        binding.progressBar.visibility = View.VISIBLE
                        //background thread for IO
                        GlobalScope.launch(Dispatchers.IO) {
                            //call api

                            //UI Thread
                            withContext(Dispatchers.Main) {
                                val mess = viewModel.getMessage()
                                if (mess.equals("Successful")) {
                                    val intent = Intent(activity, MemberActivity::class.java)
                                    val loginfo = viewModel.getLoginResult().toString()
                                    intent.putExtra("loginIno", loginfo)
                                    activity?.startActivity(intent)
                                } else {
                                    binding.authTextView.text = mess
                                    Toast.makeText(activity!!.applicationContext, mess, Toast.LENGTH_LONG).show()
                                }
                                binding.progressBar.visibility = View.GONE
                            }
                        }
                    } catch (ex: Exception) {
                        println(ex)
                    }
                }
            }
            return message
        }

这只发生在按钮上

部分我发现以下仅被调用一次:

            initButtonClick()

删除启动并将私有挂起函数更改为仍然异步调用登录服务的私有函数解决了这个问题,现在被多次调用。