微信朋友圈的點(diǎn)贊和評(píng)論功能,有2個(gè)組成部分:左下角的“更多”按鈕;點(diǎn)擊該按鈕后彈出的對(duì)話(huà)框;
成都創(chuàng)新互聯(lián)公司是專(zhuān)業(yè)的神池網(wǎng)站建設(shè)公司,神池接單;提供網(wǎng)站制作、網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行神池網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
PopupWindow,彈出框使用PopupWindow實(shí)現(xiàn),這是點(diǎn)贊和評(píng)論的載體,具體要涉及 PopupWindow 點(diǎn)擊非窗口位置和再次點(diǎn)擊消失以及顯示位置的問(wèn)題(根據(jù)相應(yīng)更多按鈕的位置確定 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 getData() { List data = new ArrayList<>(); data.add(new Data(R.drawable.xiaona, "薄荷栗", "我學(xué)過(guò)跆拳道,都給我跪下唱征服", "昨天")); data.add(new Data(R.drawable.xueyan, "欣然", "走遍天涯海角,唯有我家風(fēng)景最好,啊哈哈", "昨天")); data.add(new Data(R.drawable.leishao, "陳磊_CL", "老子以后要當(dāng)行長(zhǎng)的,都來(lái)找我借錢(qián)吧,now", "昨天")); data.add(new Data(R.drawable.yuhong, "永恒依然", "房子車(chē)子都到碗里來(lái)", "昨天")); data.add(new Data(R.drawable.lanshan, "藍(lán)珊", "你們這群傻×,我笑而不語(yǔ)", "昨天")); return data; } class MyAdapter extends BaseAdapter { private List listdata; private Context context; public MyAdapter(Context context, List 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); // 帶賦值區(qū)域 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()); // 更多按鈕的點(diǎn)擊事件 moreBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showMore(v); } }); return convertView; } /** * 彈出點(diǎn)贊和評(píng)論框 * * @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); // 點(diǎn)贊的監(jiān)聽(tīng)器 like.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("點(diǎn)贊"); alert.setNegativeButton("取消", null); alert.show(); } }); // 評(píng)論的監(jiān)聽(tīng)器 comment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("評(píng)論"); 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; // 昵稱(chēng) private String content; // 說(shuō)說(shuō) private String createdAt; // 發(fā)布時(shí)間 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 仿微信點(diǎn)贊和評(píng)論彈出,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!