如何从Kotlin中的适配器调用片段的功能?

时间:2019-12-21 06:48:39

标签: android android-fragments kotlin

我是Kotlin android开发的初学者,无法从适配器调用片段函数。按照this的方法,我写了下面的代码,但还是没有运气。

适配器代码

class CallItemAdapter(
    val context: Context,
    private val callItems: List<CallItem>,
    private val listener: ClickListener
) : RecyclerView.Adapter<CallItemAdapter.MyViewHolder>() {
    interface ClickListener {
        fun onClickListener(item: CallItem)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val callItemView = LayoutInflater.from(context).inflate(R.layout.item_call, parent, false)
        return MyViewHolder(callItemView)
    }

    override fun getItemCount(): Int {
        return callItems.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val callItem = callItems[position]
        holder.setData(callItem, position, listener)
    }

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private var currentCallItem: CallItem? = null
        private var currentPosition: Int = 0

        init {
            itemView.btnCall.setOnClickListener {
               // TODO 
            }
        }

        fun setData(callItem: CallItem?, position: Int, listener: ClickListener) {
            itemView.txtPersonName.text = callItem!!.name
            itemView.txtTask.text = callItem.task
            itemView.setOnClickListener {
                listener.onClickListener(callItem)
            }

            this.currentCallItem = callItem
            this.currentPosition = position
        }
    }
}

片段代码

class CallingWorkFragment : Fragment(), CallItemAdapter.ClickListener {
    override fun onClickListener(item: CallItem) {
        println("Interface working")
        context!!.applicationContext.showToast("Wubba lubba dub dub")
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_work_calling, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val callItems: MutableList<CallItem> = ArrayList()

        val adapter = CallItemAdapter(activity!!.applicationContext, callItems, this)
        recyclerViewCallItem.adapter = adapter
    }
.
.
.

但是单击时没有任何反应。请让我知道我在做什么错。 编辑:我已经添加了完整的适配器代码。

1 个答案:

答案 0 :(得分:1)

您应该:

class CallItemAdapter(
    val context: Context,
    private val callItems: List<CallItem>,
    private val listener: ClickListener
    ) : RecyclerView.Adapter<CallItemAdapter.MyViewHolder>() {
    interface ClickListener {
        fun onClickListener(item: CallItem)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val callItemView = LayoutInflater.from(context).inflate(R.layout.item_call, parent, false)
        return MyViewHolder(callItemView)
    }

    override fun getItemCount(): Int {
        return callItems.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val callItem = callItems[position]
        holder.setData(callItem, position)

        holder.itemView.setOnClickListener {
            listener.onClickListener(callItem)
        }

        // or 
        holder.itemView.btnCall.setOnClickListener {
            // TODO 
        }
    }

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private var currentCallItem: CallItem? = null
        private var currentPosition: Int = 0

        fun setData(callItem: CallItem?, position: Int) {
            itemView.txtPersonName.text = callItem!!.name
            itemView.txtTask.text = callItem.task

            this.currentCallItem = callItem
            this.currentPosition = position
        }
    }
}