Kotlin:获取点击项目列表视图的值(片段+适配器)
Posted
技术标签:
【中文标题】Kotlin:获取点击项目列表视图的值(片段+适配器)【英文标题】:Kotlin : get value of the clicked item list view (Fragment + Adapter) 【发布时间】:2019-06-06 10:42:04 【问题描述】:我正在尝试从 ListView 之外的列表视图中获取单击的值。当我单击我的项目时,我想在我的片段中填充一个 textView。
在片段中,我有一个 searchView 和 ListView。
这是我的适配器:
class ListAdapterHoraire (context: Context, resource: Int, list: ArrayList<String>, private val arguments: Bundle?, private val forScol : Boolean) : ArrayAdapter<String>(context, resource, list)
private val TAG = ListAdapterCustom::class.java.simpleName
var nameArret: String = ""
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View
val view = super.getView(position, convertView, parent)
val arret = view.findViewById<TextView>(R.id.arret)
val receivedBundle = arguments
arret.setOnClickListener
val fragment = HorairePassage()
fragment.arguments = receivedBundle
nameArret = arret.text.toString()
Log.i(TAG, "$arret.text clicked")
getArretCode(arret.text.toString(), fragment) //récupération du code arret + changement de fragment (en cascade)
if (position % 2 == 1)
view.setBackgroundResource(R.color.colorWhite)
else
view.setBackgroundResource(R.color.grayBackground)
return view
片段的一部分:
search.setOnQueryTextListener(object : android.support.v7.widget.SearchView.OnQueryTextListener
override fun onQueryTextSubmit(newText: String): Boolean
return false
override fun onQueryTextChange(newText: String): Boolean
if(newText.length > 3)
loadingArretSearch.visibility = View.VISIBLE
getListeArrets(view, newText.trim(), resultArgument)
adapter = ListAdapterHoraire(view.context, R.layout.list_adapter, lignes, resultArgument, false)
listSearchView.adapter = adapter
adapter.filter.filter(newText.trim())
if(newText.trim() != "")
listSearchView.visibility = View.VISIBLE
else
listSearchView.visibility = View.GONE
else
lignes.clear()
loadingArretSearch.visibility = View.GONE
listSearchView.visibility = View.GONE
return false
)
【问题讨论】:
【参考方案1】:对于这样的情况 --> 尝试将回调传递给适配器,然后您可以从您的活动中使用任何方法,然后您可以从您的活动中做您想做的事情。
class MyRecyclerViewAdapter(params , val itemClick: (Int) -> Unit): RecyclerView.Adapter<RecyclerView.ViewHolder>()
internal inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)
...
itemView.setOnClickListener( itemClick(layoutPosition) )
In your fragment or Activity
val myAdapter = MyRecyclerViewAdapter(params) position ->
// do something
--> 你的情况
ListAdapterHoraire(view.context, R.layout.list_adapter, lignes, resultArgument, false)value->
// do something
class ListAdapterHoraire (context: Context, resource: Int, list: ArrayList<String>, private val arguments: Bundle?, private val forScol : Boolean,onItemClicked: (text: String) -> Unit) : ArrayAdapter<String>(context, resource, list)
...
arret.setOnClickListener onItemClicked(arret.text.toString())
...
【讨论】:
你能在没有回收站视图的情况下调整这个吗? 你可以看看这个例子吗 adapter = ListAdapterHoraire(view.context, R.layout.list_adapter, lignes, resultArgument, false) value -> Log.i(TAG, "clicked value $value") 和你的适配器 arret.setOnClickListener onItemClicked(arret.text.toString()) 【参考方案2】:尝试在构造函数中传递一个函数作为参数,并在调用 onClickListener 时调用它:
class ListAdapterHoraire (context: Context, resource: Int, list: ArrayList<String>, private val arguments: Bundle?, private val forScol : Boolean,
private val onItemClicked: (text: String) -> Unit) : ArrayAdapter<String>(context, resource, list)
...
arret.setOnClickListener onItemClicked(arret.text.toString())
...
所以,在你的片段中更新适配器实例
adapter = ListAdapterHoraire(view.context, R.layout.list_adapter,
lignes, resultArgument, false,
text -> Log.i("TAG", text) /** Do something here **/ )
...
【讨论】:
我不明白...并且在我的适配器中未检测到“onItemClicked”以上是关于Kotlin:获取点击项目列表视图的值(片段+适配器)的主要内容,如果未能解决你的问题,请参考以下文章
如何在android中去listview适配器项目点击监听器片段?
如何通过单击适配器类中代码的项目中的删除按钮来删除列表视图中的项目后重新加载片段?