閱讀129 返回首頁    go 阿裏雲 go 技術社區[雲棲]


仿今日頭條滑動評論效果

開發中碰到問題之後實現的,覺得可能有的開發者用的到或則希望獨立成一個小功能DEMO,所以就放出來這麼一個DEMO。

原本覺得是最後完成後發網站客戶端的,可是這樣體現不出一個功能一個功能的分析實現效果,而且周期時間長,所以就完成一部分,發一部分,敬請諒解。


下麵的菜單彈出效果在很多的新聞閱讀器上都有,比如今日頭條、360新聞等。下

其實這個實現起來很簡單,看其效果,其實就是一個PopupWindow,之後設定相應postion的按鈕點擊屬性,之後獲取按鈕的位置,給它設置動畫顯示消失就可以出現了。

下麵看看代碼的思路:

由於整體是一個LISTVIEW,所以我把點擊的事件寫到了對應的Adapter適配器中。

  1. public class MyAdapter extends BaseAdapter {  
  2.     LayoutInflater inflater = null;  
  3.     Activity activity;  
  4.     ArrayList<News> newslist;  
  5.     private PopupWindow popupWindow;  
  6.   
  7.     public MyAdapter(Activity activity, ArrayList<News> newslist) {  
  8.         this.activity = activity;  
  9.         this.newslist = newslist;  
  10.         inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  11.         initPopWindow();  
  12.     }  
  13.   
  14.     @Override  
  15.     public int getCount() {  
  16.         return newslist != null ? newslist.size() : 0;  
  17.     }  
  18.   
  19.     @Override  
  20.     public News getItem(int position) {  
  21.         if (newslist != null && newslist.size() != 0) {  
  22.             return newslist.get(position);  
  23.         }  
  24.         return null;  
  25.     }  
  26.   
  27.     @Override  
  28.     public long getItemId(int position) {  
  29.         return position;  
  30.     }  
  31.   
  32.     @Override  
  33.     public View getView(final int position, View convertView, ViewGroup parent) {  
  34.         View vi = convertView;  
  35.         final ViewHolder holder;  
  36.         if (vi == null) {  
  37.             vi = inflater.inflate(R.layout.listview_item, null);  
  38.             holder = new ViewHolder();  
  39.             holder.item_title = (TextView) vi.findViewById(R.id.item_title);  
  40.             holder.item_content = (TextView) vi.findViewById(R.id.item_content);  
  41.             holder.button_showpop = (ImageView) vi.findViewById(R.id.button_showpop);  
  42.             vi.setTag(holder);  
  43.         } else {  
  44.             holder = (ViewHolder) vi.getTag();  
  45.         }  
  46.         News news = getItem(position);  
  47.         holder.item_title.setText(news.getTitle());  
  48.         holder.item_content.setText(news.getContent());  
  49.         holder.button_showpop .setOnClickListener(new popAction(position));  
  50.         return vi;  
  51.     }  
  52.   
  53.     public class ViewHolder {  
  54.         TextView item_title;  
  55.         TextView item_content;  
  56.         ImageView button_showpop;  
  57.     }  
  58.       
  59.     /**  
  60.      * 初始化popWindow 
  61.      * */  
  62.     private void initPopWindow() {  
  63.         View popView = inflater.inflate(R.layout.listview_pop, null);  
  64.         popupWindow = new PopupWindow(popView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  65.         popupWindow.setBackgroundDrawable(new ColorDrawable(0));  
  66.         //設置popwindow出現和消失動畫  
  67.         popupWindow.setAnimationStyle(R.style.PopMenuAnimation);  
  68.         btn_pop_close = (ImageView) popView.findViewById(R.id.btn_pop_close);  
  69.     }  
  70.       
  71.     /** popWindow 關閉按鈕 */  
  72.     private ImageView btn_pop_close;  
  73.       
  74.     /**  
  75.      * 顯示popWindow 
  76.      * */  
  77.     public void showPop(View parent, int x, int y,int postion) {  
  78.         //設置popwindow顯示位置  
  79.         popupWindow.showAtLocation(parent, 0, x, y);  
  80.         //獲取popwindow焦點  
  81.         popupWindow.setFocusable(true);  
  82.         //設置popwindow如果點擊外麵區域,便關閉。  
  83.         popupWindow.setOutsideTouchable(true);  
  84.         popupWindow.update();  
  85.         if (popupWindow.isShowing()) {  
  86.               
  87.         }  
  88.         btn_pop_close.setOnClickListener(new OnClickListener() {  
  89.             public void onClick(View paramView) {  
  90.                 popupWindow.dismiss();  
  91.             }  
  92.         });  
  93.     }  
  94.       
  95.     /**  
  96.      * 每個ITEM中more按鈕對應的點擊動作 
  97.      * */  
  98.     public class popAction implements OnClickListener{  
  99.         int position;  
  100.         public popAction(int position){  
  101.             this.position = position;  
  102.         }  
  103.         @Override  
  104.         public void onClick(View v) {  
  105.             int[] arrayOfInt = new int[2];  
  106.             //獲取點擊按鈕的坐標  
  107.             v.getLocationOnScreen(arrayOfInt);  
  108.             int x = arrayOfInt[0];  
  109.             int y = arrayOfInt[1];  
  110.             showPop(v, x , y, position);  
  111.         }  
  112.     }  
  113. }  
就這麼多的內容,很簡單,日後碰到這類相關的效果,也就不用怕了。

下麵是我經過上述代碼實現的效果:



下麵放上該效果源碼DEMO的下載地址:下載地址

最後更新:2017-04-03 12:56:16

  上一篇:go 仿火車出票效果
  下一篇:go 談談互聯網創業未來的發展方向我們如何把握