国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-12-12 04:46:39
字體:
來源:轉載
供稿:網友

先來看看效果圖

如何使用

示例代碼

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

使用起來還是很簡單的

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

實現思路

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

所以大體的思路就是:

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

    2、用戶長按的時候new一個PopupWindow,并且顯示它,并且設置點擊外部區域可以消失
架構

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

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 實現類   break;   case 2:   calculateLocation = ICalculateLocation 實現類    break;   TODO  }  }

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

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

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

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

具體實現

首先實現一個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[]{"復制", "粘貼", "轉發"}, Location.TOP_LEFT);  }  public ChatPromptViewManager(Activity activity, Location location) {   this(activity, new String[]{"復制", "粘貼", "轉發"}, 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,綁定數據,添加事件都已經完成了,下面就要計算View顯示的坐標了,我這邊實現了幾個方法,貼出一個來看看,如果大家對位置有自己的需求可以自己來實現一個類復寫方法。

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

總結

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 屯门区| 万盛区| 福清市| 孝感市| 阿克| 兴隆县| 江津市| 潼南县| 班玛县| 桦南县| 安义县| 上蔡县| 永寿县| 临潭县| 瓮安县| 蓝山县| 德庆县| 广灵县| 临沭县| 阿图什市| 兴仁县| 乡宁县| 原阳县| 卓尼县| 罗定市| 从江县| 梅州市| 舟山市| 武宁县| 新绛县| 贺兰县| 攀枝花市| 万山特区| 涟水县| 华池县| 龙口市| 百色市| 黄梅县| 百色市| 张家港市| 栾川县|