我的问题是 Recyclerview 项目在底部表单对话框片段中时不可点击。
我试图在适配器的 onBindViewHolder 中获得一个 setOnClickListener,就像这样
holder.itemView.setOnClickListener{
Log.d("why", "1")
}
但它不工作。
更奇怪的是,它适用于公共活动或公共片段(不是底部SheetDialogFragment)。
这是我所有来自 Fragment(调用 bottomSheetFragment)、bottomSheetFragment、Adapter 的代码。
如果您需要 xml 代码或其他代码,请告诉我。
Fragment 代码
** 顺便说一下,BaseFragment(SignUpFragment 的类型)只包含 viewBinding 和 inflate
package com.softsquared.template.kotlin.src.login.sign_up
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.softsquared.template.kotlin.R
import com.softsquared.template.kotlin.config.BaseFragment
import com.softsquared.template.kotlin.databinding.FragmentSignUpBinding
import com.softsquared.template.kotlin.databinding.FragmentSignupShoesSizeBottomsheetBinding
import com.softsquared.template.kotlin.src.goods.GoodsBottomSheetFragment
import com.softsquared.template.kotlin.src.login.LoginActivity
import com.softsquared.template.kotlin.src.login.sign_up.models.ResponsePostSignUp
import com.softsquared.template.kotlin.src.main.MainActivity
class SignUpFragment : BaseFragment<FragmentSignUpBinding>(FragmentSignUpBinding::bind, R.layout.fragment_sign_up), SignUpFragmentView {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.signupSneakersSizeTv.setOnClickListener {
val bottomSheet = SignUpBottomSheetFragment()
bottomSheet.show(childFragmentManager, bottomSheet.tag)
}
}
// for Retrofit, I didn`t connect with recyclerview not yet
override fun onPostSignUpSuccess(response: ResponsePostSignUp) {
dismissLoadingDialog()
showCustomToast(response.message)
if(response.isSuccess){
fragmentManager?.beginTransaction()?.remove(this)?.commitAllowingStateLoss()
}
}
override fun onPostSignUpFailure(message: String) {
}
}
BottomSheetFragment 代码
package com.softsquared.template.kotlin.src.login.sign_up
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.softsquared.template.kotlin.databinding.FragmentSignupShoesSizeBottomsheetBinding
import com.softsquared.template.kotlin.src.goods.GoodsBottomSheetInformation
class SignUpBottomSheetFragment: BottomSheetDialogFragment() {
private lateinit var binding : FragmentSignupShoesSizeBottomsheetBinding
private var signupSizeInformation:MutableList<String> = mutableListOf()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentSignupShoesSizeBottomsheetBinding.inflate(inflater, container, false)
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
binding.goodsBottomSheetCloseBtn.setOnClickListener{
dialog?.dismiss()
}
getSignupSizeInformation()
binding.goodsBottomSheetSizeRecyclerview.layoutManager = GridLayoutManager(activity, 3)
binding.goodsBottomSheetSizeRecyclerview.adapter = SignUpBottomSheetAdapter(signupSizeInformation)
}
private fun getSignupSizeInformation(){
with(signupSizeInformation){
add("220")
add("225")
add("230")
add("235")
add("240")
add("245")
add("250")
add("255")
add("260")
add("265")
add("270")
add("275")
add("280")
add("285")
add("290")
add("295")
add("300")
}
}
}
适配器
package com.softsquared.template.kotlin.src.login.sign_up
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.softsquared.template.kotlin.databinding.FragmentSignupShoesSizeRecyclerviewItemsBinding
class SignUpBottomSheetAdapter (private val items: MutableList<String>): RecyclerView.Adapter<SignUpBottomSheetAdapter.Holder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val binding = FragmentSignupShoesSizeRecyclerviewItemsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return Holder(binding)
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder.text.text = items[position]
holder.itemView.setOnClickListener{
Log.d("why", "1")
}
}
override fun getItemCount(): Int {
return items.size
}
inner class Holder(val binding: FragmentSignupShoesSizeRecyclerviewItemsBinding):RecyclerView.ViewHolder(binding.root){
val container = binding.signupSizeCheckerBtn
val text = binding.signupSizeCheckerTv
}
}