转载于:https://www.aliyun.com/jiaocheng/2632.html
今天这篇文章主要是介绍一下SmartRefreshLayout这个第三方下拉刷新库的使用,之前在项目中一直用的都是PullToRefreshLayout这个库,感觉有不少坑,后面谷歌又出了SwipeRefreshLayout,毕竟谷歌自家的,还是得多多支持。最近想积累自己的技术栈,想着集成一些高效稳定的第三方框架并且基于一些开放的api快速开发一款应用,于是就到处去找这个项目的各个配件。在下拉刷新这方面,最终选择了这个SmartRefreshLayout这个库,github上面的star数也是相当的多啊,可见它还是很受欢迎的。我这里是简单的写了个Demo,测试了一下它的用法,确实是挺优秀的框架。
一、使用过程
1、首先给项目添加依赖,在build.gradle文件的dependencies闭包中添加这句话:compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'
2、Activity的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/toolbar"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="测试"/>
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlEnableLoadmore="true">
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlFinishDuration="500"
app:srlEnableLastTime="true"
app:srlClassicsSpinnerStyle="FixedBehind"
app:srlTextSizeTitle="16sp"
app:srlTextSizeTime="10dp"
app:srlTextTimeMarginTop="2dp"
app:srlDrawableArrowSize="20dp"
app:srlDrawableProgressSize="20dp"
app:srlDrawableMarginRight="20dp"
app:srlDrawableProgress="@drawable/ic_progress_hojder"/>
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:listitem="@android:layout/simple_list_item_2"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
列表Item的布局文件如下:<?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="vertical">
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:text="我是第一个"
android:textSize="15sp"
android:textColor="@android:color/black"
android:gravity="center_vertical"
android:paddingLeft="12dp"/>
这里我们在主布局中通过全类名的方式引入指定SmartRefreshLayout,并且添加了经典模式下的header和footer。
3、逻辑代码
这里通过RecyclerView模拟了一个数据列表的展示,提供下拉刷新和上拉加载的功能,在Activity中我们让它实现OnRefreshListener和OnLoadmoreListener这两个接口,重写onRefresh()和onLoadmore()两个方法,我们可以在这两个方法中具体去处理刷新和加载的逻辑,设置顶部Header样式为经典样式,具体方式为:
ClassicsHeader mClassicsHeader = (ClassicsHeader) mRefreshLayout.getRefreshHeader();
// int deta = new Random().nextInt(7 * 24 * 60 * 60 * 1000);
// mClassicsHeader.setLastUpdateTime(new Date(System.currentTimeMillis()-deta));
// mClassicsHeader.setTimeFormat(new SimpleDateFormat("更新于 MM-dd HH:mm", Locale.CHINA));
if (mClassicsHeader != null) {
mClassicsHeader.setTimeFormat(new DynamicTimeFormat("更新于 %s"));
}
if (mClassicsHeader != null) {
mDrawableProgress = mClassicsHeader.getProgressView().getDrawable();
}
if (mDrawableProgress instanceof LayerDrawable) {
mDrawableProgress = ((LayerDrawable) mDrawableProgress).getDrawable(0);
}
当我们需要设置页面自动刷新时,可以这样写:
mRefreshLayout.autoRefresh();
具体代码如下:
package com.archie.rxpracticies;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("RedundantCast")
public class MainActivity extends AppCompatActivity implements OnRefreshListener, OnLoadmoreListener {
public static String TAG = "com.archie.rxpracticies";
private RecyclerView mRecyclerView;
private RefreshLayout mRefreshLayout;
private Toolbar mToolbar;
private Drawable mDrawableProgress;
private List mList = new ArrayList<>();
private MyAdaper mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
initData();
}
private void initData() {
getStringData();
setThemeColor(R.color.colorPrimary, R.color.colorPrimaryDark);
mRefreshLayout.getLayout().setBackgroundResource(android.R.color.transparent);
mRefreshLayout.setPrimaryColors(0, 0xff666666);
if (Build.VERSION.SDK_INT >= 21) {
mDrawableProgress.setTint(0xff666666);
} else if (mDrawableProgress instanceof VectorDrawableCompat) {
((VectorDrawableCompat) mDrawableProgress).setTint(0xff666666);
}
mRefreshLayout.autoRefresh();
//创建并设置Adapter
Log.e(TAG, "" + mList.size());
mAdapter = new MyAdaper(this, mList);
mRecyclerView.setAdapter(mAdapter);
}
private void initListener() {
mRefreshLayout.setOnRefreshListener(this);
mRefreshLayout.setOnLoadmoreListener(this);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void initView() {
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRefreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
ClassicsHeader mClassicsHeader = (ClassicsHeader) mRefreshLayout.getRefreshHeader();
// int deta = new Random().nextInt(7 * 24 * 60 * 60 * 1000);
// mClassicsHeader.setLastUpdateTime(new Date(System.currentTimeMillis()-deta));
// mClassicsHeader.setTimeFormat(new SimpleDateFormat("更新于 MM-dd HH:mm", Locale.CHINA));
if (mClassicsHeader != null) {
mClassicsHeader.setTimeFormat(new DynamicTimeFormat("更新于 %s"));
}
if (mClassicsHeader != null) {
mDrawableProgress = mClassicsHeader.getProgressView().getDrawable();
}
if (mDrawableProgress instanceof LayerDrawable) {
mDrawableProgress = ((LayerDrawable) mDrawableProgress).getDrawable(0);
}
//创建默认的线性LayoutManager
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
//如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
mRecyclerView.setHasFixedSize(true);
}
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
refreshlayout.finishLoadmore(2000);
mList.add("新增项");
mAdapter.notifyDataSetChanged();
}
@Override
public void onRefresh(RefreshLayout refreshlayout) {
refreshlayout.finishRefresh(2000);
if (mList.size() > 0) {
mList.clear();
getStringData();
mAdapter.notifyDataSetChanged();
}
}
private void getStringData() {
for (int i = 1; i <= 20; i++) {
mList.add("我是第" + i + "项");
}
}
private void setThemeColor(int colorPrimary, int colorPrimaryDark) {
mToolbar.setBackgroundResource(colorPrimary);
mRefreshLayout.setPrimaryColorsId(colorPrimary, android.R.color.white);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(ContextCompat.getColor(this, colorPrimaryDark));
mDrawableProgress.setTint(0xffffffff);
} else if (mDrawableProgress instanceof VectorDrawableCompat) {
((VectorDrawableCompat) mDrawableProgress).setTint(0xffffffff);
}
}
}适配器MyAdapter.java的代码如下:package com.archie.rxpracticies;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
/**
* Created by Administrator on 2017/10/9 0009.
* 适配器
*/
public class MyAdaper extends RecyclerView.Adapter {
private List mList;
private Context mContext;
public MyAdaper(Context context, List list) {
this.mContext = context;
this.mList = list;
}
//创建View,被LayoutManager使用
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_view, parent, false);
return new ViewHolder(view);
}
//将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mList.get(position));
}
//获取数据的数量
@Override
public int getItemCount() {
return mList.size();
}
//自定义的ViewHolder,持有每个Item的的所有界面元素
static class ViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
ViewHolder(View view) {
super(view);
mTextView = (TextView) view.findViewById(R.id.text);
}
}
}这里在使用RecyclerView的时候有个坑,如果主布局控件高度设置的是match_parent,那么Item中高度不能设置为match_parent,否则会导致数据只展示一条。
以上内容就是我对这个库的一个使用说明,这里介绍的是最常规的用法,当然了,这个库十分强大,还有很多很牛逼的用法,我也在慢慢探索,相信我的文章大家都能很容易看懂,如果大家不想看的,我这里给出这个库的源地址,希望大家去github上面详细的查看对应的使用说明,最好是能把它的Demo下载下来自己跑一遍看看,因为里面有很多很酷炫的效果,万一哪天你们的产品汪就提出了一样的需求呢?谢谢大家的观看,欢迎批评指正。
SmartRefreshLayout的项目地址:https://github.com/scwang90/SmartRefreshLayout
参考博客如下:
https://www.2cto.com/kf/201707/655476.html
https://blog.csdn.net/Miko_k/article/details/80279198