今天看到一個和支付寶加載RecyclerView顯示加載中動畫類似的庫,感覺很有意思,參照官網說明寫一個粗淺的使用說明給大家分享下。
項目簡介
ShimmerRecyclerView
一個自定義循環視圖,帶有shimmer視圖,用來表現視圖正在加載中。這個循環視圖有一個內置的適配器來控制shimmer性能并提供兩個方法:
- showShimmerAdapter() - 設置一個demo(加載中)適配器,顯示預設子demo視圖的數量。
- hideShimmerAdapter() -隱藏加載中視圖,恢復適配器以顯示實際的子元素.
具體的顯示效果如下:
List Demo | Grid Demo |
---|---|
屬性和方法
按如下屬性和方法初始化demo視圖.
XML 屬性 | Java 方法 | Explanation |
---|---|---|
app:demo_child_count | setDemoChildCount(int) | 在shimmer適配器中展現設置demo視圖的數量(Integer)。 |
app:demo_layout | setDemoLayoutReference(int) | 定義my_demo_view.xml ,請參考布局[1]。 |
app:demo_layout_manager_type | setDemoLayoutManager(LayoutManagerType) | 演示視圖布局管理。 |
[1] 一個顯示List Demo的 my_demo_view.xml(加載中) 的例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="4dp"
android:background="@drawable/bg_card"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@android:color/white"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="78dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="8dp" >
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginBottom="3dp"
android:alpha="0.1"
android:background="@android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:alpha="0.1"
android:background="@android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:alpha="0.1"
android:background="@android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:alpha="0.1"
android:background="@android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:alpha="0.1"
android:background="@android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:alpha="0.1"
android:background="@android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginTop="3dp"
android:alpha="0.1"
android:background="@android:color/background_dark" />
</LinearLayout>
<View
android:layout_width="78dp"
android:layout_height="78dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:alpha="0.1"
android:background="@android:color/background_dark" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="6dp"
android:alpha="0.1"
android:background="@android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:alpha="0.1"
android:background="@android:color/background_dark" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:alpha="0.1"
android:background="@android:color/background_dark" />
</LinearLayout>
</LinearLayout>
[1.1] @drawable/bg_card:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="3dp" />
<solid android:color="@android:color/white" />
<stroke
android:width="1dp"
android:color="#d6d6d6" />
</shape>
使用方法
1.將本庫添加到你的項目中
在你的項目級build.gradle文件中添加下列設置:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
module下build.gradle文件中添加依賴:
dependencies {
compile 'com.github.sharish:ShimmerRecyclerView:v1.0'
}
2.定義你的xml:
<com.cooltechworks.views.shimmer.ShimmerRecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/shimmer_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:demo_child_count="10"
app:demo_grid_child_count="2"
app:demo_layout="@layout/layout_demo_grid"
app:demo_layout_manager_type="grid" />
@layout/layout_demo_grid指在加載spinner時展現的示例布局(參考[1])。
3.在你的Activity onCreate()方法中,初始化shimmer如下:
ShimmerRecyclerView shimmerRecycler = (ShimmerRecyclerView) findViewById(R.id.shimmer_recycler_view);
shimmerRecycler.setLayoutManager(layoutManager);
shimmerRecycler.setAdapter(mAdapter);
//顯示加載中
shimmerRecycler.showShimmerAdapter();
4.耗時操作后顯示真正要顯示的內容,然后隱藏加載中:
shimmerRecycler.hideShimmerAdapter();
其余操作和RecyclerView一樣,要完成上拉加載下拉刷新,點擊事件等同RecyclerView可以在Adapter中完成。