zoukankan      html  css  js  c++  java
  • 032 Android智能下拉刷新框架-SmartRefreshLayout+RecyclerView的使用

    1.SmartRefreshLayout介绍

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

    2.使用步骤

    (1)在 build.gradle (app)中添加依赖

        implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-26'
        implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-26'//没有使用特殊Header,可以不加这行
        implementation 'com.android.support:design:28.0.0'

    (2)布局文件(使用SmartRefreshLayout和RecyclerView)

    <?xml version="1.0" encoding="utf-8"?>
    <com.scwang.smartrefresh.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.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never"
            android:scrollbars="vertical"
            android:background="#fff" />
    
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

    (3)RecyclerView的item布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="28dp"
            android:textSize="16sp"
            android:textStyle="bold"
            android:text="hehe"/>
    
    </RelativeLayout>

    (4)java后台

    package com.example.administrator.test65smartrefreshlayout;
    
    import android.support.annotation.NonNull;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.DividerItemDecoration;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import com.scwang.smartrefresh.header.MaterialHeader;
    import com.scwang.smartrefresh.layout.api.RefreshLayout;
    import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
    import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
    import com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener;
    
    import java.util.ArrayList;
    
    public class MainActivity extends AppCompatActivity {
    
        RecyclerView mRecyclerView;
        MyAdapter mAdapter;
        LinearLayoutManager mLayoutManager;
        RefreshLayout refreshLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
    
            mRecyclerView = findViewById(R.id.my_recycler_view);
            mLayoutManager = new LinearLayoutManager(this);
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
            mRecyclerView.setHasFixedSize(true);
            mAdapter = new MyAdapter(getDatas());
            mRecyclerView.setAdapter(mAdapter);
        }
    
        private void initView() {
            refreshLayout = findViewById(R.id.refreshLayout);
            /**
             * 设置不同的头部、底部样式
             */
    //        refreshLayout.setRefreshFooter(new ClassicsFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
    //        refreshLayout.setRefreshHeader(new BezierRadarHeader(this));
    //        refreshLayout.setRefreshHeader(new TwoLevelHeader(this));
            refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
    
            refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
    
            //设置样式后面的背景颜色
            refreshLayout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
    
            //设置监听器,包括顶部下拉刷新、底部上滑刷新
            refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener(){
    
                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    mAdapter.refreshData(MoreDatas()); //下拉刷新,数据从上往下添加到界面上
                    refreshLayout.finishRefresh(1000); //这个记得设置,否则一直转圈
                }
    
                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                    mAdapter.loadMore(MoreDatas());  //上滑刷新,数据从下往上添加到界面上
                    refreshLayout.finishLoadMore(1000); //这个记得设置,否则一直转圈
                }
            });
    
        }
    
        //原始的recyclerView数据
        private ArrayList<String> getDatas() {
            ArrayList<String> data = new ArrayList<>();
            String temp = " item";
            for(int i = 0; i < 15; i++) {
                data.add(i + temp);
            }
            return data;
        }
    
        //刷新得到的数据
        private ArrayList<String> MoreDatas() {
            ArrayList<String> data = new ArrayList<>();
            String temp = "新加数据 ";
            for(int i = 0; i < 6; i++) {
                data.add(temp + i);
            }
            return data;
        }
    
        public static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    
            public ArrayList<String> datas = null;
    
            public MyAdapter(ArrayList<String> datas) {
                this.datas = datas;
            }
    
            //创建新View,被LayoutManager所调用
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_smartrefreshlayout_item,viewGroup,false);
                ViewHolder vh = new ViewHolder(view);
                return vh;
            }
    
            //将数据与界面进行绑定的操作
            @Override
            public void onBindViewHolder(ViewHolder viewHolder, int position) {
                viewHolder.mTextView.setText(datas.get(position));
            }
    
            //获取数据的数量
            @Override
            public int getItemCount() {
                return datas.size();
            }
    
            //底部上拉刷新,数据直接在底部显示
            public void loadMore(ArrayList<String> strings) {
                datas.addAll(strings);
                notifyDataSetChanged();
            }
    
            //底部下拉刷新,数据直接从上往下添加数据,显示在顶部
            public void refreshData(ArrayList<String> strings) {
                datas.addAll(0, strings);
                notifyDataSetChanged();
    //            notifyItemInserted(0); 一次只能加一项数据
            }
    
            //自定义的ViewHolder,持有每个Item的的所有界面元素
            public static class ViewHolder extends RecyclerView.ViewHolder {
                public TextView mTextView;
                public ViewHolder(View view){
                    super(view);
                    mTextView = view.findViewById(R.id.text);
                }
            }
        }
    }

    3.效果图

  • 相关阅读:
    5月27 开通博客啦
    源码编译安装mysql-boost-5.7.16.tar.gz报错分析处理
    MHA实践操作
    (转)MySQL高可用方案MHA的部署和原理
    Mysql利用binlog日志恢复数据操作(转)
    生产环境mysql数据库主从恢复从数据库 -- flush tables with read lock;
    带着问题学习openstack
    openstack的网络模式(转)
    openstack 镜像初始化root登录密码
    openstack placement 组件作用理解
  • 原文地址:https://www.cnblogs.com/luckyplj/p/10888512.html
Copyright © 2011-2022 走看看