# 關(guān)于RecycleView的overScrollMode屬性的一點探索
這個屬性本身沒有特別重要的功能,只是針對滑動做的一個動效,算是錦上添花的一個東西吧。
這個屬性是在Sdk 21之后添加的,故在21之前的版本上無法顯示。
## 1. 屬性設(shè)置后的效果:
在RecycleView可以滑動的時候,如果滑動到頂部或者底部,會顯示一個過度滑動的動畫效果。這個屬性默認(rèn)設(shè)置為always,可以手動修改。
## 2. 在xml中設(shè)置:
```xml
? ? ? ? <androidx.recyclerview.widget.RecyclerView
? ? ? ? ? ? android:id="@+id/rv"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:overScrollMode="always"
? ? ? ? ? ? android:scrollbars="none"
? ? ? ? ? ? tools:itemCount="5"
? ? ? ? ? ? tools:listitem="@layout/item_rv" />
```
## 3. 在代碼中設(shè)置:
mode 共有三種模式可以設(shè)置,分別是:
1. **OVER_SCROLL_ALWAYS**( Always allow a user to over-scroll this view, provided it is a view that can scroll. )
2. **OVER_SCROLL_IF_CONTENT_SCROLLS** ( Allow a user to over-scroll this view only if the content is large enough to meaningfully scroll, provided it is a view that can scroll )
3. **OVER_SCROLL_NEVER** ( Never allow a user to over-scroll this view )
-----
1. Kotlin
```kotlin
mDataBinding.rv.overScrollMode = View.OVER_SCROLL_ALWAYS
```
2. Java
```java
recyclerView.setOverScrollMode(View.OVER_SCROLL_ALWAYS);
```
## 4. 修改EdgeEffect的顏色
如果不手動設(shè)置顏色,默認(rèn)使用App的主題色,即 colorPrimary 。
如需修改顏色,可以通過設(shè)置style的方式來修改顏色。
``` xml
<item name="android:colorEdgeEffect">@android:color/holo_red_light</item>
```
這里就是把顯示的顏色修改為紅色。
項目相關(guān)源碼已經(jīng)上傳github,
[點擊跳轉(zhuǎn)]: https://github.com/luckylicy/RecycleViewOverScrollModeDemo