【Android -- 开源库】SmartRefreshLayout 的基本使用

在这里插入图片描述

前言

SmartRefreshLayout 以打造一个强大,稳定,成熟的下拉刷新框架为目标,并集成各种的炫酷、多样、实用、美观的 Header 和 Footer。 正如名字所说,SmartRefreshLayout 是一个“聪明”或者“智能”的下拉刷新布局,由于它的“智能”,它不只是支持所有的 View ,还支持多层嵌套的视图结构。 它继承自 ViewGroup 而不是 FrameLayout 或 LinearLayout ,提高了性能。 也吸取了现在流行的各种刷新布局的优点,包括谷歌官方的 SwipeRefreshLayout, 其他第三方的 Ultra-Pull-To-RefreshTwinklingRefreshLayout 。 还集成了各种炫酷的 Header 和 Footer。

特色

  • 支持横向刷新
  • 支持多点触摸
  • 支持淘宝二楼和二级刷新
  • 支持嵌套多层的视图结构 Layout (LinearLayout,FrameLayout…)
  • 支持所有的 View(AbsListView、RecyclerView、WebView…View)
  • 支持自定义并且已经集成了很多炫酷的 Header 和 Footer.
  • 支持和 ListView 的无缝同步滚动 和 CoordinatorLayout 的嵌套滚动 .
  • 支持自动刷新、自动上拉加载(自动检测列表惯性滚动到底部,而不用手动上拉).
  • 支持自定义回弹动画的插值器,实现各种炫酷的动画效果.
  • 支持设置主题来适配任何场景的 App,不会出现炫酷但很尴尬的情况.
  • 支持设多种滑动方式:平移、拉伸、背后固定、顶层固定、全屏
  • 支持所有可滚动视图的越界回弹
  • 支持 Header 和 Footer 交换混用
  • 支持AndroidX

实战

在这里插入图片描述

1. 在 app/build.gradle 中添加依赖:

	implementation 'com.github.bumptech.glide:glide:4.12.0'
    implementation  'io.github.scwang90:refresh-layout-kernel:2.0.5'      //核心必须依赖
    implementation  'io.github.scwang90:refresh-header-classics:2.0.5'    //经典刷新头
    implementation  'io.github.scwang90:refresh-footer-classics:2.0.5'    //经典加载
    // RecyclerAdapter
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4'

如果使用 AndroidX 在 gradle.properties 中添加:

android.useAndroidX=true
android.enableJetifier=true

注意:如果我们请求的 url 地址是 http,记得在清单文件中添加:

	<application
        ...
        android:usesCleartextTraffic="true"
        >
        ...
    </application>

2. 在 activity_layout.xml 布局文件中添加 SmartRefreshLayout

<com.scwang.smart.refresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/refreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    android:orientation="vertical">

    <com.scwang.smart.refresh.header.ClassicsHeader
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"/>

    <com.scwang.smart.refresh.footer.ClassicsFooter
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</com.scwang.smart.refresh.layout.SmartRefreshLayout>

3. 适配器

public class HomeAdapter extends BaseQuickAdapter<HomeBean, BaseViewHolder> {
    public HomeAdapter(int layoutResId, List<HomeBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, HomeBean homeBean) {
        ImageView image = baseViewHolder.getView(R.id.item_image);
        Glide.with(MyApp.getContext())
                .load(homeBean.getUrl())
                .into(image);

        baseViewHolder.setText(R.id.item_title,homeBean.getName());
    }
}

4. Activity 中逻辑代码

public class MainActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private HomeAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        initAdapter();

        mRefreshLayout.setRefreshHeader(new ClassicsHeader(this));
        mRefreshLayout.setRefreshFooter(new ClassicsFooter(this));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadMoreListener(this);

    }

    private void initAdapter() {
        mAdapter = new HomeAdapter(R.layout.activity_sample_list_item,getHomeItem());

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mRecyclerView.setAdapter(mAdapter);
    }

    private List<HomeBean> getHomeItem() {
        List<HomeBean> list = Arrays.asList(
                new HomeBean("android", ConstantValues.images[0]),
                new HomeBean("ios", ConstantValues.images[1]),
                new HomeBean("java", ConstantValues.images[2]),
                new HomeBean("javascript", ConstantValues.images[3]),
                new HomeBean("php", ConstantValues.images[4]),
                new HomeBean("gyq", ConstantValues.images[5]),
                new HomeBean("ysh", ConstantValues.images[6])
        );

        List<HomeBean> infos = new ArrayList<>(list);
        return infos;
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mRefreshLayout.finishRefresh(2000);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mRefreshLayout.finishLoadMore(2000);
    }
}
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kevin-Dev

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值