Android中使用PopupWindow 仿微信点赞和评论弹出
程序员文章站
2022-07-18 12:20:37
微信朋友圈的点赞和评论功能,有2个组成部分:左下角的“更多”按钮;点击该按钮后弹出的对话框;
popupwindow,弹出框使用popupwindow实现,这是点...
微信朋友圈的点赞和评论功能,有2个组成部分:左下角的“更多”按钮;点击该按钮后弹出的对话框;
popupwindow,弹出框使用popupwindow实现,这是点赞和评论的载体,具体要涉及 popupwindow 点击非窗口位置和再次点击消失以及显示位置的问题(根据相应更多按钮的位置确定 popupwindow 的显示位置
package com.example.cmm.helloworld; import android.app.alertdialog; import android.content.context; import android.graphics.drawable.bitmapdrawable; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.listview; import android.widget.popupwindow; import android.widget.textview; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { private popupwindow mmorepopupwindow; private int mshowmorepopupwindowwidth; private int mshowmorepopupwindowheight; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview lv = (listview) findviewbyid(r.id.listview); lv.setadapter(new myadapter(mainactivity.this, getdata())); } private list<data> getdata() { list<data> data = new arraylist<>(); data.add(new data(r.drawable.xiaona, "薄荷栗", "我学过跆拳道,都给我跪下唱征服", "昨天")); data.add(new data(r.drawable.xueyan, "欣然", "走遍天涯海角,唯有我家风景最好,啊哈哈", "昨天")); data.add(new data(r.drawable.leishao, "陈磊_cl", "老子以后要当行长的,都来找我借钱吧,now", "昨天")); data.add(new data(r.drawable.yuhong, "永恒依然", "房子车子都到碗里来", "昨天")); data.add(new data(r.drawable.lanshan, "蓝珊", "你们这群傻×,我笑而不语", "昨天")); return data; } class myadapter extends baseadapter { private list<data> listdata; private context context; public myadapter(context context, list<data> listdata) { this.context = context; this.listdata = listdata; } @override public int getcount() { return listdata.size(); } @override public object getitem(int arg0) { return listdata.get(arg0); } @override public long getitemid(int arg0) { return arg0; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.listview_item, null, false); // 带赋值区域 imageview ivportrait = (imageview) convertview.findviewbyid(r.id.portrait); textview tvnickname = (textview) convertview.findviewbyid(r.id.nick_name); textview tvcontent = (textview) convertview.findviewbyid(r.id.content); textview tvcreatedat = (textview) convertview.findviewbyid(r.id.created_at); imageview morebtn = (imageview) convertview.findviewbyid(r.id.more_btn); // 赋值 data data = listdata.get(position); ivportrait.setimageresource(data.getportraitid()); tvnickname.settext(data.getnickname()); tvcontent.settext(data.getcontent()); tvcreatedat.settext(data.getcreatedat()); // 更多按钮的点击事件 morebtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { showmore(v); } }); return convertview; } /** * 弹出点赞和评论框 * * @param morebtnview */ private void showmore(view morebtnview) { if (mmorepopupwindow == null) { layoutinflater li = (layoutinflater) mainactivity.this.getsystemservice(context.layout_inflater_service); view content = li.inflate(r.layout.layout_more, null, false); mmorepopupwindow = new popupwindow(content, viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); mmorepopupwindow.setbackgrounddrawable(new bitmapdrawable()); mmorepopupwindow.setoutsidetouchable(true); mmorepopupwindow.settouchable(true); content.measure(view.measurespec.unspecified, view.measurespec.unspecified); mshowmorepopupwindowwidth = content.getmeasuredwidth(); mshowmorepopupwindowheight = content.getmeasuredheight(); view parent = mmorepopupwindow.getcontentview(); textview like = (textview) parent.findviewbyid(r.id.like); textview comment = (textview) parent.findviewbyid(r.id.comment); // 点赞的监听器 like.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { final alertdialog.builder alert = new alertdialog.builder(mainactivity.this); alert.settitle("点赞"); alert.setnegativebutton("取消", null); alert.show(); } }); // 评论的监听器 comment.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { final alertdialog.builder alert = new alertdialog.builder(mainactivity.this); alert.settitle("评论"); alert.setnegativebutton("取消", null); alert.show(); } }); } if (mmorepopupwindow.isshowing()) { mmorepopupwindow.dismiss(); } else { int heightmorebtnview = morebtnview.getheight(); mmorepopupwindow.showasdropdown(morebtnview, -mshowmorepopupwindowwidth, -(mshowmorepopupwindowheight + heightmorebtnview) / 2); } } } class data { private int portraitid; // 头像 private string nickname; // 昵称 private string content; // 说说 private string createdat; // 发布时间 public data(int portraitid, string nickname, string content, string createdat) { this.portraitid = portraitid; this.nickname = nickname; this.content = content; this.createdat = createdat; } public int getportraitid() { return portraitid; } public string getnickname() { return nickname; } public string getcontent() { return content; } public string getcreatedat() { return createdat; } } }
以上所述是小编给大家介绍的android中使用popupwindow 仿微信点赞和评论弹出,希望对大家有所帮助
上一篇: 人参保胎吗,孕后期为什么不能吃人参
下一篇: 保健要警惕 五个坏习惯会减短寿命