成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

安卓當下最流行的吸頂效果的實現(下)

移動開發 Android
今天接著上次讓我使用ItemDecoration來完成可推動的懸浮導航欄的效果。

接上文

***步:首先我們來寫一個類,它起標記的作用,來放每一個item的對應的懸浮欄的字符串

  1. public class NameBean {   
  2.     String name;   
  3.    
  4.     public String getName() {   
  5.         return name;   
  6.     }   
  7.    
  8.     public void setName(String name) {   
  9.         this.name = name;   
  10.     }   

 

第二步:自定義一個SectionDecoration 類 繼承 RecyclerView的ItemDecoration

  1. public class SectionDecoration extends RecyclerView.ItemDecoration {   
  2.     private static final String TAG = "SectionDecoration";   
  3.    
  4.     private List<NameBean> dataList;   
  5.    
  6.     private DecorationCallback callback;   
  7.     private TextPaint textPaint;   
  8.     private Paint paint;   
  9.     private int topGap;   
  10.     private int alignBottom;   
  11.     private Paint.FontMetrics fontMetrics;   
  12.    
  13.    
  14.     public SectionDecoration(List<NameBean> dataList, Context context, DecorationCallback decorationCallback) {   
  15.         Resources res = context.getResources();   
  16.         this.dataList = dataList;   
  17.         this.callback = decorationCallback;   
  18.         //設置懸浮欄的畫筆---paint   
  19.         paint = new Paint();   
  20.         paint.setColor(res.getColor(R.color.colorGray));   
  21.    
  22.         //設置懸浮欄中文本的畫筆   
  23.         textPaint = new TextPaint();   
  24.         textPaint.setAntiAlias(true);   
  25.         textPaint.setTextSize(DensityUtil.dip2px(context, 14));   
  26.         textPaint.setColor(Color.DKGRAY);   
  27.         textPaint.setTextAlign(Paint.Align.LEFT);   
  28.         fontMetrics = new Paint.FontMetrics();   
  29.         //決定懸浮欄的高度等   
  30.         topGap = res.getDimensionPixelSize(R.dimen.sectioned_top);   
  31.         //決定文本的顯示位置等   
  32.         alignBottom = res.getDimensionPixelSize(R.dimen.sectioned_alignBottom);   
  33.     }   
  34.    
  35.     @Override   
  36.     public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {   
  37.         super.getItemOffsets(outRect, view, parent, state);   
  38.         int pos = parent.getChildAdapterPosition(view);   
  39.         Log.i(TAG, "getItemOffsets:" + pos);   
  40.         String groupId = callback.getGroupId(pos);   
  41.         if (groupId.equals("-1")) return;   
  42.         //只有是同一組的***個才顯示懸浮欄   
  43.         if (pos == 0 || isFirstInGroup(pos)) {   
  44.             outRect.top = topGap;   
  45.             if (dataList.get(pos).getName() == "") {   
  46.                 outRect.top = 0;   
  47.             }   
  48.         } else {   
  49.             outRect.top = 0;   
  50.         }   
  51.     }   
  52.    
  53.     @Override   
  54.     public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {   
  55.         super.onDraw(c, parent, state);   
  56.         int left = parent.getPaddingLeft();   
  57.         int right = parent.getWidth() - parent.getPaddingRight();   
  58.         int childCount = parent.getChildCount();   
  59.         for (int i = 0; i < childCount; i++) {   
  60.             View view = parent.getChildAt(i);   
  61.             int position = parent.getChildAdapterPosition(view);   
  62.             String groupId = callback.getGroupId(position);   
  63.             if (groupId.equals("-1")) return;   
  64.             String textLine = callback.getGroupFirstLine(position).toUpperCase();   
  65.             if (textLine == "") {   
  66.                 float top = view.getTop();   
  67.                 float bottom = view.getTop();   
  68.                 c.drawRect(lefttopright, bottom, paint);   
  69.                 return;   
  70.             } else {   
  71.                 if (position == 0 || isFirstInGroup(position)) {   
  72.                     float top = view.getTop() - topGap;   
  73.                     float bottom = view.getTop();   
  74.                     //繪制懸浮欄   
  75.                     c.drawRect(lefttop - topGap, right, bottom, paint);   
  76.                     //繪制文本   
  77.                     c.drawText(textLine, left, bottom, textPaint);   
  78.                 }   
  79.             }   
  80.         }   
  81.     }   
  82.    
  83.     @Override   
  84.     public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {   
  85.         super.onDrawOver(c, parent, state);   
  86.         int itemCount = state.getItemCount();   
  87.         int childCount = parent.getChildCount();   
  88.         int left = parent.getPaddingLeft();   
  89.         int right = parent.getWidth() - parent.getPaddingRight();   
  90.         float lineHeight = textPaint.getTextSize() + fontMetrics.descent;   
  91.    
  92.         String preGroupId = "";   
  93.         String groupId = "-1";   
  94.         for (int i = 0; i < childCount; i++) {   
  95.             View view = parent.getChildAt(i);   
  96.             int position = parent.getChildAdapterPosition(view);   
  97.    
  98.             preGroupId = groupId;   
  99.             groupId = callback.getGroupId(position);   
  100.             if (groupId.equals("-1") || groupId.equals(preGroupId)) continue;   
  101.    
  102.             String textLine = callback.getGroupFirstLine(position).toUpperCase();   
  103.             if (TextUtils.isEmpty(textLine)) continue;   
  104.    
  105.             int viewBottom = view.getBottom();   
  106.             float textY = Math.max(topGap, view.getTop());   
  107.             //下一個和當前不一樣移動當前   
  108.             if (position + 1 < itemCount) {   
  109.                 String nextGroupId = callback.getGroupId(position + 1);   
  110.                 //組內***一個view進入了header   
  111.                 if (nextGroupId != groupId && viewBottom < textY) {   
  112.                     textY = viewBottom;   
  113.                 }   
  114.             }   
  115.             //textY - topGap決定了懸浮欄繪制的高度和位置   
  116.             c.drawRect(left, textY - topGap, right, textY, paint);   
  117.             //left+2*alignBottom 決定了文本往左偏移的多少(加-->向左移)   
  118.             //textY-alignBottom  決定了文本往右偏移的多少  (減-->向上移)   
  119.             c.drawText(textLine, left + 2 * alignBottom, textY - alignBottom, textPaint);   
  120.         }   
  121.     }   
  122.    
  123.    
  124.     /** 
  125.      * 判斷是不是組中的***個位置 
  126.      * 
  127.      * @param pos 
  128.      * @return 
  129.      */   
  130.     private boolean isFirstInGroup(int pos) {   
  131.         if (pos == 0) {   
  132.             return true;   
  133.         } else {   
  134.             // 因為是根據 字符串內容的相同與否 來判斷是不是同意組的,所以此處的標記id 要是String類型   
  135.             // 如果你只是做聯系人列表,懸浮框里顯示的只是一個字母,則標記id直接用 int 類型就行了   
  136.             String prevGroupId = callback.getGroupId(pos - 1);   
  137.             String groupId = callback.getGroupId(pos);   
  138.             //判斷前一個字符串 與 當前字符串 是否相同   
  139.             if (prevGroupId.equals(groupId)) {   
  140.                 return false;   
  141.             } else {   
  142.                 return true;   
  143.             }   
  144.         }   
  145.     }   
  146.    
  147.     //定義一個借口方便外界的調用   
  148.     interface DecorationCallback {   
  149.         String getGroupId(int position);   
  150.    
  151.         String getGroupFirstLine(int position);   
  152.     }   

 

第三步:在向list集合中先把每一個item的 起“標記”作用的字符串都加進去

  1. setPullAction(comingslist); 
  1. private void setPullAction(List<WaitMVBean.DataBean.ComingBean> comingslist) {   
  2.         dataList = new ArrayList<>();   
  3.    
  4.         for (int i = 0; i < comingslist.size(); i++) {   
  5.             NameBean nameBean = new NameBean();   
  6.             String name0 = comingslist.get(i).getComingTitle();   
  7.             nameBean.setName(name0);   
  8.             dataList.add(nameBean);   
  9.         }   
  10.     } 

 

第四步:在setAdapter() 前,為RecyclerView添加ItemDecoration:

  1. recyclerView.addItemDecoration(new SectionDecoration(dataList,mContext, new SectionDecoration.DecorationCallback() {   
  2.                //返回標記id (即每一項對應的標志性的字符串)   
  3.                 @Override   
  4.                 public String getGroupId(int position) {   
  5.                     if(dataList.get(position).getName()!=null) {   
  6.                         return dataList.get(position).getName();   
  7.                     }   
  8.                     return "-1";   
  9.                 }   
  10.    
  11.                 //獲取同組中的***個內容   
  12.                 @Override   
  13.                 public String getGroupFirstLine(int position) {   
  14.                     if(dataList.get(position).getName()!=null) {   
  15.                         return dataList.get(position).getName();   
  16.                     }   
  17.                     return "";   
  18.                 }   
  19.             })); 

 

這樣就完成了~

再看一眼最終效果感受一下:

責任編輯:龐桂玉 來源: 安卓開發精選
相關推薦

2017-01-13 11:10:41

Android吸頂效果開發

2022-07-28 14:33:32

webviewweb頁面

2020-08-19 10:22:45

CIOIT試點項目技術

2011-05-03 10:40:58

Ubuntu 11.0應用

2023-10-11 08:14:43

iPhoneTabs標簽頁

2014-02-04 19:44:23

編程語言開發

2014-02-19 10:34:48

JavaScript代碼規范

2018-01-31 11:10:21

安卓操作系統手機屏幕

2011-03-21 13:01:10

2011-01-04 18:04:49

PHP

2010-07-20 09:49:07

分布式文件系統

2018-03-13 09:34:30

人工智能編程語言Python

2023-03-31 11:21:10

網絡協議LoRaWAN

2021-07-28 14:25:01

編程語言開發JavaScript

2017-06-27 14:02:09

前端框架Bootstrap

2021-07-13 06:51:16

H5web開發吸頂

2013-11-11 11:34:56

UbuntuLinux發行版Unity

2017-07-14 14:50:00

架構框架前端

2016-10-21 17:13:16

開發Java

2019-01-30 12:38:41

JavaScript前端編程語言
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 一区二区三区四区在线 | 国产精品1区2区 | 国产亚洲精品美女久久久久久久久久 | 日韩精品一区在线观看 | 国产在线观看一区二区 | 亚洲精品福利视频 | 国产精品日韩一区二区 | 日韩欧美精品一区 | 国产91久久久久蜜臀青青天草二 | 涩涩视频在线播放 | 欧美日一区二区 | 美女视频黄色片 | 日本一道本 | m豆传媒在线链接观看 | 欧美三级免费观看 | 亚洲精品一区二区三区蜜桃久 | 91久久精品国产91久久 | 国产在线视频在线观看 | 日韩中文字幕视频在线观看 | 免费观看成人av | 日韩一区二区黄色片 | 久久精品国产亚洲 | 成人影院在线视频 | 美女毛片免费看 | 精品一区二区三区电影 | 国产免费一区二区三区 | 国产精品免费av | 综合色播| 日韩久久中文字幕 | 国产成人综合一区二区三区 | www.啪啪.com| 男女羞羞视频在线 | 国产一级一级毛片 | 精品欧美一区二区三区 | 久久福利电影 | 国产成人精品久久二区二区91 | 99精品99 | 亚洲一区二区免费电影 | 黄色在线播放视频 | 4hu最新网址 | 在线观看午夜视频 |