android中的setOnLongClickListener与kotlin

时间:2017-08-22 09:04:14

标签: android listview kotlin

如何在setOnItemClickListner的每个项目中使用ListView

我的xml:

<ListView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</ListView>

这是我的适配器类

inner class mo3d1Adapter : BaseAdapter {
    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {
        return listOfmo3d.size
    }

    var listOfMkabala = ArrayList<MeetingDetails>()
    var context: Context? = null

    constructor(context: Context, listOfMkabaln: ArrayList<MeetingDetails>) : super() {
        this.listOfMkabala = listOfMkabaln
        this.context = context
    }

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        val mo3d = listOfmo3d[p0]

        var inflatormo3d = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var myViewmo3d = inflatormo3d.inflate(R.layout.fragment_item, null)

        lvMo3d.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, i, l ->
            Toast.makeText(context, "   TEST STACK   ", Toast.LENGTH_LONG).show()

        }


        myViewmo3d.meeting_name.text = mo3d.name1!!
        myViewmo3d.meeting_date.text = mo3d.date.toString()!!
        myViewmo3d.attendance_number.text = mo3d.n2.toString()!!



        return myViewmo3d


    }

    override fun getItem(p0: Int): Any {
        return listOfmo3d[p0]

    }


}
  • 我想要监听ListView
  • 中的每个项目

当我在适配器中使用此方法setOnClickListener时它不起作用,我可以在哪里使用?

3 个答案:

答案 0 :(得分:8)

在您的活动类

中尝试此操作
lv.setOnItemClickListener { parent, view, position, id ->
    Toast.makeText(this, "Position Clicked:"+" "+position,Toast.LENGTH_SHORT).show()
}

答案 1 :(得分:1)

虽然有点古怪,但对我来说很好。

    latestMessagesAdapter.setOnItemLongClickListener { item, view ->
        val row = item as LatestMessageRow
        return@setOnItemLongClickListener(true)
    }

答案 2 :(得分:0)

首先,我想告诉它是RecyclerView而不是ListView。你可以找到很多信息,为什么要这样做。例如,您可以阅读它:

RecyclerView vs. ListView

关于如何使用RecyclerView以正确方式执行此操作的问题。

使用RecyclerView插入依赖项,它们现在位于Kotlin的支持库中。

实施&#34; com.android.support:appcompat-v7:25.4.0&#34;

首先在xml布局中使用RecyclerView更改ListView,如下所示:

<android.support.v7.widget.RecyclerView
    android:id="@+id/accountList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

Adapter创建RecyclerView

class AccountListAdapter(val accountList: AccountList, val itemListener: (Account) -> Unit) :
    RecyclerView.Adapter<AccountListAdapter.ViewHolder>(){

    override fun getItemCount(): Int = accountList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) =
        holder.bind(accountList[position])


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder{
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_account, parent, false)
        return ViewHolder(view, itemListener)
    }

    class ViewHolder(itemView: View, val itemClick: (Account) -> Unit): RecyclerView.ViewHolder(itemView){
        fun bind(account : Account){
            with(account){
                itemView.accountName.text = title
                itemView.setOnClickListener{ itemClick(this)}
            }
        }
    }
}

item_account.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/accountName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

模型(在Kotlin中,您可以将它们放在一个文件中并命名为 AccountModels.kt ):

data class AccountList(val accounts: List<Account>){

    val size : Int
        get() = accounts.size

    operator fun get(position: Int) = accounts[position]
}

data class Account(val id : Long, val title : String, val balance : Int, val defCurrency: Int)

片段/活动中,将AdapterRecyclerView相关联:

override fun onStart() {
    super.onStart()
    setupAdapter()
}

fun setupAdapter(){

    Log.d(TAG, "updating ui..")
    val account1 = Account(1,"Credit", 1000, 2)
    val account2 = Account(2, "Debit", 500, 2)
    val account3 = Account(3, "Cash", 7000, 2)

    val accounts : List<Account> = listOf(account1, account2, account3)

    val adapter = AccountListAdapter(AccountList(accounts)){
        val title = it.title
        Log.d(TAG, "$title clicked")
    }
    accountList.layoutManager = LinearLayoutManager(activity)
    accountList.adapter = adapter
}

就是这样。现在一切都应该有效。希望它有所帮助。