Android App使用RecyclerView实现上拉和下拉刷新的方法
关于RecyclerView
RecyclerView在Android5.0以来被引入,以前经常使用的ListView继承的是AbsListView,而RecyclerView则直接继承ViewGroup,并实现了ScrollingView和NestedScrollingChild接口,RecyclerView相比ListView,是一次彻底的改变。
RecyclerView比listview更先进更灵活,对于很多的视图它就是一个容器,可以有效的重用和滚动。当数据动态变化的时候请使用它。
RecyclerView使用起来很方便因为它提供:
1、它为item的定位提供一个layoutmanager
2、为item的操作提供一个缺省的animations
3、还可以灵活地定义这个小部件的自定义布局管理器和动画
实现上拉刷新和下拉刷新
布局文件:
<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 protectedvoidonCreate(BundlesavedInstanceState){ 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(newRecyclerView.OnScrollListener(){ @Override publicvoidonScrollStateChanged(RecyclerViewrecyclerView, intnewState){ super.onScrollStateChanged(recyclerView,newState); if(newState==RecyclerView.SCROLL_STATE_IDLE &&lastVisibleItem+1==adapter.getItemCount()){ mSwipeRefreshWidget.setRefreshing(true); //此处在现实项目中,请换成网络请求数据代码,sendRequest..... handler.sendEmptyMessageDelayed(0,3000); } } @Override publicvoidonScrolled(RecyclerViewrecyclerView,intdx,intdy){ super.onScrolled(recyclerView,dx,dy); lastVisibleItem=mLayoutManager.findLastVisibleItemPosition(); } }); mRecyclerView.setHasFixedSize(true); mLayoutManager=newLinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.setItemAnimator(newDefaultItemAnimator()); adapter=newSampleAdapter(); mRecyclerView.setAdapter(adapter); //此处在现实项目中,请换成网络请求数据代码,sendRequest..... handler.sendEmptyMessageDelayed(0,3000); }
SwipeRefreshLayout里面需要注意的Api:
1、setOnRefreshListener(OnRefreshListenerlistener) 设置下拉监听,当用户下拉的时候会去执行回调
2、setColorSchemeColors(int...colors)设置进度条的颜色变化,最多可以设置4种颜色
3、setProgressViewOffset(booleanscale,intstart,intend)调整进度条距离屏幕顶部的距离
4、setRefreshing(booleanrefreshing)设置SwipeRefreshLayout当前是否处于刷新状态,一般是在请求数据的时候设置为true,在数据被加载到View中后,设置为false。
RecyclerView的实现:
第一种,下拉刷新和上拉刷新都用SwipeRefreshLayout自带的进度条
mRecyclerView=(RecyclerView)findViewById(android.R.id.list); mRecyclerView.setOnScrollListener(newRecyclerView.OnScrollListener(){ @Override publicvoidonScrollStateChanged(RecyclerViewrecyclerView, intnewState){ super.onScrollStateChanged(recyclerView,newState); if(newState==RecyclerView.SCROLL_STATE_IDLE &&lastVisibleItem+1==adapter.getItemCount()){ mSwipeRefreshWidget.setRefreshing(true); //此处在现实项目中,请换成网络请求数据代码,sendRequest..... handler.sendEmptyMessageDelayed(0,3000); } } @Override publicvoidonScrolled(RecyclerViewrecyclerView,intdx,intdy){ super.onScrolled(recyclerView,dx,dy); lastVisibleItem=mLayoutManager.findLastVisibleItemPosition(); } }); mRecyclerView.setHasFixedSize(true); mLayoutManager=newLinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.setItemAnimator(newDefaultItemAnimator()); adapter=newSampleAdapter(); mRecyclerView.setAdapter(adapter);
第二种实现下拉刷新用SwipeRefreshLayout自带的进度条,上拉刷新用类似ListView的刷新提示“加载中”等信息。
我们可以给RecyclerView也添加一个类似FooterView的item。
我们在Adapter中实现:
publicclassSampleAdapterextendsRecyclerView.Adapter<ViewHolder>{ privateList<Integer>list; privatestaticfinalintTYPE_ITEM=0; privatestaticfinalintTYPE_FOOTER=1; publicList<Integer>getList(){ returnlist; } publicSampleAdapter(){ list=newArrayList<Integer>(); } //RecyclerView的count设置为数据总条数+1(footerView) @Override publicintgetItemCount(){ returnlist.size()+1; } @Override publicintgetItemViewType(intposition){ //最后一个item设置为footerView if(position+1==getItemCount()){ returnTYPE_FOOTER; }else{ returnTYPE_ITEM; } } @Override publicvoidonBindViewHolder(ViewHolderholder,finalintposition){ if(holderinstanceofItemViewHolder){ ((ItemViewHolder)holder).textView.setText(String.valueOf(list .get(position))); } } @Override publicViewHolderonCreateViewHolder(ViewGroupparent,intviewType){ if(viewType==TYPE_ITEM){ Viewview=LayoutInflater.from(parent.getContext()).inflate( R.layout.list_item_text,null); view.setLayoutParams(newLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); returnnewItemViewHolder(view); } //type==TYPE_FOOTER返回footerView elseif(viewType==TYPE_FOOTER){ Viewview=LayoutInflater.from(parent.getContext()).inflate( R.layout.footerview,null); view.setLayoutParams(newLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); returnnewFooterViewHolder(view); } returnnull; } classFooterViewHolderextendsViewHolder{ publicFooterViewHolder(Viewview){ super(view); } } classItemViewHolderextendsViewHolder{ TextViewtextView; publicItemViewHolder(Viewview){ super(view); textView=(TextView)view.findViewById(R.id.text); } } }
这样我们就可以针对footerview的布局做一些处理了,比如提示“加载中,”,“已经全部加载”等信息。更加灵活一点