自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

安卓當(dāng)下最流行的吸頂效果的實(shí)現(xiàn)(下)

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

接上文

***步:首先我們來寫一個(gè)類,它起標(biāo)記的作用,來放每一個(gè)item的對應(yīng)的懸浮欄的字符串

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

 

第二步:自定義一個(gè)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.         //設(shè)置懸浮欄的畫筆---paint   
  19.         paint = new Paint();   
  20.         paint.setColor(res.getColor(R.color.colorGray));   
  21.    
  22.         //設(shè)置懸浮欄中文本的畫筆   
  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.         //只有是同一組的***個(gè)才顯示懸浮欄   
  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.             //下一個(gè)和當(dāng)前不一樣移動當(dāng)前   
  108.             if (position + 1 < itemCount) {   
  109.                 String nextGroupId = callback.getGroupId(position + 1);   
  110.                 //組內(nèi)***一個(gè)view進(jìn)入了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.      * 判斷是不是組中的***個(gè)位置 
  126.      * 
  127.      * @param pos 
  128.      * @return 
  129.      */   
  130.     private boolean isFirstInGroup(int pos) {   
  131.         if (pos == 0) {   
  132.             return true;   
  133.         } else {   
  134.             // 因?yàn)槭歉鶕?jù) 字符串內(nèi)容的相同與否 來判斷是不是同意組的,所以此處的標(biāo)記id 要是String類型   
  135.             // 如果你只是做聯(lián)系人列表,懸浮框里顯示的只是一個(gè)字母,則標(biāo)記id直接用 int 類型就行了   
  136.             String prevGroupId = callback.getGroupId(pos - 1);   
  137.             String groupId = callback.getGroupId(pos);   
  138.             //判斷前一個(gè)字符串 與 當(dāng)前字符串 是否相同   
  139.             if (prevGroupId.equals(groupId)) {   
  140.                 return false;   
  141.             } else {   
  142.                 return true;   
  143.             }   
  144.         }   
  145.     }   
  146.    
  147.     //定義一個(gè)借口方便外界的調(diào)用   
  148.     interface DecorationCallback {   
  149.         String getGroupId(int position);   
  150.    
  151.         String getGroupFirstLine(int position);   
  152.     }   

 

第三步:在向list集合中先把每一個(gè)item的 起“標(biāo)記”作用的字符串都加進(jìn)去

  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.                //返回標(biāo)記id (即每一項(xiàng)對應(yīng)的標(biāo)志性的字符串)   
  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.                 //獲取同組中的***個(gè)內(nèi)容   
  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.             })); 

 

這樣就完成了~

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

責(zé)任編輯:龐桂玉 來源: 安卓開發(fā)精選
相關(guān)推薦

2017-01-13 11:10:41

Android吸頂效果開發(fā)

2022-07-28 14:33:32

webviewweb頁面

2023-10-11 08:14:43

iPhoneTabs標(biāo)簽頁

2020-08-19 10:22:45

CIOIT試點(diǎn)項(xiàng)目技術(shù)

2011-05-03 10:40:58

Ubuntu 11.0應(yīng)用

2018-01-31 11:10:21

安卓操作系統(tǒng)手機(jī)屏幕

2014-02-04 19:44:23

編程語言開發(fā)

2014-02-19 10:34:48

JavaScript代碼規(guī)范

2021-07-13 06:51:16

H5web開發(fā)吸頂

2011-03-21 13:01:10

2011-01-04 18:04:49

PHP

2010-07-20 09:49:07

分布式文件系統(tǒng)

2017-06-27 14:02:09

前端框架Bootstrap

2018-03-13 09:34:30

人工智能編程語言Python

2021-07-28 14:25:01

編程語言開發(fā)JavaScript

2023-03-31 11:21:10

網(wǎng)絡(luò)協(xié)議LoRaWAN

2013-11-11 11:34:56

UbuntuLinux發(fā)行版Unity

2017-07-14 14:50:00

架構(gòu)框架前端

2016-10-21 17:13:16

開發(fā)Java

2017-01-15 17:44:27

Java EE服務(wù)器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號