You just have to set it up properly and implement the callbacks.
So start by making yourself an interface. you can either nest it in your adapter class (which is common practice) or you can separate it out, up to you. For this example, I nested it at the bottom of the class.
NOTE*
I would recommend using RecyclerViews and RecyclerView.Adapter unless you have a static list or know your content is small. I will supply an example of a small list of filter items where a simple baseAdapter is adequate for your knowledge sharing.
Then let's make an adapter. I will share an example of a filter menu I created.
Notice the ViewHolder pattern for ensuring reusable UI and passing the callback listener into place.
import android.content.Context
import android.support.v7.widget.SwitchCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.LinearLayout
import android.widget.Space
import android.widget.TextView
import com.appstudio35.yourappstudio.R
import com.appstudio35.yourappstudio.models.CategoryModel
import java.util.*
/**
* Copyright © 2017 App Studio 35. All rights reserved.
*
* Created by App Studio 35 on 7/3/17.
*/
class FilterListAdapter : BaseAdapter {
////////////////////////////////////////////////////////////////
// MEMBERS
////////////////////////////////////////////////////////////////
private var mData: ArrayList<CategoryModel>
private var mContext: Context
private var mActionMenuItemClickListener: IActionMenuItemClickListener?
private var layoutInflater: LayoutInflater
/////////////////////////////////////////////////////////////////
// CONSTRUCTOR
/////////////////////////////////////////////////////////////////
constructor(context: Context, data: ArrayList<CategoryModel>, clickListener: IActionMenuItemClickListener): super(){
mContext = context
mData = data
mActionMenuItemClickListener = clickListener
layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
}
/////////////////////////////////////////////////////////////////
// BASE CLASS OVERRIDES
/////////////////////////////////////////////////////////////////
override fun getCount(): Int {
return mData.size
}
override fun getItem(position: Int): Any? {
return mData[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val holder: ViewHolder
val model = mData!![position]
var retView: View
if (convertView == null) {
holder = ViewHolder()
retView = layoutInflater.inflate(R.layout.row_filter_model_action, parent, false)
holder.llRoot = retView.findViewById<View>(R.id.llRoot) as LinearLayout
holder.txtDisplayText = retView.findViewById<View>(R.id.txtDisplayText) as TextView
holder.swIsSelected = retView.findViewById<View>(R.id.swIsSelected) as SwitchCompat
holder.spcFilter = retView.findViewById<View>(R.id.spcFilter) as Space
holder.dividerBottom = retView.findViewById(R.id.dividerBottom)
retView.tag = holder
} else {
holder = convertView.tag as ViewHolder
retView = convertView
}
holder.txtDisplayText!!.text = model.getCategoryName()
//holder.dividerBottom.setVisibility(model.getIsTitleRow() ? View.VISIBLE : View.GONE);
holder.swIsSelected!!.visibility = View.VISIBLE
holder.swIsSelected!!.isChecked = model.getIsFilterApplied()
//holder.spcFilter.setVisibility(model.getIsTitleRow() ? View.GONE : View.VISIBLE);
actionMenuItem_onClick(retView, position)
return retView
}
/*///////////////////////////////////////////////////////////////
// CLICK LISTENER
*////////////////////////////////////////////////////////////////
private fun actionMenuItem_onClick(convertView: View, position: Int) {
convertView.setOnClickListener {
mActionMenuItemClickListener?.onMenuItemClicked(convertView, position)
}
}
/*///////////////////////////////////////////////////////////////
// VIEWHOLDER PATTERN
*////////////////////////////////////////////////////////////////
class ViewHolder {
var llRoot: LinearLayout? = null
var txtDisplayText: TextView? = null
var swIsSelected: SwitchCompat? = null
var spcFilter: Space? = null
var dividerBottom: View? = null
}
/*///////////////////////////////////////////////////////////////
// INTERFACE
*////////////////////////////////////////////////////////////////
interface IActionMenuItemClickListener {
fun onMenuItemClicked(v: View, position: Int)
}
}
Now you just implement the interface and catch the click and you are done.
class myFragment : BaseFragment(), FilterListAdapter.IActionMenuItemClickListener {
now in the class you must implement the callback.
override fun onMenuItemClicked(v: View, position: Int) {
//handle your clicked item
}
That's all there is to it, and you are done. Happy coding.