最近要在項(xiàng)目中實(shí)現(xiàn)一個(gè)長(zhǎng)按提示 “復(fù)制” 的功能腕唧,本來(lái)想偷懶在網(wǎng)上找個(gè)開源的項(xiàng)目用,但是看了好幾個(gè)都不是很滿意瘾英,所以就打算按照自己的思路來(lái)實(shí)現(xiàn)一個(gè)枣接。
如何使用
翠花,上代碼
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類缺谴,然后設(shè)置一個(gè)提示view管理器但惶,最后調(diào)用addPrompt方法添加view,此 view就是要添加提示框的view湿蛔。
思路
通過(guò)使用QQ,微信這個(gè)功能膀曾,感覺(jué)提示框使用PopupWindow應(yīng)該是可以滿足需求的。
所以大體的思路就是:
- View加載成功的時(shí)候給它添加長(zhǎng)按事件
- 用戶長(zhǎng)按的時(shí)候new一個(gè)PopupWindow阳啥,并且顯示它妓肢,并且設(shè)置點(diǎn)擊外部區(qū)域可以消失
架構(gòu)
為了讓上層調(diào)用簡(jiǎn)單,方便苫纤,我打算把提示框View封裝到一個(gè)類中碉钠,這個(gè)類包括:初始方法,綁定數(shù)據(jù),添加事件等等;基于這樣的考慮糯累,首先定義一個(gè)抽象類,然后讓具體的實(shí)現(xiàn)類來(lái)實(shí)現(xiàn)相應(yīng)的方法污筷,我們先來(lái)看看這個(gè)抽象類。
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è)抽象類中有一個(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)類
break;
case 2:
calculateLocation = ICalculateLocation 實(shí)現(xiàn)類
break;
TODO
}
}
這個(gè)枚舉對(duì)象里面包含了6種位置顯示類型叠殷,然后在構(gòu)造方法里面根據(jù)type類型會(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ì)算類瘪松,計(jì)算出不同位置的坐標(biāo)胸嘁,然后在顯示的時(shí)候new一個(gè)PopupWindow,通過(guò)PromtpViewManager得到提示框View設(shè)置給PopupWindow凉逛,再通過(guò)PromtpViewManager得到Location枚舉得到計(jì)算坐標(biāo)的類,調(diào)用calculate方法得到x群井,y坐標(biāo)状飞,然后通過(guò)PopupWindow的showAtLocation方法來(lái)顯示PopupWindow提示框。
具體實(shí)現(xiàn)
首先實(shí)現(xiàn)一個(gè)PromtpViewManager管理類
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è)類復(fù)寫方法样屠。
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;
}}
至此所有的已經(jīng)都分析完了
最后附上代碼地址:https://github.com/chenpengfei88/PromptView