1、源码github网址:https://github.com/lcodecorex/TwinklingRefreshLayout/
2、使用步骤:
2.1 添加依赖:
implementation 'com.lcodecorex:tkrefreshlayout:1.0.7'
2.2 布局中引入TwinklingRefreshLayout,包裹着RecyclerView
2.3 根据id找到 TwinklingRefreshLayout
// 找到刷新控件
mRefreshLayout = mRootView.findViewById(R.id.refresh_layout);
// 设置头部
BezierLayout headerView = new BezierLayout(this);
mRefreshLayout.setHeaderView(headerView);
mRefreshLayout.setMaxHeadHeight(140);
// 设置尾部
BallPulseView ballPulseView = new BallPulseView(getContext());
ballPulseView.setNormalColor(getContext().getColor(R.color.colorPrimaryDark));
ballPulseView.setAnimatingColor(getContext().getColor(R.color.colorPrimaryDark));
mRefreshLayout.setBottomView(ballPulseView);
// 设置监听方法
mRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
// 下拉刷新方法
@Override
public void onRefresh(TwinklingRefreshLayout refreshLayout) {
super.onRefresh(refreshLayout);
BaseApplication.getHandler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(RecommendDetailActivity.this, "下拉刷新",
Toast.LENGTH_SHORT).show();
mRefreshLayout.finishRefreshing();
}
}, 2000);
}
// 上拉加载更多方法
@Override
public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
super.onLoadMore(refreshLayout);
BaseApplication.getHandler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(RecommendDetailActivity.this, "加载更多",
Toast.LENGTH_SHORT).show();
mRefreshLayout.finishLoadmore();
}
}, 2000);
}
});
给RecyclerView添加上拉下拉回弹效果
1.布局中引入TwinklingRefreshLayout
<com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
android:id="@+id/over_bound_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recommend_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff4f4f4"
android:overScrollMode="never" />
</com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout>
2.找到TwinklingRefreshLayout控件
TwinklingRefreshLayout twinklingRefreshLayout = mRootView.findViewById(R.id.over_bound_view);
3.设置回弹效果
// 给recyclerView设置上拉/下拉回弹效果
twinklingRefreshLayout.setPureScrollModeOn();