仿微信点赞和评论弹出

微信朋友圈的点赞和评论功能,有2个组成部分:左下角的“更多”按钮;点击该按钮后弹出的对话框;

图片 1

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
仿微信点赞和评论弹出,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:

  • Android实现朋友圈评论回复列表
  • Android评论功能的实现过程
  • Android
    仿微信朋友圈点赞和评论弹出框功能
  • Android实现评论栏随Recyclerview滑动左右移动
  • Android
    仿今日头条评论时键盘自动弹出的效果(推荐)
  • Android
    仿抖音的评论列表的UI和效果的实现代码
  • Android模拟登录评论CSDN实现代码
  • Android评论图片可移动顺序选择器(推荐)
  • Android仿微信朋友圈点击评论自动定位到相关行功能
  • Android如何实现社交应用中的评论与回复功能详解

Post Author: admin

发表评论

电子邮件地址不会被公开。 必填项已用*标注