Android中 ListView:响应item内部按钮点击事件(Kotlin语言)

item内部按钮点击事件

  • 前言
  • 效果图
  • 实现过程
    • 1、布局文件
      • (1)布局文件contact
      • (2)布局文件friends_item.xml
    • 2、适配器FriendsAdapter
    • 3、contact.kt文件
    • 4、源码

前言

在上篇文章中我们已经实现了ListView中OnitemClick的点击事件,接下来将对item添加按钮点击事件,实现点击按钮删除所在的item。

效果图

Android中 ListView:响应item内部按钮点击事件(Kotlin语言)_第1张图片

实现过程

1、布局文件

(1)布局文件contact

布局未改变,只有标题栏和一个ListView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F0F8FF">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="#009FCC"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:gravity="center_horizontal"
                    android:text="联系人"
                    android:textColor="#ffffff"
                    android:textFontWeight="30"
                    android:textSize="25dp" />
            </LinearLayout>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white">

            <ListView
                android:id="@+id/lv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </RelativeLayout>
    </LinearLayout>
</FrameLayout>

(2)布局文件friends_item.xml

在Button添加了一个属性android:focusable=”false” ,不让Button强制获取item的焦点

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#F0F8FF"
    android:descendantFocusability="blocksDescendants">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="horizontal"
        android:padding="10dp">
        <ImageView
            android:id="@+id/image"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            />
        <TextView
            android:id="@+id/title"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:textSize="19dp"
            android:textColor="@color/black"/>
        <Button
            android:id="@+id/delete_friends"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="155dp"
            android:background="@color/white"
            android:drawableLeft="@drawable/delete" />
    </LinearLayout>
</LinearLayout>

2、适配器FriendsAdapter

自定义适配器FriendsAdapter.kt,使用了接口回调,当点击删除按钮的时候,将点击item的position作为参数传到contact

package com.example.we_chatting

import android.content.Context
import android.view.ViewGroup
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import android.widget.TextView
import android.widget.BaseAdapter


class FriendsAdapter(context: Context, list: List<Friends>, resourceId: Int) :
    BaseAdapter() {
    private val context: Context
    //定义数据源
    private val list: List<Friends>
    //定义布局资源Id
    private val resourceId: Int
    private var viewHolder: ViewHolder? = null
    override fun getCount(): Int {
        return list.size
    }
    // 获得某一位置的数据
    override fun getItem(position: Int): Any {
        return list[position]
    }
    //获得唯一标识
    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, view: View?, viewGroup: ViewGroup?): View? {
        var view = view
        if (view == null) {
            view = LayoutInflater.from(context).inflate(resourceId, null)
            viewHolder = ViewHolder()
            viewHolder!!.friendImage = view.findViewById<View>(R.id.image) as ImageView
            viewHolder!!.friendName = view.findViewById<View>(R.id.title) as TextView
            viewHolder!!.mButton  = view.findViewById<View>(R.id.delete_friends) as Button
            view.tag = viewHolder
        } else {
            viewHolder = view.tag as ViewHolder
        }
        viewHolder!!.friendImage!!.setImageResource(list[position].friendsimageId)
        viewHolder!!.friendName!!.text = list[position].friendsname
        viewHolder!!.mButton!!.setOnClickListener(View.OnClickListener {
            mOnItemDeleteListener?.onDeleteClick(
                position
            )
        })
        return view
    }

    /*
         删除好友的监听接口
         */
    interface onItemDeleteListener {
        fun onDeleteClick(position: Int)
    }

    private var mOnItemDeleteListener: onItemDeleteListener? = null

    fun setOnItemDeleteClickListener(mOnItemDeleteListener: onItemDeleteListener?) {
        this.mOnItemDeleteListener = mOnItemDeleteListener
    }

    internal inner class ViewHolder {
        var friendImage: ImageView? = null
        var friendName: TextView? = null
        var mButton: Button?=null
    }

    init {
        this.context = context
        this.list = list
        this.resourceId = resourceId
    }
}

3、contact.kt文件

package com.example.we_chatting

import android.app.AlertDialog
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import java.util.*


class contact : Fragment() {

    val friendslist = ArrayList<Friends>()
    var listView: ListView? = null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val page: View = inflater.inflate(R.layout.fragment_contact, container, false)

        listView = page.findViewById<View>(R.id.lv) as ListView
        //初始化
        initfriend()
        val adapter = getContext()?.let {
            FriendsAdapter(
                it, friendslist,
                R.layout.friends_item)
        }
        listView!!.adapter = adapter
        listView!!.setOnItemClickListener { parent, view, position, id ->
            val friend = friendslist[position]
            // Toast.makeText(getActivity(), fruit.friendsname ,Toast.LENGTH_SHORT).show()
            val alertdialogbuilder: AlertDialog.Builder = AlertDialog.Builder(getActivity())
            alertdialogbuilder.setMessage("您确认要与  " + friend.friendsname + "  聊天吗?")
            alertdialogbuilder.setPositiveButton("确定", null)
            alertdialogbuilder.setNeutralButton("取消", null)
            val alertdialog1: AlertDialog = alertdialogbuilder.create()
            alertdialog1.show()
        }
        //删除好友
        adapter!!.setOnItemDeleteClickListener(object : FriendsAdapter.onItemDeleteListener {
            override fun onDeleteClick(i: Int) {
                friendslist.removeAt(i)
                adapter.notifyDataSetChanged()
            }
        })
        return page
    }
    private fun initfriend() {
        repeat(2) {
            friendslist.add(Friends("卷王1", R.drawable.friend1))
            friendslist.add(Friends("卷王2", R.drawable.friend1))
            friendslist.add(Friends("卷王3", R.drawable.friend1))
            friendslist.add(Friends("卷王4", R.drawable.friend1))
            friendslist.add(Friends("卷王5", R.drawable.friend1))
            friendslist.add(Friends("卷王6", R.drawable.friend1))
            friendslist.add(Friends("卷王7", R.drawable.friend1))
            friendslist.add(Friends("卷王8", R.drawable.friend1))
            friendslist.add(Friends("卷王9", R.drawable.friend1))
            friendslist.add(Friends("卷王10", R.drawable.friend1))
            friendslist.add(Friends("卷王11", R.drawable.friend1))
            friendslist.add(Friends("卷王12", R.drawable.friend1))
        }
    }
}



4、源码

源码可通过此链接下载,如果有任何问题可在评论区告诉我,最后感谢你的阅读啦!!

你可能感兴趣的:(android(kotlin),kotlin,android)