刷新框架TwinklingRefreshLayout的使用

本文介绍了如何在Android应用中利用TwinklingRefreshLayout库为RecyclerView添加上拉下拉回弹效果,包括添加依赖、配置刷新和加载更多监听,以及设置头部和尾部视图。通过示例代码指导读者快速集成并定制个性化动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值