package com.izhuazhua.swiperecyclerviewmy;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by andy on 2016/6/18.
*/
public class MainActivity extends AppCompatActivity {
private String[] names = new String[]
{ "小虎", "小玉", "小清", "小李"};
private String[] descs = new String[]
{ "游戏高手", "一个具有音乐天赋的女孩,弹得一手好钢琴"
, "C++女高手,十五年C++编程经验", "android开发牛人,微信客户端核心成员之一"};
private int[] imageIds = new int[]
{ R.drawable.tiger , R.drawable.nongyu
, R.drawable.qingzhao , R.drawable.libai};
private List<Map<String, Object>> originalData = new ArrayList<>();
private RecyclerViewAdapter adapter = new RecyclerViewAdapter(this);
SwipeRefreshLayout swipeRefreshLayout;
private Handler handler = new Handler();
RecyclerView recyclerView;
boolean isLoading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("==MainActivity==", "setContentViewLog");
swipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.SwipeRefreshLayout);
swipeRefreshLayout.setColorSchemeColors(Color.BLUE, Color.RED, Color.YELLOW, Color.GREEN);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//下拉刷新是触发该回调函数。
@Override
public void onRefresh() {
Log.d("==MainActivity==", "onRefresh");
handler.postDelayed(new Runnable() {
@Override
public void run() {
//添加刷新内容
Log.d("==MainActivity==", "handler.Onrefrshes");
addData();
swipeRefreshLayout.setRefreshing(false);
}
}, 2000);
}
});
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
Log.d("==MainActivity==", "onScrolledStart");
super.onScrolled(recyclerView, dx, dy);
int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
int count = adapter.getItemCount();
//下面的判断比较列表项是否和可见列表最后一项一致,程序刚启动时,条件满足,自动加载,当加载到列表项多到一页展示不完时,停止自动加载
//当手指在屏幕上向上滑动时,该条件又满足,然后就加载更多了
if (lastVisibleItemPosition + 1 == count )
{
Log.d("==MainActivity==", "loading excute");
boolean isRefreshing = swipeRefreshLayout.isRefreshing();
if (isRefreshing) {
return;
}
if (!isLoading) {
isLoading = true;
handler.postDelayed(new Runnable() {
@Override
public void run() {
isLoading = false;
Log.d("==MainActivity==", "load data complete");
int count = adapter.getItemCount();
//加载更多数据
addData();
int count1 = adapter.getItemCount();
swipeRefreshLayout.setRefreshing(false);
}
}, 2000);
}
}//*/
}
});
setClickListener();
addData();
}
public void setClickListener() {
adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClicked(View view, int positon) {
}
@Override
public void onItemLongClicked(View view, int position) {
}
});
}
private void createOriginalData()
{
for (int i = 0; i < names.length; i++)
{
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("header", imageIds[i]);
listItem.put("personName", names[i]);
listItem.put("desc", descs[i]);
originalData.add(listItem);
}
}
public void addData() {
Log.d("=MainActivity=", "addData data size:" + originalData.size());
if(originalData.size()<1) {
createOriginalData();
}
adapter.addData(originalData);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SwipeRefreshLayout结合RecyclerView实现下拉刷新和滑动加载更多
共58个文件
xml:28个
png:5个
java:4个
5星 · 超过95%的资源 需积分: 9 161 下载量 26 浏览量
2016-06-20
22:04:04
上传
评论
收藏 130KB ZIP 举报
温馨提示
SwipeRefreshLayout结合RecyclerView实现下拉刷新和滑动加载更多 现在很多界面都是要求,下拉刷新,列表项目更多的时候,滑动加载更多,就像微信朋友圈一样,下拉时要刷新,向上滑动要实 现“加载更多”。 我也是初学Android开发,为了搞懂这个逻辑,花了两三天时间,在网上找例子,也找了很久,一直没找到对于初学者合适的,不 是太复杂,就是版本比较旧,没有用到RecyclerView控件。逻辑理清后,其实很简单。代码中有详细注释,希望对刚入门的初学 者有所帮助
资源推荐
资源详情
资源评论
收起资源包目录
MySwipeRecyclerView.zip (58个子文件)
SwipeRecyclerViewMy
gradlew.bat 2KB
gradlew 5KB
gradle.properties 872B
.gradle
2.10
taskArtifacts
cache.properties.lock 17B
cache.properties 31B
app
proguard-rules.pro 662B
src
test
java
com
izhuazhua
swiperecyclerviewmy
ExampleUnitTest.java 340B
androidTest
java
com
izhuazhua
swiperecyclerviewmy
ApplicationTest.java 376B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
drawable
tiger.gif 2KB
nongyu.gif 2KB
qingzhao.gif 2KB
libai.gif 2KB
mipmap-hdpi
ic_launcher.png 3KB
values
colors.xml 792B
strings.xml 135B
styles.xml 748B
dimens.xml 402B
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
layout
activity_main.xml 1KB
item_base.xml 2KB
item_foot.xml 798B
values-w820dp
dimens.xml 364B
java
com
izhuazhua
swiperecyclerviewmy
RecyclerViewAdapter.java 4KB
MainActivity.java 6KB
AndroidManifest.xml 692B
app.iml 10KB
.gitignore 8B
build
build.gradle 772B
libs
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 231B
.idea
gradle.xml 739B
runConfigurations.xml 564B
misc.xml 3KB
.name 19B
copyright
profiles_settings.xml 76B
modules.xml 375B
encodings.xml 159B
compiler.xml 686B
workspace.xml 145KB
libraries
android_android_23.xml 241B
support_vector_drawable_23_4_0.xml 640B
support_annotations_23_4_0.xml 495B
hamcrest_core_1_3.xml 526B
junit_4_12.xml 477B
recyclerview_v7_23_4_0.xml 600B
appcompat_v7_23_4_0.xml 585B
animated_vector_drawable_23_4_0.xml 645B
support_v4_23_4_0.xml 729B
cardview_v7_23_2_1.xml 580B
.gitignore 105B
local.properties 428B
settings.gradle 16B
SwipeRecyclerViewMy.iml 950B
build
intermediates
dex-cache
cache.xml 64B
generated
build.gradle 521B
共 58 条
- 1
资源评论
- 0点起航2017-06-30真的很不错哦
wxq888
- 粉丝: 61
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功