RecyclerView 下拉刷新和加载更多
一.SwipeRefreshLayout实现下拉刷新
1.方法API:
setOnRefreshListener(OnRefreshListener):添加下拉刷新监听器
setRefreshing(boolean):显示或者隐藏刷新进度条
isRefreshing():检查是否处于刷新状态
setColorSchemeResources():设置进度条的颜色主题,最多设置四种,以前的setColorScheme()方法已经弃用了。
2.简单使用:
<?xmlversionxmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="match_parent">
<includelayoutincludelayout="@layout/common_top_bar_layout"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/demo_swiperefreshlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/demo_recycler"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
Java:
原理:是一个刷新布局,来自兼容包v4可以运行在低版本,控件如果想要支持下拉刷新,只要使用当前布局包裹
setColorSchemeColors:修改旋转颜色,可以添加多种颜色
setRefreshing: 是否显示loading状态
setOnRefreshListener:下拉刷新监听
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipeRefreshLayout;
protected void onCreate(Bundle savedInstanceState) {
·····
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
//设置旋转的颜色效果
swipeRefreshLayout.setColorSchemeColors(Color.GREEN, Color.YELLOW, Color.RED);
//设置下拉刷新的监听器
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
public void onRefresh() {
requestData();
}
});
}
private void requestData() {
new Handler().postDelayed(new Runnable() {//延时加载
public void run() {
String json = "{name:小米}";
TextView text = (TextView) findViewById(R.id.text);
text.setText(json);
swipeRefreshLayout.setRefreshing(false);//关闭显示
}
}, );
}
二.加载更多——上滑加载数据
思路:
1.滚动到底部 getItemCount()-2 ==bottomPosition
2.不处理滚动中
OnScrollListener监听滚动加载的监听器
int dy 滚动距离,向上滚动为正
layoutManager.findLastVisibleItemPosition获取处于底部数据的下标
dy>0与isLoading 都是用来控件灵敏度
public class HomeFragment extends Fragment {
private SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView recyclerView;
private AppAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//有一个显示网络状态
//StateLayout stateLayout = new StateLayout(container.getContext());
swipeRefreshLayout = new SwipeRefreshLayout(container.getContext());
//设置颜色
swipeRefreshLayout.setColorSchemeColors(Color.GREEN);
//设置下拉监听
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
HttpConnect.get(ApiUrls.HOME+"?index=0", callback);
}
});
recyclerView = new RecyclerView(container.getContext());
//设置排列规则
recyclerView.setLayoutManager(new LinearLayoutManager(container.getContext()));
//预先设置了Noemal视图,但是normal视图没有内容 stateLayout.addNormalView(recyclerView);
swipeRefreshLayout.addView(recyclerView);
//发送请求给服务器
HttpConnect.get(ApiUrls.HOME+"?index=0",callback);
return swipeRefreshLayout;
}
DefaultCallBack callback = new DefaultCallBack() {
@Override
public void onStart(int what) {
swipeRefreshLayout.setRefreshing(true);
}
@Override
public void onFinish(int what) {
swipeRefreshLayout.setRefreshing(false);
}
//拿到数据
protected void createView(String json) {
HomeWebInfo info = new Gson().fromJson(json, HomeWebInfo.class);
//创建控件,设置适配器
adapter=new AppAdapter(info.list);
recyclerView.setAdapter(adapter);
//上滑加载数据
addLoadMoreList();
}
};
private void addLoadMoreList(){
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
private boolean isLoading = false;
//int dy 上下滑动的距离 +数代表往上滑动
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//判断 是否是排列布局
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//判断是否是线性排列,并且是否滑动距离
if(layoutManager instanceof LinearLayoutManager && dy> ){
//当前是列表
int total = adapter.getItemCount();
int lastPosition = total-;
//获取rv 的bottom条目
int currLastPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
if(currLastPosition==lastPosition&&!isLoading){ }
isLoading=true;
Toast.makeText(MyApp.getContext(),"加载更多...",Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MyApp.getContext(), "加载成功", Toast.LENGTH_SHORT).show();
isLoading=false;
}
},);
}
}
});
}
}
RecyclerView 下拉刷新和加载更多的更多相关文章
- Android之RecyclerView轻松实现下拉刷新和加载更多
今天研究了下RecyclerView的滑动事件,特别是下拉刷新和加载更多事件,在现在几乎所有的APP显示数据列表时都用到了.自定义RecyclerView下拉刷新和加载更多听上去很复杂,实际上并不难, ...
- RecyclerView的下拉刷新和加载更多 动画
下拉刷新和加载更多 1.https://github.com/jianghejie/XRecyclerView 2.http://blog.csdn.net/jabony/article/detail ...
- 自己封装的工具类,使用原生SwipeRefreshLayout+RecycleView实现下拉刷新和加载更多
实现SwipeRefreshLayout+RecycleView实现刷新 在你的xml文件里写上如下代码: <android.support.v4.widget.SwipeRefreshLayo ...
- iOS 下拉刷新和加载更多 (OC\Swift)
Swift语言出来之后, 可能还没有第三方的下拉刷新和上提加载, 所以自己用UIRefreshControl控件和UITableView实例的tableFooterView(底部视图)属性结合起来写了 ...
- Android UI--自定义ListView(实现下拉刷新+加载更多)
Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...
- Android Demo 下拉刷新+加载更多+滑动删除
小伙伴们在逛淘宝或者是各种app上,都可以看到这样的功能,下拉刷新和加载更多以及滑动删除,刷新,指刷洗之后使之变新,比喻突破旧的而创造出新的,比如在手机上浏览新闻的时候,使用下拉刷新的功能,我们可以第 ...
- PullToRefresh下拉刷新 加载更多 详解 +示例
常用设置 项目地址:https://github.com/chrisbanes/Android-PullToRefresh a. 设置刷新模式 如果Mode设置成Mode.PULL_FROM_STAR ...
- 实现RecyclerView下拉刷新和上拉加载更多以及RecyclerView线性、网格、瀑布流效果演示
实现RecyclerView下拉刷新和上拉加载更多以及RecyclerView线性.网格.瀑布流效果演示 效果预览 实例APP 小米应用商店 使用方法 build.gradle文件 dependenc ...
- RecyclerView下拉刷新上拉加载更多
现在Android里都建议用RecyclerView代替ListView和GridView,所以下拉刷新和上拉加载更多也需要实现.下拉刷新可以用SwipeRefreshLayout 包裹Recycle ...
随机推荐
- (转)request模拟知乎登录(无验证码机制
原文:http://www.itnose.net/detail/6755805.html import request try: import cookielib #python2版本 except: ...
- C# 多线程五之Task(任务)一
1.简介 为什么MS要推出Task,而不推Thread和ThreadPool,以下是我的见解: (1).Thread的Api并不靠谱,甚至MS自己都不推荐,原因,它将整个Thread类都不开放给Win ...
- 全网最详细的Windows系统里PLSQL Developer 64bit的下载与安装过程(图文详解)
不多说,直接上干货! ORACLE是数据库,有客户端和服务器: 其,具体下载,可见http://www.oracle.com/technetwork/database/enterprise-editi ...
- Shell脚本 | 抓取log文件
在安卓应用的测试过程中,遇到 Crash 或者 ANR 后,想必大家都会通过 adb logcat 命令来抓取日志定位问题.如果直接使用 logcat 命令的话,默认抓取出的 log 文件包含安卓运行 ...
- 解决Oracle死锁问题,及产生的原因
文章来源:http://www.cnblogs.com/leijh/archive/2012/10/15/2724165.html 最近老是发现应该执行操作数据库的代码时发现执行不了,查了一下发现是数 ...
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(七):集成 Druid 数据源
数据库连接池负责分配.管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个:释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏 ...
- curl常用命令【转】
原文地址: http://www.thegeekstuff.com/2012/04/curl-examples/ 下载单个文件,默认将输出打印到标准输出中(STDOUT)中 curl http://w ...
- C# 1.0 到 4.0 的进化 1
定义一个产品类 Product C# 1 using System; using System.Collections; namespace C1 { public class Product { s ...
- CentOS6.5 QT5.3 找不到GLIBCXX3.4.15解决方法
下载安装后 启动的时候提示 GLIBCXX_3.4.15,发现libstdc++.so.6的版本过, 在安装qt-creator的时候运行这个IDE就出现了这个问题,是由于libstdc++.so.6 ...
- Spring-IOC注解
注解主要的目的就是实现零XML配置.一:自动扫描装配Bean. spring为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component.@Service.@Controller.@ ...