我的思路是:
1:重写一个继承了SwipeRefreshLayout的类的MySwipeRefreshLayout,使得这个MySwipeRefreshLayout,在拦截的方法onInterruptEvent里面添加一个判断,如果按下的地方在屏幕的上方的10分之一的地方拦截否则不拦截。
2:将上面的MySwipeRefreshLayout添加到布局文件中
3:给RecyclerView 添加一个
addOnScrollListener(new RecyclerView.OnScrollListener(){里面有两个方法onScrollStateChangedonScrolled}
mouge_lei_bie_detail_recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){/** * 在这个方法里面判断当前状态是不是SCROLL_STATE_IDLE,并且 最后一个可见的Item也就是lastVisibleItem * 是倒数第二个 数据 ,如果两个条件都满足,那么进行上拉加载的操作,就是发送一个 新的handle的消息。 * 这个消息的what是ADD */ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {super.onScrollStateChanged(recyclerView, newState);if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleItem+1==adapter.getItemCount()){Message message = new Message();message.what=ADD;handler.sendMessage(message);}}/** * 通过recyclerView_layoutManager,这个recyclerView_layoutManager是通过下面的两句话来设置的 * recyclerView_layoutManager = new LinearLayoutManager(this); * mouge_lei_bie_detail_recyclerView.setLayoutManager(recyclerView_layoutManager); * 得到最后一个可见的item的位置,并且把这个传递给变量lastVisibleItem , */ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {super.onScrolled(recyclerView, dx, dy);lastVisibleItem = recyclerView_layoutManager.findLastVisibleItemPosition(); }});
参考:http://blog.csdn.net/dalancon/article/details/46125667
SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新
版权声明:本文为博主原创文章,未经博主允许不得转载。
SwipeRefreshLayout 是谷歌公司推出的用于下拉刷新的控件,
SwipeRefreshLayout已经被放到了sdk中,在Version 19.1之后SwipeRefreshLayout 被放到support v4中。
源码在
SDK\sdk\extras\android\support\v4\src\java\android\support\v4\widget\
SwipeRefreshLayout.java
谷歌公司只提供了下拉刷新的功能,RecyclerView的出现基本就是为了替代ListView,GridView的。
今天说一下最常见的下拉刷新 和 上拉刷新的功能。
布局文件:
- <android.support.v4.widget.SwipeRefreshLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/swipe_refresh_widget"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
-
- <android.support.v7.widget.RecyclerView
- android:id="@android:id/list"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:cacheColorHint="@null"
- android:scrollbars="vertical" />
-
- </android.support.v4.widget.SwipeRefreshLayout>
在Activity中引用这个布局并初始化
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- mSwipeRefreshWidget = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
- mRecyclerView = (RecyclerView) findViewById(android.R.id.list);
-
- mSwipeRefreshWidget.setColorScheme(R.color.color1, R.color.color2,
- R.color.color3, R.color.color4);
- mSwipeRefreshWidget.setOnRefreshListener(this);
-
- // 这句话是为了,第一次进入页面的时候显示加载进度条
- mSwipeRefreshWidget.setProgressViewOffset(false, 0, (int) TypedValue
- .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
- .getDisplayMetrics()));
-
- mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
-
- @Override
- public void onScrollStateChanged(RecyclerView recyclerView,
- int newState) {
- super.onScrollStateChanged(recyclerView, newState);
- if (newState == RecyclerView.SCROLL_STATE_IDLE
- && lastVisibleItem + 1 == adapter.getItemCount()) {
- mSwipeRefreshWidget.setRefreshing(true);
- // 此处在现实项目中,请换成网络请求数据代码,sendRequest .....
- handler.sendEmptyMessageDelayed(0, 3000);
- }
- }
-
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
- }
-
- });
-
- mRecyclerView.setHasFixedSize(true);
- mLayoutManager = new LinearLayoutManager(this);
- mRecyclerView.setLayoutManager(mLayoutManager);
- mRecyclerView.setItemAnimator(new DefaultItemAnimator());
-
- adapter = new SampleAdapter();
- mRecyclerView.setAdapter(adapter);
-
- // 此处在现实项目中,请换成网络请求数据代码,sendRequest .....
- handler.sendEmptyMessageDelayed(0, 3000);
- }
SwipeRefreshLayout里面需要注意的Api:
1、
setOnRefreshListener( OnRefreshListener
listener) 设置下拉监听,当用户下拉的时候会去执行回调
2、
setColorSchemeColors(int... colors) 设置 进度条的颜色变化,最多可以设置4种颜色
3、
setProgressViewOffset(boolean scale, int start, int end) 调整进度条距离屏幕顶部的距离
4、
setRefreshing(boolean refreshing) 设置SwipeRefreshLayout当前是否处于刷新状态,一般是在请求数据的时候设置为true,在数据被加载到View中后,设置为false。
RecyclerView的实现:
第一种,下拉刷新和上拉刷新都用SwipeRefreshLayout
自带的进度条
- mRecyclerView = (RecyclerView) findViewById(android.R.id.list);
- mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
-
- @Override
- public void onScrollStateChanged(RecyclerView recyclerView,
- int newState) {
- super.onScrollStateChanged(recyclerView, newState);
- if (newState == RecyclerView.SCROLL_STATE_IDLE
- && lastVisibleItem + 1 == adapter.getItemCount()) {
- mSwipeRefreshWidget.setRefreshing(true);
- // 此处在现实项目中,请换成网络请求数据代码,sendRequest .....
- handler.sendEmptyMessageDelayed(0, 3000);
- }
- }
-
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
- }
- });
-
- mRecyclerView.setHasFixedSize(true);
- mLayoutManager = new LinearLayoutManager(this);
- mRecyclerView.setLayoutManager(mLayoutManager);
- mRecyclerView.setItemAnimator(new DefaultItemAnimator());
-
- adapter = new SampleAdapter();
- mRecyclerView.setAdapter(adapter);
第二种实现下拉刷新用SwipeRefreshLayout
自带的进度条, 上拉刷新用类似ListView的刷新 提示“加载中”等信息。
我们可以给RecyclerView 也添加一个类似FooterView的item。
我们在Adapter中实现:
- public class SampleAdapter extends RecyclerView.Adapter<ViewHolder> {
- private List<Integer> list;
-
- private static final int TYPE_ITEM = 0;
- private static final int TYPE_FOOTER = 1;
-
- public List<Integer> getList() {
- return list;
- }
-
- public SampleAdapter() {
- list = new ArrayList<Integer>();
- }
-
- // RecyclerView的count设置为数据总条数+ 1(footerView)
- @Override
- public int getItemCount() {
- return list.size() + 1;
- }
-
- @Override
- public int getItemViewType(int position) {
- // 最后一个item设置为footerView
- if (position + 1 == getItemCount()) {
- return TYPE_FOOTER;
- } else {
- return TYPE_ITEM;
- }
- }
-
- @Override
- public void onBindViewHolder(ViewHolder holder, final int position) {
- if (holder instanceof ItemViewHolder) {
- ((ItemViewHolder) holder).textView.setText(String.valueOf(list
- .get(position)));
- }
- }
-
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- if (viewType == TYPE_ITEM) {
- View view = LayoutInflater.from(parent.getContext()).inflate(
- R.layout.list_item_text, null);
- view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
- LayoutParams.WRAP_CONTENT));
- return new ItemViewHolder(view);
- }
- // type == TYPE_FOOTER 返回footerView
- else if (viewType == TYPE_FOOTER) {
- View view = LayoutInflater.from(parent.getContext()).inflate(
- R.layout.footerview, null);
- view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
- LayoutParams.WRAP_CONTENT));
- return new FooterViewHolder(view);
- }
-
- return null;
- }
-
- class FooterViewHolder extends ViewHolder {
-
- public FooterViewHolder(View view) {
- super(view);
- }
-
- }
-
- class ItemViewHolder extends ViewHolder {
- TextView textView;
-
- public ItemViewHolder(View view) {
- super(view);
- textView = (TextView) view.findViewById(R.id.text);
- }
- }
- }
这样我们就可以针对footerview的布局做一些处理了,比如提示“加载中,”,“已经全部加载”等信息。更加灵活一点
版权声明:本文为博主原创文章,未经博主允许不得转载。
源码在
SDK\sdk\extras\android\support\v4\src\java\android\support\v4\widget\
SwipeRefreshLayout.java
谷歌公司只提供了下拉刷新的功能,RecyclerView的出现基本就是为了替代ListView,GridView的。
今天说一下最常见的下拉刷新 和 上拉刷新的功能。
布局文件:
- <android.support.v4.widget.SwipeRefreshLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/swipe_refresh_widget"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <android.support.v7.widget.RecyclerView
- android:id="@android:id/list"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:cacheColorHint="@null"
- android:scrollbars="vertical" />
- </android.support.v4.widget.SwipeRefreshLayout>
在Activity中引用这个布局并初始化
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mSwipeRefreshWidget = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
- mRecyclerView = (RecyclerView) findViewById(android.R.id.list);
- mSwipeRefreshWidget.setColorScheme(R.color.color1, R.color.color2,
- R.color.color3, R.color.color4);
- mSwipeRefreshWidget.setOnRefreshListener(this);
- // 这句话是为了,第一次进入页面的时候显示加载进度条
- mSwipeRefreshWidget.setProgressViewOffset(false, 0, (int) TypedValue
- .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
- .getDisplayMetrics()));
- mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
- @Override
- public void onScrollStateChanged(RecyclerView recyclerView,
- int newState) {
- super.onScrollStateChanged(recyclerView, newState);
- if (newState == RecyclerView.SCROLL_STATE_IDLE
- && lastVisibleItem + 1 == adapter.getItemCount()) {
- mSwipeRefreshWidget.setRefreshing(true);
- // 此处在现实项目中,请换成网络请求数据代码,sendRequest .....
- handler.sendEmptyMessageDelayed(0, 3000);
- }
- }
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
- }
- });
- mRecyclerView.setHasFixedSize(true);
- mLayoutManager = new LinearLayoutManager(this);
- mRecyclerView.setLayoutManager(mLayoutManager);
- mRecyclerView.setItemAnimator(new DefaultItemAnimator());
- adapter = new SampleAdapter();
- mRecyclerView.setAdapter(adapter);
- // 此处在现实项目中,请换成网络请求数据代码,sendRequest .....
- handler.sendEmptyMessageDelayed(0, 3000);
- }
SwipeRefreshLayout里面需要注意的Api:
1、
setOnRefreshListener( OnRefreshListener
listener) 设置下拉监听,当用户下拉的时候会去执行回调
2、
setColorSchemeColors(int... colors) 设置 进度条的颜色变化,最多可以设置4种颜色
3、
setProgressViewOffset(boolean scale, int start, int end) 调整进度条距离屏幕顶部的距离
4、
setRefreshing(boolean refreshing) 设置SwipeRefreshLayout当前是否处于刷新状态,一般是在请求数据的时候设置为true,在数据被加载到View中后,设置为false。
RecyclerView的实现:
第一种,下拉刷新和上拉刷新都用SwipeRefreshLayout
自带的进度条
- mRecyclerView = (RecyclerView) findViewById(android.R.id.list);
- mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
- @Override
- public void onScrollStateChanged(RecyclerView recyclerView,
- int newState) {
- super.onScrollStateChanged(recyclerView, newState);
- if (newState == RecyclerView.SCROLL_STATE_IDLE
- && lastVisibleItem + 1 == adapter.getItemCount()) {
- mSwipeRefreshWidget.setRefreshing(true);
- // 此处在现实项目中,请换成网络请求数据代码,sendRequest .....
- handler.sendEmptyMessageDelayed(0, 3000);
- }
- }
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
- }
- });
- mRecyclerView.setHasFixedSize(true);
- mLayoutManager = new LinearLayoutManager(this);
- mRecyclerView.setLayoutManager(mLayoutManager);
- mRecyclerView.setItemAnimator(new DefaultItemAnimator());
- adapter = new SampleAdapter();
- mRecyclerView.setAdapter(adapter);
第二种实现下拉刷新用SwipeRefreshLayout
自带的进度条, 上拉刷新用类似ListView的刷新 提示“加载中”等信息。
我们可以给RecyclerView 也添加一个类似FooterView的item。
我们在Adapter中实现:
- public class SampleAdapter extends RecyclerView.Adapter<ViewHolder> {
- private List<Integer> list;
- private static final int TYPE_ITEM = 0;
- private static final int TYPE_FOOTER = 1;
- public List<Integer> getList() {
- return list;
- }
- public SampleAdapter() {
- list = new ArrayList<Integer>();
- }
- // RecyclerView的count设置为数据总条数+ 1(footerView)
- @Override
- public int getItemCount() {
- return list.size() + 1;
- }
- @Override
- public int getItemViewType(int position) {
- // 最后一个item设置为footerView
- if (position + 1 == getItemCount()) {
- return TYPE_FOOTER;
- } else {
- return TYPE_ITEM;
- }
- }
- @Override
- public void onBindViewHolder(ViewHolder holder, final int position) {
- if (holder instanceof ItemViewHolder) {
- ((ItemViewHolder) holder).textView.setText(String.valueOf(list
- .get(position)));
- }
- }
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- if (viewType == TYPE_ITEM) {
- View view = LayoutInflater.from(parent.getContext()).inflate(
- R.layout.list_item_text, null);
- view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
- LayoutParams.WRAP_CONTENT));
- return new ItemViewHolder(view);
- }
- // type == TYPE_FOOTER 返回footerView
- else if (viewType == TYPE_FOOTER) {
- View view = LayoutInflater.from(parent.getContext()).inflate(
- R.layout.footerview, null);
- view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
- LayoutParams.WRAP_CONTENT));
- return new FooterViewHolder(view);
- }
- return null;
- }
- class FooterViewHolder extends ViewHolder {
- public FooterViewHolder(View view) {
- super(view);
- }
- }
- class ItemViewHolder extends ViewHolder {
- TextView textView;
- public ItemViewHolder(View view) {
- super(view);
- textView = (TextView) view.findViewById(R.id.text);
- }
- }
- }
这样我们就可以针对footerview的布局做一些处理了,比如提示“加载中,”,“已经全部加载”等信息。更加灵活一点