我實現的效果是點擊按鈕 一個從底部出現popwindow的動畫 窗體顏色變灰 顯示recyclerview列表,以及實現類傳入適配器數據和點擊事件 以及點擊外部隱藏popwindow
上代碼:
class PlayListPopWindow(context: Context, adapter: PopWindowAdapter,listener:BaseQuickAdapter.OnItemClickListener,val window: Window):PopupWindow() {
var alpha:Float = 0f
init {
//記錄當前透明度
alpha = window.attributes.alpha
//設置佈局
val view = LayoutInflater.from(context).inflate(R.layout.pop_playlist,null,false)
//獲取recyclerview
val recyclerview = view.findViewById<RecyclerView>(R.id.pop_recy)
//適配
recyclerview.layoutManager = LinearLayoutManager(context)
recyclerview.adapter= adapter
adapter.setOnItemClickListener(listener)
contentView = view
//設置寬高
width = ViewGroup.LayoutParams.MATCH_PARENT
//設置高度爲屏幕的3/5
val manager =context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val point = Point()
manager.defaultDisplay.getSize(point)
val windowheight = point.y
height = (windowheight*3)/5
//設置獲取焦點
isFocusable = true
//點擊外部空白區域
isOutsideTouchable = true
//能夠響應返回按鈕 低版本點擊返回按鈕能dismiss
setBackgroundDrawable(ColorDrawable())
//處理popwindow動畫
animationStyle = R.style.pop
}
override fun showAsDropDown(anchor: View?, xoff: Int, yoff: Int, gravity: Int) {
super.showAsDropDown(anchor, xoff, yoff, gravity)
//當前popwindow已經顯示了
val attarbutes = window.attributes
attarbutes.alpha = 0.3f
//設置到應用程序窗體上面
window.attributes = attarbutes
}
override fun dismiss() {
super.dismiss()
//隱藏
val attarbutes = window.attributes
attarbutes.alpha = alpha
window.attributes = attarbutes
}
}
實現類
//點擊事件
override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
//播放當前歌曲
Iservice?.playPosition(position)
}
//初始化數據
list?.let {
val adapter = PopWindowAdapter(it)
//獲取底部高度
val bottomH = woshibottom.height
val popWindow = PlayListPopWindow(this,adapter,this,window)
popWindow.showAsDropDown(woshibottom,0,bottomH)
}