sponsored links

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的布局做一些处理了,比如提示“加载中,”,“已经全部加载”等信息。更加灵活一点


Tags: