Android中使用PopupWindow 仿微信點(diǎn)贊和評論彈出
微信朋友圈的點(diǎn)贊和評論功能,有2個(gè)組成部分:左下角的“更多”按鈕;點(diǎn)擊該按鈕后彈出的對話框;

PopupWindow,彈出框使用PopupWindow實(shí)現(xiàn),這是點(diǎn)贊和評論的載體,具體要涉及 PopupWindow 點(diǎn)擊非窗口位置和再次點(diǎ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<Data> getData() {
List<Data> data = new ArrayList<>();
data.add(new Data(R.drawable.xiaona, "薄荷栗", "我學(xué)過跆拳道,都給我跪下唱征服", "昨天"));
data.add(new Data(R.drawable.xueyan, "欣然", "走遍天涯海角,唯有我家風(fēng)景最好,啊哈哈", "昨天"));
data.add(new Data(R.drawable.leishao, "陳磊_CL", "老子以后要當(dāng)行長的,都來找我借錢吧,now", "昨天"));
data.add(new Data(R.drawable.yuhong, "永恒依然", "房子車子都到碗里來", "昨天"));
data.add(new Data(R.drawable.lanshan, "藍(lán)珊", "你們這群傻×,我笑而不語", "昨天"));
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);
// 帶賦值區(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)贊和評論框
*
* @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)聽器
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();
}
});
// 評論的監(jiān)聽器
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; // 發(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)贊和評論彈出,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android 仿微信朋友圈點(diǎn)贊和評論彈出框功能
- 簡單實(shí)用的Android UI微博動態(tài)點(diǎn)贊效果
- Android高級UI特效仿直播點(diǎn)贊動畫效果
- Android項(xiàng)目開發(fā) 教你實(shí)現(xiàn)Periscope點(diǎn)贊效果
- Android自定義view實(shí)現(xiàn)仿抖音點(diǎn)贊效果
- Android中Listview點(diǎn)贊功能的實(shí)現(xiàn)
- Android仿微信朋友圈點(diǎn)贊和評論功能
- Android自定義ViewGroup實(shí)現(xiàn)堆疊頭像的點(diǎn)贊Layout
- Android控件實(shí)現(xiàn)直播App點(diǎn)贊飄心動畫
- Android SDK安裝及配置教程
相關(guān)文章
Flutter 使用fluro的轉(zhuǎn)場動畫進(jìn)行頁面切換
在實(shí)際應(yīng)用中,我們常常會對不同的頁面采取不同的轉(zhuǎn)場動畫,以提高頁面切換過程中的用戶體驗(yàn)。例如,微信的掃碼后在手機(jī)上確認(rèn)登錄頁面就是從底部彈出的,而大部分頁面的跳轉(zhuǎn)都是從右向左滑入。通過這種形式區(qū)分不同的轉(zhuǎn)場場景,從而給用戶更多的趣味性以提高用戶體驗(yàn)。2021-06-06
Android 如何攔截用戶頻繁操作(點(diǎn)擊事件)
本文主要介紹了Android 如何攔截用戶頻繁操作,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Android百度地圖應(yīng)用開發(fā)基礎(chǔ)知識
這篇文章主要為大家詳細(xì)介紹了Android百度地圖應(yīng)用開發(fā)基礎(chǔ)知識,為開發(fā)百度地圖應(yīng)用做準(zhǔn)備,感興趣的小伙伴們可以參考一下2016-06-06
Android實(shí)戰(zhàn)RecyclerView頭部尾部添加方法示例
本篇文章主要介紹了Android實(shí)戰(zhàn)RecyclerView頭部尾部添加方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Android創(chuàng)建與解析XML(二)——詳解Dom方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(二)——詳解Dom方式 ,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11
Android利用RecyclerView實(shí)現(xiàn)全選、置頂和拖拽功能示例
列表控件可以說是我們絕大部分App中都會使用的,為了提升交互樂趣,我們經(jīng)常需要在列表中加入置頂、拖拽等操作,下面這篇文章主要介紹了Android利用RecyclerView如何實(shí)現(xiàn)全選、置頂和拖拽功能的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04
Android UI設(shè)計(jì)系列之自定義ViewGroup打造通用的關(guān)閉鍵盤小控件ImeObserverLayout(9)
這篇文章主要介紹了Android UI設(shè)計(jì)系列之自定義ViewGroup打造通用的關(guān)閉鍵盤小控件ImeObserverLayout,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06

