本文同步自http://javaexception.com/archives/77html
在上一篇文章中,給出了一種複製QQ效果的方案,今天就來說講換一種方式實現。主要依賴的是一個開源項目https://github.com/shangmingchao/PopupList。java
PopupList.java的代碼封裝的比較完善,用純java代碼實現view效果,不須要使用圖片,xml資源文件,引入的話,只須要copy PopupList.java代碼到項目工程中。git
剩下的就是調用了。這裏不分析源碼,源碼比較簡單,只講如何使用的。github
PopupList popupList = new PopupList(this); List<String> popupMenuItemList = new ArrayList<>(Arrays.asList("複製QQ號")); popupList.bind(tvQQNum, popupMenuItemList, new PopupList.PopupListListener() { @Override public boolean showPopupList(View adapterView, View contextView, int contextPosition) { return true; } @Override public void onPopupListClick(View contextView, int contextPosition, int position) { ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipData clipData = ClipData.newPlainText("Label", "10001"); cm.setPrimaryClip(clipData); } });
PopupList popupList = new PopupList(this); List<String> popupMenuItemList = new ArrayList<>(Arrays.asList("複製")); popupList.bind(tvUserName, popupMenuItemList, new PopupList.PopupListListener() { @Override public boolean showPopupList(View adapterView, View contextView, int contextPosition) { return true; } @Override public void onPopupListClick(View contextView, int contextPosition, int position) { ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipData clipData = ClipData.newPlainText("Label", "每天"); cm.setPrimaryClip(clipData); } });
用法很簡單。PopupList支持單個,也支持數組形式的結構,如朋友圈點讚的那種效果等。數組