安卓高级3 RecyclerView结合SwipeRefreshLayout并添加上拉
目录结构:
效果图:
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并添加上拉的更多相关文章
- 安卓高级3 RecyclerView 和cardView使用案例
cardView: 添加依赖:在Studio搜索cardview即可 在V7包中 或者直接在gradle中添加 compile 'com.android.support:cardview-v7:24. ...
- 安卓开发——WebView+Recyclerview文章详情页,解决高度问题
安卓开发--WebView+Recyclerview文章详情页,解决高度问题 最近在写一个APP时,需要显示文章详情页,准备使用WebView和RecyclerView实现上面文章,下面评论.出现了W ...
- 使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突
使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种冲突 如果你还在为处理滑动冲突而发愁,那么你需要静 ...
- mysql添加上log_bin步骤如下
mysql添加上log_bin步骤如下
- Android 高级编程 RecyclerView 控件的使用
RecyclerView 是Android 新添加的一个用来取代ListView的控件,它的灵活性与可替代性比listview更好. 看一下继承关系: ava.lang.Object ↳ and ...
- Android开发——使用高级的RecyclerView实现侧滑菜单删除功能(SwipeRecyclerView)
使用之前,先简单介绍一下这个SwipeRecyclerView,这是严大(严振杰)基于RecyclerView的进行修改和封装的高级RecyclerView,其可以实现像QQ聊天界面的侧滑删除菜单,和 ...
- 安卓高级3 Android应用Design Support Library完全使用实例
原作者:http://www.open-open.com/lib/view/open1433385856119.html 1 背景 上周一年一度的Google IO全球开发者大会刚刚结束,Google ...
- 使用SwipeRefreshLayout和RecyclerView实现仿“简书”下拉刷新和上拉载入很多其它
一.概述 本篇博客介绍的是怎样使用SwipeRefreshLayout和RecyclerView实现高仿简书Android端的下拉刷新和上拉载入很多其它的效果. 依据效果图能够发现,本案例实现了例如以 ...
- android实现倒计时,最简单实现RecyclerView倒计时+SwipeRefreshLayout下拉刷新
先上效果图: RecyclerView + SwipeRefreshLayout 实现倒计时效果 MainActivity.java package top.wintp.counttimedemo1; ...
随机推荐
- SQL 语句 (二) --- SELECT
1 完整句法: SELECT [ ALL | DISTINCT TOP n [] WITH TIES select_list [INTO [new_table_name] ] [FROM {table ...
- 关于python 使用腾讯云OCR 通用印刷体识别
腾讯的python SDK没有通用印刷体识别,所以参考了别人识别网上图片的方式:https://www.cnblogs.com/semishigure/p/7690789.html 但是咱们使用的基本 ...
- 设计APP时我们该怎么做
不得不承认,手机APP已经渗透到我们的生活中,根据数据统计,人们每天平均有3.9个小时是花费在手机APP的使用上的,可以预见,手机APP正在改变我们的生活.手机APP受到人们的欢迎,很多商家也看到了其 ...
- 如何降低移动APP的开发成本
在当下竞争激烈的商业世界中,移动APP开发是您业务的有利补充.移动APP可通过吸引新客户和保留现有客户,帮助公司成功开展业务.定制一个属于自己公司的移动APP扮演着重要角色,手机APP可以说通过轻松处 ...
- [LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- 计蒜客NOIP模拟赛D2T3 数三角形
刚刚上高中的洁洁在学习组合数学的过程中遇到一道麻烦的题目,她希望你能帮助她解决.给定一张无向完全图 G,其中大部分边被染成蓝色,但也有一些边被染成红色或者绿色.现在,洁洁需要给这张图的多样性进行打分. ...
- ●Joyoi Normal
题链: http://www.joyoi.cn/problem/tyvj-1953题解: 定义d(u,v)这个函数,满足: d(u,v)=1,当且仅当在点分树中,u是v的祖先 d(u,v)=0,其它情 ...
- ●BZOJ 4176 Lucas的数论
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4176 题解: 莫比乌斯反演,杜教筛 首先有这么一个结论: 令d(n)表示n的约数的个数(就是 ...
- POJ 1486二分图的必要边
题意:有n个大小不等透明的幻灯片(只有轮廓和上面的数字可见)A.B.C.D.E…按顺序叠放在一起,现在知道每个幻灯片大小,由于幻灯片是透明的,所以能看到幻灯片上的数字(给出了每个数字的坐标,但不知道这 ...
- ●BZOJ 4710 [Jsoi2011]分特产
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4710 题解: 容斥,组合先看看这个方案数的计算:把 M 个相同的东西分给 N 个人,每个人可 ...