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

為什么要使用ItemDecoration

移動開發(fā) Android
稀土掘金,這是一個針對技術(shù)開發(fā)者的一個應用,你可以在掘金上獲取最新最優(yōu)質(zhì)的技術(shù)干貨,不僅僅是Android知識、前端、后端以至于產(chǎn)品和設(shè)計都有涉獵,想成為全棧工程師的朋友不要錯過!

稀土掘金,這是一個針對技術(shù)開發(fā)者的一個應用,你可以在掘金上獲取******質(zhì)的技術(shù)干貨,不僅僅是Android知識、前端、后端以至于產(chǎn)品和設(shè)計都有涉獵,想成為全棧工程師的朋友不要錯過!

Part 1:不要用view做分割線

 

首先,什么是ItemDecoration?來看看官網(wǎng)是如何解釋的。

ItemDecoration允許從adapter的數(shù)據(jù)集合中為特定的item視圖添加特性的繪制以及布局間隔。它可以用來實現(xiàn)item之間的分割線,高亮,分組邊界等。

我們不能簡單的把ItemDecoration看成一個名字響亮的分割線。它比divider要多很多內(nèi)容。一個divider只能繪制在item之間,但是ItemDecoration可以繪制在item的四邊。ItemDecoration為decoration的測量和繪制提供了全方位的控制。一個decoration可以是一條分割線,也可以僅僅是一個間隔(inset)。

但不幸的是,絕大多數(shù)android開發(fā)者都沒有使用item decoration。在這個分為三部分的系列文章中,我們將了解ItemDecoration的強大之處。

***部分: 不要添加view來做分割線— 使用 ItemDecoration

第二部分: 不要使用padding來做間隔 —使用 ItemDecoration

第三部分: 在GridLayoutManager中高效的繪制decorations

本文是***部分。

不要用view做分割線 —會影響性能

我曾看到一些開發(fā)者在為RecyclerView添加divider的時候采用了一些捷徑。原因很簡單,ListView原生支持divider,可以直接在xml中設(shè)置divider。

  1. <ListView 
  2.  
  3.     android:id="@+id/activity_home_list_view" 
  4.  
  5.     android:layout_width="match_parent"  
  6.  
  7.     android:layout_height="match_parent" 
  8.  
  9.     android:divider="@android:color/black" 
  10.  
  11.     android:dividerHeight="8dp"/> 

但是到了RecyclerView,就再也不能直接添加divider了。需要添加一個繪制divider的ItemDecoration。但是開發(fā)者發(fā)現(xiàn)它很麻煩,于是直接把divider添加到(item的)view上,而不是使用ItemDecoration。

  1. <LinearLayout android:orientation="vertical"
  2.  
  3.     <LinearLayout android:orientation="horizontal"
  4.  
  5.         <ImageView /> 
  6.  
  7.         <TextView /> 
  8.  
  9.     </LinearLayout> 
  10.  
  11.     <View 
  12.  
  13.         android:width="match_parent" 
  14.  
  15.         android:height="1dp" 
  16.  
  17.         android:background="#333" /> 
  18.  
  19. </LinearLayout> 

每當我們走捷徑的時候,都有可能會產(chǎn)生副作用。而這里的副作用是可能影響性能。

當在布局中添加了一個divider的時候,我們增加了view的個數(shù)。我們都知道view的數(shù)目越少會得到越好的性能。有時候增加一個view來實現(xiàn)divider還會增加布局的層級。比如上面的例子中,我們不僅僅增加了一個view,還增加了一個包含它們的 linear layout。為了一個divider而創(chuàng)建了額外的布局。

不要用view做分割線 —會帶來副作用

因為divider是view的一部分,所以在item 動畫期間,divider也會一起跟著動畫。如下圖:

 

顯然divider不應該隨著item一起做動畫。而是和item分開,像這樣才是對的:

 

不要用view做分割線— 缺乏靈活性

如果divider是(item的)view的一部分,那么你就無法控制它。你唯一能控制的就是根據(jù)item的position改變divider的可見狀態(tài)。 而item decoration就靈活多了。

 

In the above image for the last item in the group divider fills the entire width. Other dividers have a margin of 56dp to their left side. Here is the ItemDecorator’s onDraw code.

在上圖中,group***一個item的divider充滿了整個寬度。其它的divider都有一個56dp的左邊距。這是這個ItemDecorator的onDraw代碼:

  1. @Override 
  2.  
  3. public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) { 
  4.  
  5.   canvas.save(); 
  6.  
  7.   final int leftWithMargin = convertDpToPixel(56); 
  8.  
  9.   final int right = parent.getWidth(); 
  10.  
  11.   
  12.  
  13.   final int childCount = parent.getChildCount(); 
  14.  
  15.   for (int i = 0; i < childCount; i++) { 
  16.  
  17.     final View child = parent.getChildAt(i); 
  18.  
  19.     int adapterPosition = parent.getChildAdapterPosition(child); 
  20.  
  21.     left = (adapterPosition == lastPosition) ?  0 : leftWithMargin; 
  22.  
  23.     parent.getDecoratedBoundsWithMargins(child, mBounds); 
  24.  
  25.     final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child)); 
  26.  
  27.     final int top = bottom - mDivider.getIntrinsicHeight(); 
  28.  
  29.     mDivider.setBounds(lefttopright, bottom); 
  30.  
  31.     mDivider.draw(canvas); 
  32.  
  33.   } 
  34.  
  35.   canvas.restore(); 
  36.  

不要用view做分割線—使用 ItemDecoration

寫一個自己的ItemDecoration其實非常簡單。你只需要創(chuàng)建一個繼承了ItemDecoration的類就可以了。重寫 getItemOffsets() 和 onDraw() 方法。具體實現(xiàn)可以參考 這個 示例。

而 25.0.0版本的支持庫中,我們有一個新的類 “DividerItemDecoration”。這個類直接實現(xiàn)了divider。

  1. DividerItemDecoration decoration = new DividerItemDecoration(getApplicationContext(), VERTICAL); 
  2.  
  3. recyclerView.addItemDecoration(decoration); 

提示

一個RecyclerView可以添加多個ItemDecoration。發(fā)揮頭腦風暴的時候到了。

所有decoration都在item繪制之前繪制。如果你想讓decoration在view之后繪制,重寫onDrawOver() 而不是onDraw() 。

所以下次想為RecyclerView添加分割線的時候,別使用在item布局添加view這種方式了,使用ItemDecoration。 

責任編輯:龐桂玉 來源: Android技術(shù)之家
相關(guān)推薦

2014-11-21 10:50:26

JavaString

2011-03-08 12:59:38

proftpd

2011-04-14 09:30:15

集合框架

2010-05-11 10:29:06

Unix awk

2014-05-19 15:52:57

Apache StraApache

2014-04-25 10:05:42

OpenStack私有云公共云

2024-01-24 11:24:03

C++編程異常處理

2013-09-27 11:33:57

交換機技術(shù)Vlan技術(shù)

2023-09-21 09:00:00

Merge Que開發(fā)工具Mergify

2014-01-03 10:59:34

2023-03-06 08:01:25

structGo語言

2024-01-01 08:57:55

ODBCSqlServer數(shù)據(jù)庫

2021-12-24 17:01:29

Linux工具系統(tǒng)

2022-04-26 16:56:20

行為數(shù)據(jù)數(shù)據(jù)

2020-08-24 07:03:10

物聯(lián)網(wǎng)設(shè)備RTOS物聯(lián)網(wǎng)

2018-05-01 06:43:33

2024-02-20 22:13:49

SQL語句編程

2021-02-18 09:23:47

數(shù)據(jù)庫分區(qū)數(shù)據(jù)庫倉庫

2023-11-29 09:19:00

WebhookURL

2018-11-02 14:00:20

點贊
收藏

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

主站蜘蛛池模板: 国产在线一区二区 | 青青久久久| 国产一区免费视频 | 91视视频在线观看入口直接观看 | 国产一区二区三区视频 | 精品日韩 | 久久久美女 | 精品久久久久久久人人人人传媒 | 天堂一区二区三区 | 毛片一级网站 | 国产精品精品久久久 | 欧美日韩国产一区 | 精品一区在线 | 播放一级黄色片 | 91亚洲精品久久久电影 | 国产精品视频入口 | 久久高清国产视频 | 在线国产一区二区三区 | 日韩成人影院在线观看 | 亚洲一区二区三区免费在线 | 国产一区二区三区四区hd | 欧美日韩在线精品 | 91精品久久久久久久久久入口 | 一区二区国产精品 | 久艹网站 | 三级特黄特色视频 | 欧美日韩精品一区 | 超级碰在线 | 521av网站| 免费人成在线观看网站 | 亚洲1区 | 成人亚洲视频 | 亚洲欧美激情视频 | 午夜精品视频在线观看 | 久久精品视频一区二区 | 中文字幕91av | 欧美黄色一区 | 欧美淫片| 亚洲精品一区二区二区 | 国产成人在线看 | 久久三级av |