Android 显示在某个位置下的菜单(Popwindow实现)
程序员文章站
2022-05-31 14:01:25
...
直接看效果 一目了然,现在咱们来看看怎么实现的吧
step1: 咱们先定义一个布局吧 view_dropdown.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="43dp"
android:paddingBottom="1dp"
android:background="@drawable/bg_corner_gray">
<ListView
android:id="@+id/lv_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@null"
android:padding="1dp"/>
</RelativeLayout>
step2: 定义类 DropDownDialog.kt
package com.bbx.bmtc.dialog
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.util.Pair
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.bbx.bmtc.R
import com.bbx.bmtc.ext.isEmpty
import com.bbx.bmtc.ext.showLog
import com.bbx.bmtc.helper.ComputeHelper
import com.bbx.bmtc.utils.UtilSystem
object DropDownDialog {
private var popupWindow: PopupWindow? = null
private var mTypeAdapter: DropTypeAdapter? = null
interface ClickItemCallback {
fun onItemClick(action: Int)
}
interface ClickOnDismissCallback {
fun onDismiss()
}
fun showActionDialog(
context: Context,
view: View,
actions: List<Pair<String, Int>>,
clickItemCallback: ClickItemCallback?=null,
clickOnDismissCallback: ClickOnDismissCallback?=null,
width:Int=500
) {
if (popupWindow != null && popupWindow!!.isShowing) {
popupWindow?.dismiss()
}
if (isEmpty(mTypeAdapter)) {
val itemAccountId = R.layout.item_drop_text
mTypeAdapter = DropTypeAdapter(context, itemAccountId, actions)
}
var mTypePopupView = LayoutInflater.from(context).inflate(R.layout.view_dropdown, null)
var mTypeListView = mTypePopupView.findViewById<ListView>(R.id.lv_list)
mTypeListView.adapter = mTypeAdapter
mTypeListView.onItemClickListener = AdapterView.OnItemClickListener { _, _, i, l ->
popupWindow?.let {
clickItemCallback?.onItemClick(actions[i].second)
popupWindow?.dismiss()
}
}
val listPadding = UtilSystem.dip2px(context, 5f)
val itemHeight = UtilSystem.dip2px(context, 40f)
var windowHeight = itemHeight * actions.size + listPadding * 2
val min = itemHeight + listPadding * 2
val max = itemHeight * 10
if (windowHeight > max) {
windowHeight = max
} else if (windowHeight < min) {
windowHeight = min
}
var width:Int=ComputeHelper.getTextMaxLen(context,actions).toInt()
popupWindow = PopupWindow(mTypePopupView, width+UtilSystem.dip2px(context, 30f), windowHeight)
popupWindow?.isOutsideTouchable = true
popupWindow?.setBackgroundDrawable(BitmapDrawable())
popupWindow?.setOnDismissListener {
clickOnDismissCallback?.onDismiss()
}
popupWindow?.isFocusable = true
val anchorLoc = IntArray(2)
view.getLocationOnScreen(anchorLoc)
showLog(""+anchorLoc[0])
showLog(""+anchorLoc[1])
popupWindow?.showAsDropDown(view, 0, 30)
}
internal class DropTypeAdapter(
val mContext: Context,
val mResId: Int,
val mItems: List<Pair<String, Int>>
) : ArrayAdapter<Pair<String, Int>?>(mContext, mResId, mItems) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var holder: DropTypeViewHolder
var tempView = convertView
if (tempView == null) {
holder = DropTypeViewHolder()
tempView = LayoutInflater.from(mContext).inflate(mResId, parent, false)
holder.tvStockCode = tempView.findViewById(R.id.tv_text)
tempView.tag = holder
}
tempView?.let {
holder = tempView.tag as DropTypeViewHolder
holder.tvStockCode?.text = getItem(position)?.first
}
return tempView!!
}
internal inner class DropTypeViewHolder {
var tvStockCode: TextView? = null
}
}
}
step3: 定义adapter需要的item布局文件 item_drop_text.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textColor="@color/whiteText"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#2a3342"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
</RelativeLayout>
step4: 调用方法如下
var list= mutableListOf<Pair<String, Int>>()
list.add(Pair("111",0))
list.add(Pair("1234",0))
list.add(Pair("123",0))
view.setOnClickListener {
DropDownDialog.showActionDialog(this,tv_switch_account,list,object : DropDownDialog.ClickItemCallback{
override fun onItemClick(action: Int) {
}
})
}
注:bg_corner_gray.xml可以是自己定义的颜色,也可是UI设计切的背景图
下一篇: 红豆薏米可以瘦身吗