下面是我的Fragment1.kt整体代码
class Fragment1 : Fragment() {
private lateinit var binding: Fragment1Binding
private lateinit var list : ArrayList<TasksItem>
private lateinit var recyclerView: RecyclerView
private lateinit var manager: RecyclerView.LayoutManager
private lateinit var myAdapter: RecyclerView.Adapter<*>
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_1, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recyclerView = view.findViewById(R.id.app_list)
recyclerView.setHasFixedSize(true)
list = arrayListOf()
manager = LinearLayoutManager(this.requireContext())
GroomedKidzApi.retrofitService.getTasksProgress().enqueue(object :
Callback<List<TasksItem>?> {
override fun onResponse(
call: Call<List<TasksItem>?>,
response: Response<List<TasksItem>?>
) {
val responseBody = response.body()
if (responseBody != null) {
recyclerView = view.findViewById<RecyclerView>(R.id.app_list).apply {
myAdapter = TaskAdapter(response.body()!!)
layoutManager = manager
adapter = myAdapter
Log.e("TAG", "response 33: "+ (response.body()) )
}
}
}
override fun onFailure(call: Call<List<TasksItem>?>, t: Throwable) {
}
})
var adapter = TaskAdapter(list)
recyclerView.adapter = adapter
adapter.setOnItemClickListener(object : TaskAdapter.OnItemClickListener {
override fun onItemClick(position: Int) {
val intent = Intent(activity,ViewTaskActivity::class.java).apply{
putExtra("taskname", list[position].taskName)
putExtra("desc", list[position].taskDesc)
putExtra("category", list[position].category)
putExtra("duedate", list[position].dueDate)
putExtra("assignee", list[position].assignee)
putExtra("point", list[position].pointEarned)
Log.d("Fragment1", "onViewCreated executed")
}
startActivity(intent)
}
})
}
}
字符串
下面是TaskAdapter的整体代码
class TaskAdapter(val list: List<TasksItem> ) : RecyclerView.Adapter<TaskAdapter.MyViewHolder>() {
private var listener2: OnItemClickListener? = null
interface OnItemClickListener {
fun onItemClick(position: Int)
}
fun setOnItemClickListener(listener: OnItemClickListener){
listener2 = listener
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item,
parent,false)
return MyViewHolder(itemView,listener2)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentitem = list[position]
holder.TaskName.text = currentitem.taskName
holder.Date.text = currentitem.dueDate
holder.Assignee.text = currentitem.assignee
holder.Status.text = currentitem.taskStatus
}
override fun getItemCount(): Int {
return list.size
}
class MyViewHolder(itemView : View, listener: OnItemClickListener?) : RecyclerView.ViewHolder(itemView){
val TaskName : TextView = itemView.findViewById(R.id.tvTaskname)
val Assignee : TextView = itemView.findViewById(R.id.tvAssignee)
val Date : TextView = itemView.findViewById(R.id.tvDate)
val Status : TextView = itemView.findViewById(R.id.tvStatus)
init {
itemView.setOnClickListener{
listener?.onItemClick(adapterPosition)
}
}
}
}
型onItemClick
内部的Log.d("Fragment1", "onViewCreated executed")
的logcat没有被调用。但是,可以成功调用其他函数。
为什么onItemClick
没有被调用?所有函数都工作正常,即使是API。
注:适配器很好,setOnClickListener
也能得到位置
2条答案
按热度按时间vshtjzan1#
看起来你的TaskAdapter中的setOnClickList实现可能有问题。
字符串
确保您正在Fragment1类中的适配器示例上调用setOnItemClickServer:
型
cigdeys32#
我想你必须在
onBindViewHolder
中调用setOnClickListener
字符串
请注意,
recyclerView.adapter
被重新分配给没有点击监听器的myAdapter
型
你可以这样引用:
型