リストを引っ張って更新する機能jetpackで提供されています。
Swiperefreshlayoutが該当のクラスです。
使い方は以下です。
1.アプリケーションレベルのbuild.gradleに以下を追加する。
build.gradle
dependencies {
//~~中略~~
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
//~~中略~~
}
2.レイアウトに追加し、チャイルドビューにRecyclerViewをセットする
layout/my_fragment.xml
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swiped_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/adView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
3.コールバックをセットする
my_fragment.java
private final SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//更新処理
//更新マークを非表示にする
mSwipeRefreshLayout.setRefreshing(false);
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//~~中略~~
SwipeRefreshLayout swipeRefreshLayout = mView.findViewById(R.id.swiped_layout);
//更新時のコールバックをセットする
swipeRefreshLayout.setOnRefreshListener(mOnRefreshListener);
//プログレスアイコンの色をセットする
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.TRANSPARENT);
swipeRefreshLayout.setColorSchemeColors(Color.TRANSPARENT);
//~~中略~~
}

0 件のコメント :
コメントを投稿