亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中使用PopupWindow 仿微信點贊和評論彈出

發布時間:2020-10-08 06:42:14 來源:腳本之家 閱讀:199 作者:ganchuanpu 欄目:移動開發

微信朋友圈的點贊和評論功能,有2個組成部分:左下角的“更多”按鈕;點擊該按鈕后彈出的對話框;

Android中使用PopupWindow 仿微信點贊和評論彈出

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 仿微信點贊和評論彈出,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

红原县| 宁都县| 孝义市| 油尖旺区| 天峻县| 海丰县| 南平市| 河津市| 澄江县| 崇礼县| 蓬溪县| 汽车| 岳阳县| 崇文区| 绥滨县| 南乐县| 名山县| 房产| 务川| 深州市| 茶陵县| 德保县| 独山县| 兴业县| 闽清县| 海丰县| 肥城市| 隆回县| 沂水县| 怀来县| 花莲市| 晴隆县| 祁门县| 金坛市| 临泽县| 萍乡市| 水富县| 永和县| 绥芬河市| 临西县| 嘉定区|