目录结构:

效果图:

MainActivity.java

package qianfeng.com.pullrecyclerviewdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { private SwipeRefreshLayout swipeLayout;
private RecyclerView recyclerView;
private List<String> total = new ArrayList<>();
private MyAdapter myAdapter;
boolean isReferencing;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 100:
isReferencing = false;
swipeLayout.setRefreshing(false);
myAdapter.notifyDataSetChanged();
break;
case 200:
isReferencing = false;
// 添加数据结束后 将 空的那条数据 移除 在刷新
total.remove("");
myAdapter.notifyDataSetChanged();
break; }
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); final LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(manager);
initData();
myAdapter = new MyAdapter(total, this);
recyclerView.setAdapter(myAdapter); 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) {
super.onScrolled(recyclerView, dx, dy);
int lastVisibleItemPosition = manager.findLastVisibleItemPosition();
if (lastVisibleItemPosition == myAdapter.getItemCount() - 1) {
isReferencing = swipeLayout.isRefreshing();
if (!isReferencing) { isReferencing = true;
initData();
handler.sendEmptyMessageDelayed(200, 3000);
Toast.makeText(MainActivity.this, "最后拉 ,不要在向下了", Toast.LENGTH_SHORT).show();
}
}
}
}); swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() { new Thread() {
@Override
public void run() {
super.run();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
total.clear();
initData();
handler.sendEmptyMessage(100);
}
}.start();
}
}); } private void initData() {
for (int i = 0; i < 50; i++) {
total.add("都别睡觉啊~ 逮住" + i);
}
// 每次加载数据的时候添加一条空数据 作为 footerView 展示的 itemView
total.add("");
} public void onClick(View view) {
// myAdapter.delete(2);
}
}

MyAdapter.java

package qianfeng.com.pullrecyclerviewdemo;

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 ${Mr.Zhao} on 2016/10/19.
*/
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<String> list;
LayoutInflater inflater;
//1. 定义两种类型
private final int TYPE_FOOTER_VIEW = 0;
private final int TYPE_ITEM_VIEW = 1; public MyAdapter(List<String> list, Context context) {
this.list = list;
inflater = LayoutInflater.from(context);
} @Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1)
return TYPE_FOOTER_VIEW;
return TYPE_ITEM_VIEW;
} @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM_VIEW) {
View view = inflater.inflate(R.layout.item_layout, parent, false);
return new MyViewHolder(view);
} else {
View view = inflater.inflate(R.layout.footer_layout, parent, false);
return new FooterViewHolder(view);
}
} @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder) {
((MyViewHolder) holder).item_tv.setText(list.get(position));
}
} @Override
public int getItemCount() {
return list.size();
} class MyViewHolder extends RecyclerView.ViewHolder {
TextView item_tv; public MyViewHolder(View itemView) {
super(itemView);
item_tv = (TextView) itemView.findViewById(R.id.item_tv);
}
} class FooterViewHolder extends RecyclerView.ViewHolder { public FooterViewHolder(View itemView) {
super(itemView);
}
} // 待验证
// public void delete(int position) {
// notifyItemRemoved(position);
// }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="qianfeng.com.pullrecyclerviewdemo.MainActivity"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="删除" /> <android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

footer_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="加载更多"
android:textColor="@android:color/black"
android:textSize="20sp" />
</LinearLayout>

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="vertical"
app:cardBackgroundColor="@android:color/holo_blue_light"
app:cardCornerRadius="10dp"
app:cardElevation="10dp"> <TextView
android:id="@+id/item_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:textColor="@android:color/white"
android:textSize="20sp" /> </android.support.v7.widget.CardView>

安卓高级3 RecyclerView结合SwipeRefreshLayout并添加上拉的更多相关文章

  1. 安卓高级3 RecyclerView 和cardView使用案例

    cardView: 添加依赖:在Studio搜索cardview即可 在V7包中 或者直接在gradle中添加 compile 'com.android.support:cardview-v7:24. ...

  2. 安卓开发——WebView+Recyclerview文章详情页,解决高度问题

    安卓开发--WebView+Recyclerview文章详情页,解决高度问题 最近在写一个APP时,需要显示文章详情页,准备使用WebView和RecyclerView实现上面文章,下面评论.出现了W ...

  3. 使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突

    使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种冲突 如果你还在为处理滑动冲突而发愁,那么你需要静 ...

  4. mysql添加上log_bin步骤如下

    mysql添加上log_bin步骤如下

  5. Android 高级编程 RecyclerView 控件的使用

    RecyclerView 是Android 新添加的一个用来取代ListView的控件,它的灵活性与可替代性比listview更好. 看一下继承关系: ava.lang.Object    ↳ and ...

  6. Android开发——使用高级的RecyclerView实现侧滑菜单删除功能(SwipeRecyclerView)

    使用之前,先简单介绍一下这个SwipeRecyclerView,这是严大(严振杰)基于RecyclerView的进行修改和封装的高级RecyclerView,其可以实现像QQ聊天界面的侧滑删除菜单,和 ...

  7. 安卓高级3 Android应用Design Support Library完全使用实例

    原作者:http://www.open-open.com/lib/view/open1433385856119.html 1 背景 上周一年一度的Google IO全球开发者大会刚刚结束,Google ...

  8. 使用SwipeRefreshLayout和RecyclerView实现仿“简书”下拉刷新和上拉载入很多其它

    一.概述 本篇博客介绍的是怎样使用SwipeRefreshLayout和RecyclerView实现高仿简书Android端的下拉刷新和上拉载入很多其它的效果. 依据效果图能够发现,本案例实现了例如以 ...

  9. android实现倒计时,最简单实现RecyclerView倒计时+SwipeRefreshLayout下拉刷新

    先上效果图: RecyclerView + SwipeRefreshLayout 实现倒计时效果 MainActivity.java package top.wintp.counttimedemo1; ...

随机推荐

  1. Dev GridControl GridView 属性大全[中文解释]

    Options 选项 OptionsBehavior 视图的行为选项 AllowAddRows 允许添加新数据行 AllowDeleteRows 允许删除数据行 AllowIncrementalSea ...

  2. Jenkins持续集成演示

    1.去我们的仓库修改一下代码 为了节约时间,我直接在网页上改了. 我们把布局页的footer信息改一下: 然后提交. 2.切换到Jenkins可以看到已经在构建了 等待构建完成. 3.访问我们部署的地 ...

  3. [LeetCode] Redundant Connection 冗余的连接

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  4. C# 获取字符串中的英文字母

    string str20 = "ABC123"; string strSplit1,strSplit2; //取出字符串中所有的英文字母 strSplit1 = Regex.Rep ...

  5. python 网络爬虫(一)爬取天涯论坛评论

    我是一个大二的学生,也是刚接触python,接触了爬虫感觉爬虫很有趣就爬了爬天涯论坛,中途碰到了很多问题,就想把这些问题分享出来, 都是些简单的问题,希望大佬们以宽容的眼光来看一个小菜鸟

  6. [BZOJ 4589]Hard Nim

    Description 题库链接 两人玩 \(nim\) 游戏,\(n\) 堆石子,每堆石子初始数量是不超过 \(m\) 的质数,那么后手必胜的方案有多少种.对 \(10^9+7\) 取模. \(1\ ...

  7. [BZOJ 2144]跳跳棋

    Description 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他 ...

  8. UVALive - 3530:Martian Mining

    dp 可以发现,对于(i,j),要么把它运上去,那么把它运到左边,枚举一下即可 #include<cstdio> #include<cstdlib> #include<a ...

  9. C++Primer学习——未定义行为

    定义: 主要是求值顺序的问题 int i = f1() + f2();          //我们无法知道是f1 还是 f2先被调用 而且求值顺序和优先级和结合律无关,比如: f() + g()*h( ...

  10. bzoj1791: [Ioi2008]Island 岛屿 单调队列优化dp

    1791: [Ioi2008]Island 岛屿 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1826  Solved: 405[Submit][S ...