Android自定义PopWindow

我实现的效果是点击按钮 一个从底部出现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)
        }

你可能感兴趣的:(细节功能的实现)