1.添加加载更多布局
1_初始化和隐藏代码
在RefreshListVIEw构造方法中调用
private voID initFooterVIEw(Context context) {VIEw footerVIEw = VIEw.inflate(context,R.layout.refresh_ListvIEw_footer,null);//隐藏代码footerVIEw.measure(0,0);int footerVIEwHeight = footerVIEw.getMeasuredHeight();footerVIEw.setpadding(0,-footerVIEwHeight,0);this.addFooterVIEw(footerVIEw);}
2_布局文件refresh_ListvIEw_footer.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:gravity="center"androID:orIEntation="horizontal" ><ProgressbarandroID:layout_margin="5dip"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:indeterminateDrawable="@drawable/custom_progressbar" /><TextVIEwandroID:layout_marginleft="10dip"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:text="加载更多中..."androID:textcolor="#ff0000"androID:textSize="25sp" /></linearLayout>
2.拖动到底部的时候
/*** 菜单页面对应的新闻页签页面* 总共有12个* @author administrator**/public class TabMenuDetailPager extends MenuDetailBasePagerimplements OnPagechangelistener {/*** 新闻中心-新闻菜单对应的标签对应的数据*/private NewCenterTag newCenterTag;......................./*** 加载更多数据的URL*/private String moreUrl;/*** 是否加载更多数据中*/protected boolean isLoadingMore = false;...................@OverrIDepublic VIEw initVIEw() {VIEw vIEw = VIEw.inflate(mActivity,R.layout.tab_detail,null);//把VIEw注入到XUtils框架中VIEwUtils.inject(this,vIEw); .......................... //设置监听下拉刷新mListVIEw.setonRefreshListener(new OnRefreshListener() {@OverrIDepublic voID onPullDownRefresh() {isPullDownRefreshing = true;getDataFromNet();}@OverrIDepublic voID onLoadingMore() {if(TextUtils.isEmpty(moreUrl)){Toast.makeText(mActivity,"没有更多数据了",1).show();mListVIEw.onRefreshFinish(false);}else{//有更多数据,要加载更多数据了getMoreDataFromNet();}}});return vIEw;}/*** 加载更多数据*/protected voID getMoreDataFromNet() {httpUtils httpUtils = new httpUtils();httpUtils.send(httpMethod.GET,moreUrl,new RequestCallBack<String>() {@OverrIDepublic voID onSuccess(ResponseInfo<String> responseInfo) {System.out.println("加载更多数据成功:"+responseInfo.result);mListVIEw.onRefreshFinish(false);isLoadingMore = true;processData(responseInfo.result);}@OverrIDepublic voID onFailure(httpException error,String msg) {mListVIEw.onRefreshFinish(false);System.out.println("加载更多数据失败:"+ msg);}}); }/*** 处理和解析Json数据* @param Json*/protected voID processData(String Json) {TabDetailBean bean = parserjson(Json);if(!isLoadingMore){System.out.println(bean.data.news.get(0).Title);topnews = bean.data.topnews;//给VIEwPager设置适配器TabDetailAdapter adapter = new TabDetailAdapter();mVIEwPager.setAdapter(adapter);// 把所有的VIEw清除ll_point_group.removeAllVIEws();for(int i=0;i<topnews.size();i++){VIEw point = new VIEw(mActivity);LayoutParams params = new LayoutParams(5,5) ;point.setBackgroundResource(R.drawable.tab_detail_point_bg);if(i!=0){params.leftmargin = 10;}point.setEnabled(false);point.setLayoutParams(params);ll_point_group.addVIEw(point);}prevIoUsPointposition = 0;//设置默认的图片描述和指示点mtv_Title_description.setText(topnews.get(prevIoUsPointposition).Title);ll_point_group.getChildAt(prevIoUsPointposition).setEnabled(true);//设置页面改变的监听mVIEwPager.setonPagechangelistener(this);//设置适配器和对应的数据newsLists = bean.data.news;ListVIEwAdapter = new ListVIEwAdapter();mListVIEw.setAdapter(ListVIEwAdapter);// mListVIEw.addheaderVIEw(v) ;//把一个视图一头的方式添加到ListVIEw中 }else{//把列表新闻取出来,在加载到以前的集合中,在刷新数据isLoadingMore = false;List<News>moreDatanews = bean.data.news;newsLists.addAll(moreDatanews);ListVIEwAdapter.notifyDataSetChanged();//刷新数据}}................/*** 用Gson开源项目解析Json* @param Json*/private TabDetailBean parserjson(String Json) {Gson gson = new Gson();TabDetailBean bean = gson.fromJson(Json,TabDetailBean.class);moreUrl = bean.data.more;if(TextUtils.isEmpty(moreUrl)){moreUrl = null;}else{moreUrl = ConstantUtils.server_url+moreUrl;}return bean;}@OverrIDepublic voID onPageScrollStateChanged(int arg0) {// Todo auto-generated method stub}@OverrIDepublic voID onPageScrolled(int arg0,float arg1,int arg2) {// Todo auto-generated method stub}...............}
3.完整代码
package com.atguigu.refreshListvIEw;import androID.content.Context;import androID.util.AttributeSet;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.animation.Animation;import androID.vIEw.animation.RotateAnimation;import androID.Widget.AbsListVIEw;import androID.Widget.ImageVIEw;import androID.Widget.linearLayout;import androID.Widget.ListVIEw;import androID.Widget.Progressbar;import androID.Widget.TextVIEw;import java.text.SimpleDateFormat;import java.util.Date;/*** 作用:自定义下拉刷新的ListVIEw*/public class RefreshListvIEw extends ListVIEw {/*** 下拉刷新和顶部轮播图*/private linearLayout headerVIEw;/*** 下拉刷新控件*/private VIEw ll_pull_down_refresh;private ImageVIEw iv_arrow;private Progressbar pb_status;private TextVIEw tv_status;private TextVIEw tv_time;/*** 下拉刷新控件的高*/private int pullDownRefreshHeight;/*** 下拉刷新*/public static final int PulL_DOWN_REFRESH = 0;/*** 手松刷新*/public static final int RELEASE_REFRESH = 1;/*** 正在刷新*/public static final int REFRESHING = 2;/*** 当前状态*/private int currentStatus = PulL_DOWN_REFRESH;private Animation upAnimation;private Animation downAnimation;/*** 加载更多的控件*/private VIEw footerVIEw;/*** 加载更多控件高*/private int footerVIEwHeight;/*** 是否已经加载更多*/private boolean isLoadMore = false;/*** 顶部轮播图部分*/private VIEw topNewsVIEw;/*** ListVIEw在Y轴上的坐标*/private int ListVIEwOnScreenY = -1;public RefreshListvIEw(Context context) {this(context,null);}public RefreshListvIEw(Context context,AttributeSet attrs) {this(context,attrs,0);}public RefreshListvIEw(Context context,AttributeSet attrs,int defStyleAttr) {super(context,defStyleAttr);initheaderVIEw(context);initAnimation();initFooterVIEw(context);}private voID initFooterVIEw(Context context) {footerVIEw = VIEw.inflate(context,R.layout.refresh_footer,null);footerVIEw.measure(0,0);footerVIEwHeight = footerVIEw.getMeasuredHeight();footerVIEw.setpadding(0,0);//ListVIEw添加footeraddFooterVIEw(footerVIEw);//监听ListVIEw的滚动setonScrollListener(new MyOnScrollListener());}/*** 添加顶部轮播图* @param topNewsVIEw*/public voID addtopNewsVIEw(VIEw topNewsVIEw) {if(topNewsVIEw != null){this.topNewsVIEw =topNewsVIEw;headerVIEw.addVIEw(topNewsVIEw);}}class MyOnScrollListener implements OnScrollListener{@OverrIDepublic voID onScrollStateChanged(AbsListVIEw vIEw,int scrollState) {//当静止或者惯性滚动的时候if(scrollState ==OnScrollListener.SCRolL_STATE_IDLE||scrollState ==OnScrollListener.SCRolL_STATE_FliNG){//并且是最后一条可见if(getLastVisibleposition()>=getCount()-1){//1.显示加载更多布局footerVIEw.setpadding(8,8,8);//2.状态改变isLoadMore = true;//3.回调接口if(mOnRefreshListener != null){mOnRefreshListener.onLoadMore();}}}}@OverrIDepublic voID onScroll(AbsListVIEw vIEw,int firstVisibleItem,int visibleItemCount,int totalitemCount) {}}private voID initAnimation() {upAnimation = new RotateAnimation(0,-180,RotateAnimation.relative_TO_SELF,0.5f,0.5f);upAnimation.setDuration(500);upAnimation.setFillAfter(true);downAnimation = new RotateAnimation(-180,-360,0.5f);downAnimation.setDuration(500);downAnimation.setFillAfter(true);}private voID initheaderVIEw(Context context) {headerVIEw = (linearLayout) VIEw.inflate(context,R.layout.refresh_header,null);//下拉刷新控件ll_pull_down_refresh = headerVIEw.findVIEwByID(R.ID.ll_pull_down_refresh);iv_arrow = (ImageVIEw) headerVIEw.findVIEwByID(R.ID.iv_arrow);pb_status = (Progressbar) headerVIEw.findVIEwByID(R.ID.pb_status);tv_status = (TextVIEw) headerVIEw.findVIEwByID(R.ID.tv_status);tv_time = (TextVIEw) headerVIEw.findVIEwByID(R.ID.tv_time);//测量ll_pull_down_refresh.measure(0,0);pullDownRefreshHeight = ll_pull_down_refresh.getMeasuredHeight();//默认隐藏下拉刷新控件// VIEw.setpadding(0,-控件高,0,0);//完全隐藏//VIEw.setpadding(0,0,0,0);//完全显示ll_pull_down_refresh.setpadding(0,-pullDownRefreshHeight,0);//添加ListVIEw的头addheaderVIEw(headerVIEw);}private float startY = -1;@OverrIDepublic boolean ontouchEvent(MotionEvent ev) {switch (ev.getAction()) {case MotionEvent.ACTION_DOWN://1.记录起始坐标startY = ev.getY();break;case MotionEvent.ACTION_MOVE:if (startY == -1) {startY = ev.getY();}//判断顶部轮播图是否完全显示,只有完全显示才会有下拉刷新boolean isdisplaytopNews = isdisplaytopNews();if(!isdisplaytopNews){//加载更多break;}//如果是正在刷新,就不让再刷新了if (currentStatus == REFRESHING) {break;}//2.来到新的坐标float endY = ev.getY();//3.记录滑动的距离float distanceY = endY - startY;if (distanceY > 0) {//下拉//int paddingtop = -控件高 + distanceY;int paddingtop = (int) (-pullDownRefreshHeight + distanceY);if (paddingtop < 0 && currentStatus != PulL_DOWN_REFRESH) {//下拉刷新状态currentStatus = PulL_DOWN_REFRESH;//更新状态refreshVIEwState();} else if (paddingtop > 0 && currentStatus != RELEASE_REFRESH) {//手松刷新状态currentStatus = RELEASE_REFRESH;//更新状态refreshVIEwState();}ll_pull_down_refresh.setpadding(0,paddingtop,0);//VIEw.setpadding(0,paddingtop,0,0);//动态的显示下拉刷新控件}break;case MotionEvent.ACTION_UP:startY = -1;if (currentStatus == PulL_DOWN_REFRESH) {// VIEw.setpadding(0,0);//完全隐藏ll_pull_down_refresh.setpadding(0,0);} else if (currentStatus == RELEASE_REFRESH) {//设置状态为正在刷新currentStatus = REFRESHING;refreshVIEwState();// VIEw.setpadding(0,0);//回调接口if (mOnRefreshListener != null) {mOnRefreshListener.onPullDownRefresh();}}break;}return super.ontouchEvent(ev);}/*** 判断是否完全显示顶部轮播图* 当ListVIEw在屏幕上的Y轴坐标小于或者等于顶部轮播图在Y轴的坐标的时候,顶部轮播图完全显示* @return*/private boolean isdisplaytopNews() {if(topNewsVIEw != null){//1.得到ListVIEw在屏幕上的坐标int[] location = new int[2];if(ListVIEwOnScreenY == -1){getLocationOnScreen(location);ListVIEwOnScreenY = location[1];}//2.得到顶部轮播图在屏幕上的坐标topNewsVIEw.getLocationOnScreen(location);int topNewsVIEwOnScreenY = location[1];// if(ListVIEwOnScreenY <= topNewsVIEwOnScreenY){// return true;// }else{// return false;// }return ListVIEwOnScreenY <= topNewsVIEwOnScreenY;}else{return true;}}private voID refreshVIEwState() {switch (currentStatus) {case PulL_DOWN_REFRESH://下拉刷新状态iv_arrow.startAnimation(downAnimation);tv_status.setText("下拉刷新...");break;case RELEASE_REFRESH://手松刷新状态iv_arrow.startAnimation(upAnimation);tv_status.setText("手松刷新...");break;case REFRESHING://正在刷新状态tv_status.setText("正在刷新...");pb_status.setVisibility(VISIBLE);iv_arrow.clearanimation();iv_arrow.setVisibility(GONE);break;}}/*** 当联网成功和失败的时候回调该方法* 用户刷新状态的还原** @param sucess*/public voID onRefreshFinish(boolean sucess) {if(isLoadMore){//加载更多isLoadMore = false;//隐藏加载更多布局footerVIEw.setpadding(0,0);}else{//下拉刷新tv_status.setText("下拉刷新...");currentStatus = PulL_DOWN_REFRESH;iv_arrow.clearanimation();pb_status.setVisibility(GONE);iv_arrow.setVisibility(VISIBLE);//隐藏下拉刷新控件ll_pull_down_refresh.setpadding(0,0);if (sucess) {//设置最新更新时间tv_time.setText("上次更新时间:" + getSystemTime());}}}/*** 得到当前AndroID系统的时间** @return*/private String getSystemTime() {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");return format.format(new Date());}/*** 监听控件的刷新*/public interface OnRefreshListener {/*** 当下拉刷新的时候回调这个方法*/public voID onPullDownRefresh();/**当加载更多的时候回调这个方法*/public voID onLoadMore();}private OnRefreshListener mOnRefreshListener;/*** 设置监听刷新,由外界设置*/public voID setonRefreshListener(OnRefreshListener l) {this.mOnRefreshListener = l;}}
以上所述是小编给大家介绍的AndroID 仿硅谷新闻下拉刷新/上拉加载更多,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android 仿硅谷新闻下拉刷新/上拉加载更多全部内容,希望文章能够帮你解决Android 仿硅谷新闻下拉刷新/上拉加载更多所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)