Android仿QQ、微信聊天界面長(zhǎng)按提示框效果
先來(lái)看看效果圖
如何使用
示例代碼
PromptViewHelper pvHelper = new PromptViewHelper(mActivity); pvHelper.setPromptViewManager(new ChatPromptViewManager(mActivity)); pvHelper.addPrompt(holder.itemView.findViewById(R.id.textview_content));
使用起來(lái)還是很簡(jiǎn)單的
首先new一個(gè)PromptViewHelper
類(lèi),然后設(shè)置一個(gè)提示view管理器,最后調(diào)用addPrompt
方法添加view,此 view就是要添加提示框的view。
實(shí)現(xiàn)思路
通過(guò)使用QQ,微信這個(gè)功能,感覺(jué)提示框使用PopupWindow
應(yīng)該是可以滿足需求的。
所以大體的思路就是:
1、View加載成功的時(shí)候給它添加長(zhǎng)按事件
2、用戶長(zhǎng)按的時(shí)候new一個(gè)PopupWindow
,并且顯示它,并且設(shè)置點(diǎn)擊外部區(qū)域可以消失
架構(gòu)
為了讓上層調(diào)用簡(jiǎn)單,方便,我打算把提示框View封裝到一個(gè)類(lèi)中,這個(gè)類(lèi)包括:初始方法,綁定數(shù)據(jù),添加事件等等;基于這樣的考慮,首先定義一個(gè)抽象類(lèi),然后讓具體的實(shí)現(xiàn)類(lèi)來(lái)實(shí)現(xiàn)相應(yīng)的方法,我們先來(lái)看看這個(gè)抽象類(lèi)。
public static abstract class PromptViewManager { private View promptView; protected Activity activity; private String[] dataArray; private Location location; public OnItemClickListener onItemClickListener; public PromptViewManager(Activity activity, String[] dataArray, Location location) { this.activity = activity; this.dataArray = dataArray; this.location = location; init(); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } public void init() { promptView = inflateView(); bindData(promptView, dataArray); } public abstract View inflateView(); public abstract void bindData(View view, String[] dataArray); public View getPromptView() { return promptView; } public Location getLocation() { return location; } }
注意:在一個(gè)抽象類(lèi)中有一個(gè)Location對(duì)象的屬性,這個(gè)Location是做什么的個(gè),因?yàn)槲覀冊(cè)陲@示這個(gè)提示框View的時(shí)候會(huì)要考慮它顯示的位置,這個(gè)Location是個(gè)枚舉對(duì)象,它里面就包括了一些位置的信息;
public enum Location { TOP_LEFT(1), TOP_CENTER(2), TOP_RIGHT(3), BOTTOM_LEFT(4), BOTTOM_CENTER(5), BOTTOM_RIGHT(6); ICalculateLocation calculateLocation; private Location(int type) { switch (type) { case 1: calculateLocation = ICalculateLocation 實(shí)現(xiàn)類(lèi) break; case 2: calculateLocation = ICalculateLocation 實(shí)現(xiàn)類(lèi) break; TODO } }
這個(gè)枚舉對(duì)象里面包含了6種位置顯示類(lèi)型,然后在構(gòu)造方法里面根據(jù)type類(lèi)型會(huì)實(shí)例化一個(gè)ICalculateLocation
對(duì)象,ICalculateLocation
是什么呢?
public interface ICalculateLocation { int[] calculate(int[] srcViewLocation, View srcView, View promptView); }
它是一個(gè)接口,提供了一個(gè)calculate
方法來(lái)計(jì)算提示框View的x,y坐標(biāo),我們通過(guò)實(shí)現(xiàn)這個(gè)接口來(lái)計(jì)算不同位置的坐標(biāo)。
到這,大體的架構(gòu)已經(jīng)出來(lái)了;首先我們定義一個(gè)PromtpViewManager
管理器來(lái)來(lái)實(shí)現(xiàn)提示框View的加載,綁定數(shù)據(jù),添加事件,然后通過(guò)設(shè)置的Location枚舉來(lái)實(shí)現(xiàn)不同的計(jì)算類(lèi),計(jì)算出不同位置的坐標(biāo),然后在顯示的時(shí)候new一個(gè)PopupWindow
,通過(guò)PromtpViewManager
得到提示框View設(shè)置給PopupWindow
,再通過(guò)PromtpViewManager
得到Location枚舉得到計(jì)算坐標(biāo)的類(lèi),調(diào)用calculate方法得到x,y坐標(biāo),然后通過(guò)PopupWindow
的showAtLocation
方法來(lái)顯示PopupWindow
提示框。
具體實(shí)現(xiàn)
首先實(shí)現(xiàn)一個(gè)PromtpViewManager
管理類(lèi)
public class ChatPromptViewManager extends PromptViewHelper.PromptViewManager { public ChatPromptViewManager(Activity activity, String[] dataArray, Location location) { super(activity, dataArray, location); } public ChatPromptViewManager(Activity activity) { this(activity, new String[]{"復(fù)制", "粘貼", "轉(zhuǎn)發(fā)"}, Location.TOP_LEFT); } public ChatPromptViewManager(Activity activity, Location location) { this(activity, new String[]{"復(fù)制", "粘貼", "轉(zhuǎn)發(fā)"}, location); } @Override public View inflateView() { return new PromptView(activity); } @Override public void bindData(View view, String[] dataArray) { if(view instanceof PromptView) { PromptView promptView = (PromptView) view; promptView.setContentArray(dataArray); promptView.setOnItemClickListener(new PromptView.OnItemClickListener() { @Override public void onItemClick(int position) { if(onItemClickListener != null) onItemClickListener.onItemClick(position); } }); } }}
實(shí)例化View,綁定數(shù)據(jù),添加事件都已經(jīng)完成了,下面就要計(jì)算View顯示的坐標(biāo)了,我這邊實(shí)現(xiàn)了幾個(gè)方法,貼出一個(gè)來(lái)看看,如果大家對(duì)位置有自己的需求可以自己來(lái)實(shí)現(xiàn)一個(gè)類(lèi)復(fù)寫(xiě)方法。
public class TopCenterLocation implements ICalculateLocation { @Override public int[] calculate(int[] srcViewLocation, View srcView, View promptView) { int[] location = new int[2]; int offset = (promptView.getWidth() - srcView.getWidth()) / 2; location[0] = srcViewLocation[0] - offset; location[1] = srcViewLocation[1] - promptView.getHeight(); return location; }}
總結(jié)
以上就是本文的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能有所幫助,如果有疑問(wèn)大家可以留言交流。
- android 彈出提示框的使用(圖文實(shí)例)
- Android使用Toast顯示消息提示框
- android實(shí)現(xiàn)彈出提示框
- Android編程之自定義AlertDialog(退出提示框)用法實(shí)例
- Android仿IOS自定義AlertDialog提示框
- Android仿百度谷歌搜索自動(dòng)提示框AutoCompleteTextView簡(jiǎn)單應(yīng)用示例
- Android超實(shí)用的Toast提示框優(yōu)化分享
- Android中仿IOS提示框的實(shí)現(xiàn)方法
- Android模擬美團(tuán)客戶端進(jìn)度提示框
- Android模仿Toast實(shí)現(xiàn)提示框效果
相關(guān)文章
Android中ListView的幾種常見(jiàn)的優(yōu)化方法總結(jié)
Android中的ListView應(yīng)該算是布局中幾種最常用的組件之一,本篇文章主要做了三種優(yōu)化總結(jié),有興趣的可以了解一下。2017-02-02Android使用Pull解析器解析xml文件的實(shí)現(xiàn)代碼
Android使用Pull解析器解析xml文件的實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-02-02Android開(kāi)發(fā)之動(dòng)畫(huà)實(shí)現(xiàn)方法
這篇文章主要介紹了Android開(kāi)發(fā)之動(dòng)畫(huà)實(shí)現(xiàn)方法,實(shí)例分析了Android中動(dòng)畫(huà)的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-05-05詳解Android .9.png “點(diǎn)九”圖片的使用
這篇文章主要為大家詳細(xì)介紹了Android .9.png “點(diǎn)九”圖片的使用方法,感興趣的小伙伴們可以參考一下2016-09-09Android編程應(yīng)用風(fēng)格和主題詳解
這篇文章主要介紹了Android編程應(yīng)用風(fēng)格和主題,較為詳細(xì)的分析了Android應(yīng)用風(fēng)格和主題的概念、功能、使用方法與注意事項(xiàng),需要的朋友可以參考下2016-10-10android端實(shí)現(xiàn)驗(yàn)證碼隨機(jī)生成功能
這篇文章主要為大家詳細(xì)介紹了android端實(shí)現(xiàn)驗(yàn)證碼隨機(jī)生成功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android高性能日志寫(xiě)入方案的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Android高性能日志寫(xiě)入方案的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01Android系統(tǒng)進(jìn)程間通信(IPC)機(jī)制Binder中的Server和Client獲得Service Manager接
本文主要介紹Android IPC通信Binder中的Server和Client獲得Service Manager接口,這里詳細(xì)的說(shuō)明了如何實(shí)現(xiàn)Service Manager接口,對(duì)研究Android源碼的朋友提供幫助,有需要的小伙伴可以參考下2016-08-08Android Gradle多渠道打包的實(shí)現(xiàn)方法
這篇文章主要介紹了Android Gradle多渠道打包的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Android利用ViewPager實(shí)現(xiàn)滑動(dòng)廣告板實(shí)例源碼
利用ViewPager我們可以做很多事情,從最簡(jiǎn)單的導(dǎo)航,到頁(yè)面切換菜單等等。ViewPager的功能就是可以使視圖滑動(dòng),就像Lanucher左右滑動(dòng)那樣2013-06-06