android 在片段中未调用onItemClick

eqfvzcg8  于 2024-01-04  发布在  Android
关注(0)|答案(2)|浏览(200)

下面是我的Fragment1.kt整体代码

  1. class Fragment1 : Fragment() {
  2. private lateinit var binding: Fragment1Binding
  3. private lateinit var list : ArrayList<TasksItem>
  4. private lateinit var recyclerView: RecyclerView
  5. private lateinit var manager: RecyclerView.LayoutManager
  6. private lateinit var myAdapter: RecyclerView.Adapter<*>
  7. override fun onCreateView(
  8. inflater: LayoutInflater, container: ViewGroup?,
  9. savedInstanceState: Bundle?
  10. ): View? {
  11. // Inflate the layout for this fragment
  12. return inflater.inflate(R.layout.fragment_1, container, false)
  13. }
  14. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  15. super.onViewCreated(view, savedInstanceState)
  16. recyclerView = view.findViewById(R.id.app_list)
  17. recyclerView.setHasFixedSize(true)
  18. list = arrayListOf()
  19. manager = LinearLayoutManager(this.requireContext())
  20. GroomedKidzApi.retrofitService.getTasksProgress().enqueue(object :
  21. Callback<List<TasksItem>?> {
  22. override fun onResponse(
  23. call: Call<List<TasksItem>?>,
  24. response: Response<List<TasksItem>?>
  25. ) {
  26. val responseBody = response.body()
  27. if (responseBody != null) {
  28. recyclerView = view.findViewById<RecyclerView>(R.id.app_list).apply {
  29. myAdapter = TaskAdapter(response.body()!!)
  30. layoutManager = manager
  31. adapter = myAdapter
  32. Log.e("TAG", "response 33: "+ (response.body()) )
  33. }
  34. }
  35. }
  36. override fun onFailure(call: Call<List<TasksItem>?>, t: Throwable) {
  37. }
  38. })
  39. var adapter = TaskAdapter(list)
  40. recyclerView.adapter = adapter
  41. adapter.setOnItemClickListener(object : TaskAdapter.OnItemClickListener {
  42. override fun onItemClick(position: Int) {
  43. val intent = Intent(activity,ViewTaskActivity::class.java).apply{
  44. putExtra("taskname", list[position].taskName)
  45. putExtra("desc", list[position].taskDesc)
  46. putExtra("category", list[position].category)
  47. putExtra("duedate", list[position].dueDate)
  48. putExtra("assignee", list[position].assignee)
  49. putExtra("point", list[position].pointEarned)
  50. Log.d("Fragment1", "onViewCreated executed")
  51. }
  52. startActivity(intent)
  53. }
  54. })
  55. }
  56. }

字符串
下面是TaskAdapter的整体代码

  1. class TaskAdapter(val list: List<TasksItem> ) : RecyclerView.Adapter<TaskAdapter.MyViewHolder>() {
  2. private var listener2: OnItemClickListener? = null
  3. interface OnItemClickListener {
  4. fun onItemClick(position: Int)
  5. }
  6. fun setOnItemClickListener(listener: OnItemClickListener){
  7. listener2 = listener
  8. }
  9. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
  10. val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item,
  11. parent,false)
  12. return MyViewHolder(itemView,listener2)
  13. }
  14. override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
  15. val currentitem = list[position]
  16. holder.TaskName.text = currentitem.taskName
  17. holder.Date.text = currentitem.dueDate
  18. holder.Assignee.text = currentitem.assignee
  19. holder.Status.text = currentitem.taskStatus
  20. }
  21. override fun getItemCount(): Int {
  22. return list.size
  23. }
  24. class MyViewHolder(itemView : View, listener: OnItemClickListener?) : RecyclerView.ViewHolder(itemView){
  25. val TaskName : TextView = itemView.findViewById(R.id.tvTaskname)
  26. val Assignee : TextView = itemView.findViewById(R.id.tvAssignee)
  27. val Date : TextView = itemView.findViewById(R.id.tvDate)
  28. val Status : TextView = itemView.findViewById(R.id.tvStatus)
  29. init {
  30. itemView.setOnClickListener{
  31. listener?.onItemClick(adapterPosition)
  32. }
  33. }
  34. }
  35. }


onItemClick内部的Log.d("Fragment1", "onViewCreated executed")的logcat没有被调用。但是,可以成功调用其他函数。
为什么onItemClick没有被调用?所有函数都工作正常,即使是API。
注:适配器很好,setOnClickListener也能得到位置

vshtjzan

vshtjzan1#

看起来你的TaskAdapter中的setOnClickList实现可能有问题。

  1. class TaskAdapter(val list: List<TasksItem>) : RecyclerView.Adapter<TaskAdapter.MyViewHolder>() {
  2. private var listener: OnItemClickListener? = null
  3. interface OnItemClickListener {
  4. fun onItemClick(position: Int)
  5. }
  6. fun setOnItemClickListener(listener: OnItemClickListener) {
  7. this.listener = listener
  8. }
  9. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
  10. val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
  11. return MyViewHolder(itemView, listener)
  12. }
  13. override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
  14. val currentitem = list[position]
  15. holder.TaskName.text = currentitem.taskName
  16. holder.Date.text = currentitem.dueDate
  17. holder.Assignee.text = currentitem.assignee
  18. holder.Status.text = currentitem.taskStatus
  19. }
  20. override fun getItemCount(): Int {
  21. return list.size
  22. }
  23. class MyViewHolder(itemView: View, listener: OnItemClickListener?) :
  24. RecyclerView.ViewHolder(itemView) {
  25. val TaskName: TextView = itemView.findViewById(R.id.tvTaskname)
  26. val Assignee: TextView = itemView.findViewById(R.id.tvAssignee)
  27. val Date: TextView = itemView.findViewById(R.id.tvDate)
  28. val Status: TextView = itemView.findViewById(R.id.tvStatus)
  29. init {
  30. itemView.setOnClickListener {
  31. listener?.onItemClick(adapterPosition)
  32. }
  33. }
  34. }
  35. }

字符串
确保您正在Fragment1类中的适配器示例上调用setOnItemClickServer:

  1. adapter.setOnItemClickListener(object : TaskAdapter.OnItemClickListener {
  2. override fun onItemClick(position: Int) {
  3. val intent = Intent(activity, ViewTaskActivity::class.java).apply {
  4. // Put extras
  5. }
  6. startActivity(intent)
  7. }
  8. })

展开查看全部
cigdeys3

cigdeys32#

我想你必须在onBindViewHolder中调用setOnClickListener

  1. override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
  2. ...
  3. holder.ItemView.setOnClickListener{
  4. listener2?.onItemClick(position)
  5. }
  6. }
  7. class MyViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
  8. val ItemView : View = itemView
  9. ...
  10. }

字符串
请注意,recyclerView.adapter被重新分配给没有点击监听器的myAdapter

  1. myAdapter = TaskAdapter(response.body()!!)
  2. layoutManager = manager
  3. adapter = myAdapter


你可以这样引用:

  1. if (responseBody != null) {
  2. myAdapter.updateList(response.body()!!)
  3. }
  4. myAdapter = TaskAdapter(list)
  5. recyclerView.adapter = myAdapter
  6. myAdapter.setOnItemClickListener(...)

展开查看全部

相关问题