SwipeRefreshLayout+RecyclerView实现上拉刷新和下拉刷新功能
SwipeRefreshLayout是谷歌公司推出的用于下拉刷新的控件,SwipeRefreshLayout已经被放到了sdk中,在Version19.1之后SwipeRefreshLayout 被放到supportv4中。
源码在SDK\sdk\extras\android\support\v4\src\java\android\support\v4\widget\SwipeRefreshLayout.java
谷歌公司只提供了下拉刷新的功能,RecyclerView的出现基本就是为了替代ListView,GridView的。
今天说一下最常见的下拉刷新 和上拉刷新的功能。
布局文件:
在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(OnRefreshListener listener) 设置下拉监听,当用户下拉的时候会去执行回调
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{ privateList list; privatestaticfinalintTYPE_ITEM=0; privatestaticfinalintTYPE_FOOTER=1; publicList getList(){ returnlist; } publicSampleAdapter(){ list=newArrayList (); } //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的布局做一些处理了,比如提示“加载中,”,“已经全部加载”等信息。更加灵活一点。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。