美文网首页Android应用开发那些事
第三方开源库之 SmartRefreshLayout

第三方开源库之 SmartRefreshLayout

作者: Kevin_小飞象 | 来源:发表于2019-11-28 15:08 被阅读0次

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.scwang.smartrefresh:SmartRefreshLayout:1.1.0'  
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0'  //没有使用特殊Header,可以不加这行

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

android.useAndroidX=true
android.enableJetifier=true
  1. 在 xml 布局文件中添加 SmartRefreshLayout
<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/refreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.scwang.smartrefresh.layout.header.ClassicsHeader
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:srlAccentColor="#aaa"
        app:srlEnableLastTime="false"
        app:srlPrimaryColor="#f2f2f2" />

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

    <com.scwang.smartrefresh.layout.footer.ClassicsFooter
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srlClassicsSpinnerStyle="Translate" />

</com.scwang.smartrefresh.layout.SmartRefreshLayout>
  1. Activity 中逻辑代码
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.epro.test.R;
import com.epro.test.adapter.HomeAdapter;
import com.epro.test.bean.HomeBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LayoutActivity extends AppCompatActivity implements OnLoadMoreListener, OnRefreshListener {
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    private HomeAdapter mAdapter;


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

        ButterKnife.bind(this);

        initAdapter();

        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadMoreListener(this);

    }

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

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(LayoutActivity.this,"click"+position,Toast.LENGTH_SHORT).show();
            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mRecyclerView.setAdapter(mAdapter);
    }

    private List<HomeBean> getHomeItem() {
        List<HomeBean> list = Arrays.asList(
                new HomeBean("android", R.mipmap.ic_launcher),
                new HomeBean("ios", R.mipmap.ic_launcher),
                new HomeBean("java", R.mipmap.ic_launcher),
                new HomeBean("javascript", R.mipmap.ic_launcher),
                new HomeBean("php", R.mipmap.ic_launcher),
                new HomeBean("gyq", R.mipmap.ic_launcher),
                new HomeBean("ysh", R.mipmap.ic_launcher)
        );

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

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

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mRefreshLayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
    }


}

相关文章

网友评论

    本文标题:第三方开源库之 SmartRefreshLayout

    本文链接:https://www.haomeiwen.com/subject/xbrbwctx.html