SmartRefreshLayout集成笔记,实现下拉刷新上拉加载更多。

时间:2022-12-22 22:05:25

SmartRefreshLayout是一个功能超级全面的下拉刷新,上拉加载更多框架。

1.地址:https://github.com/scwang90/SmartRefreshLayout

2.在gradle添加依赖:

compile 'com.android.support:appcompat-v7:25.3.1'//版本随意
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.3'//没有使用特殊Header,可以不加这行
3.在布局中添加SmartRefreshLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="supplier.ztxx.com.test.MainActivity">
    <android.support.v7.widget.Toolbar style="@style/AppTheme.Toolbar"
        android:id="@+id/toolbar"
        app:title="标题栏"/>
    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/refreshLayout"
        >
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never"
            android:background="#fff"
            />
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
 
4.SmartRefreshLayout的头布局和尾布局有三种方法可以定义,这里只实现一种。
在自定义的app全局定义
 
public class App extends Application {
    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
                @Override
                public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                    layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
                    return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header,默认是 贝塞尔雷达Header
                }
            });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
                @Override
                public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                    //指定为经典Footer,默认是 BallPulseFooter
                    return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
                }
            });
    }
}
5.代码中的实现:
 
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private BaseRecyclerAdapter<TestBean> mAdpater;
    private RecyclerView mRecyclerView;
    private ArrayList<TestBean> al;
    private SmartRefreshLayout mRefreshLayout;
    private TestBean testBean;
    private static boolean isFirstEnter = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        mRecyclerView= (RecyclerView) findViewById(recyclerView);
        //状态栏透明和间距处理
        //标题栏处理
        StatusBarUtil.setPaddingSmart(this,findViewById(R.id.toolbar));
        StatusBarUtil.immersive(this, 0xff000000, 0.1f);
        getData();
        setAdapter();
        setListener();
    }
    private void getData() {
        al = new ArrayList<>();
       
        for (int i=0;i<50;i++){
            testBean = new TestBean();
            testBean.setName("条目"+i);
            al.add(testBean);
        }
    }

    private void setAdapter() {
        View view = findViewById(recyclerView);
        if (view instanceof RecyclerView) {
            RecyclerView recyclerView = (RecyclerView) view;
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));
            recyclerView.setItemAnimator(new DefaultItemAnimator());
          
            recyclerView.setAdapter(mAdpater=new BaseRecyclerAdapter<TestBean>(al,simple_list_item_2,this) {
                @Override
                protected void onBindViewHolder(SmartViewHolder holder, TestBean model, int position) {
                    holder.text(android.R.id.text1, model.getName());
                    holder.text(android.R.id.text2, model.getName());
                    holder.textColorId(android.R.id.text2, R.color.colorAccent);
                }
            });
            mRecyclerView = recyclerView;
        }
        if (isFirstEnter) {
            isFirstEnter = false;
            //触发自动刷新
            mRefreshLayout.autoRefresh();
        }
    }
    private void setListener() {
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);
            }
        });
        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
            }
        });
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent=new Intent(this,WeiboPracticeActivity.class);
        this.startActivity(intent);
        mRefreshLayout.autoRefresh();
    }
}