Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

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

編輯:關於 Android 程式設計

先來看看效果圖

如何使用

示例代碼

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

調用起來非常輕量:先 new 一個 PromptViewHelper,再塞進一個提示框管理器,最後調 addPrompt 把需要長按的 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 是個枚舉,裡面裝著幾種位置信息:

 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 坐標;不同位置對應不同的實現類。

到這裡整體架構就清晰了:用 PromptViewManager 負責加載提示框、綁定數據與事件;通過 Location 枚舉選擇合適的坐標計算類;顯示時 new 一個 PopupWindow,把 PromptViewManager 提供的 View 塞進去,再讓 Location 對應的計算類算出 x、y,最後調 showAtLocation 展示。

具體實現

先實現 PromptViewManager 的具體管理類:

 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 實例化、數據綁定、事件綁定都完成了,接下來算坐標。下面以「頂部居中」為例,其他位置同理;如果項目的顯示規則比較特殊,自己實現 ICalculateLocation 即可。

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

把「管理器 + 枚舉 + 計算接口」這三層拆開之後,後續要加新的彈出位置或自定義菜單樣式,都不用動原有邏輯,擴展起來很輕鬆。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved