欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android仿QQ、微信聊天界面長按提示框效果

 更新時間:2016年11月06日 15:17:08   作者:偽文藝大叔  
最近在工作項目中要實現(xiàn)一個長按提示 “復(fù)制” 的功能,類似于QQ、微信聊天界面長按提示框效果,本來想偷懶在網(wǎng)上找個開源的項目用,但是看了好幾個都不是很滿意,所以就打算按照自己的思路來實現(xiàn)一個。下面分享給大家,有需要的朋友們可以參考借鑒。

先來看看效果圖

如何使用

示例代碼

PromptViewHelper pvHelper = new PromptViewHelper(mActivity);
pvHelper.setPromptViewManager(new ChatPromptViewManager(mActivity));
pvHelper.addPrompt(holder.itemView.findViewById(R.id.textview_content));

使用起來還是很簡單的

首先new一個PromptViewHelper類,然后設(shè)置一個提示view管理器,最后調(diào)用addPrompt方法添加view,此 view就是要添加提示框的view。

實現(xiàn)思路

通過使用QQ,微信這個功能,感覺提示框使用PopupWindow應(yīng)該是可以滿足需求的。

所以大體的思路就是:

    1、View加載成功的時候給它添加長按事件

    2、用戶長按的時候new一個PopupWindow,并且顯示它,并且設(shè)置點擊外部區(qū)域可以消失
架構(gòu)

為了讓上層調(diào)用簡單,方便,我打算把提示框View封裝到一個類中,這個類包括:初始方法,綁定數(shù)據(jù),添加事件等等;基于這樣的考慮,首先定義一個抽象類,然后讓具體的實現(xiàn)類來實現(xiàn)相應(yīng)的方法,我們先來看看這個抽象類。

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; 
 }
 }

注意:在一個抽象類中有一個Location對象的屬性,這個Location是做什么的個,因為我們在顯示這個提示框View的時候會要考慮它顯示的位置,這個Location是個枚舉對象,它里面就包括了一些位置的信息;

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 實現(xiàn)類  
 break;  

 case 2:  
 calculateLocation = ICalculateLocation 實現(xiàn)類   
 break;  

 TODO 
 } 
 }

這個枚舉對象里面包含了6種位置顯示類型,然后在構(gòu)造方法里面根據(jù)type類型會實例化一個ICalculateLocation 對象,ICalculateLocation 是什么呢?

public interface ICalculateLocation { 
 int[] calculate(int[] srcViewLocation, View srcView, View promptView);
}

它是一個接口,提供了一個calculate方法來計算提示框View的x,y坐標,我們通過實現(xiàn)這個接口來計算不同位置的坐標。

到這,大體的架構(gòu)已經(jīng)出來了;首先我們定義一個PromtpViewManager管理器來來實現(xiàn)提示框View的加載,綁定數(shù)據(jù),添加事件,然后通過設(shè)置的Location枚舉來實現(xiàn)不同的計算類,計算出不同位置的坐標,然后在顯示的時候new一個PopupWindow,通過PromtpViewManager得到提示框View設(shè)置給PopupWindow,再通過PromtpViewManager得到Location枚舉得到計算坐標的類,調(diào)用calculate方法得到x,y坐標,然后通過PopupWindowshowAtLocation方法來顯示PopupWindow提示框。

具體實現(xiàn)

首先實現(xiàn)一個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); 
    }  
   }); 
 } 
 }}

實例化View,綁定數(shù)據(jù),添加事件都已經(jīng)完成了,下面就要計算View顯示的坐標了,我這邊實現(xiàn)了幾個方法,貼出一個來看看,如果大家對位置有自己的需求可以自己來實現(xiàn)一個類復(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; 
 }}

總結(jié)

以上就是本文的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能有所幫助,如果有疑問大家可以留言交流。

相關(guān)文章

最新評論