欢迎访问 生活随笔!

ag凯发k8国际

当前位置: ag凯发k8国际 > 运维知识 > android >内容正文

android

android 博客园客户端 (八) 下拉刷新、分页,asynctask -ag凯发k8国际

发布时间:2024/10/12 android 14 豆豆
ag凯发k8国际 收集整理的这篇文章主要介绍了 android 博客园客户端 (八) 下拉刷新、分页,asynctask 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

一直以来,无论是博客、新闻、还是推荐用户列表。只能加载固定的数量,也没有刷新功能。

为了实现这个功能,也试过很多第三方的开源控件,如pulltorefreshlistvie等。无意中发现了google官方发布了一个新的控件(swiperefreshlayout),支持下拉刷新,这个控件在google应用中都有出现过。效果也是非常的不错。

具体的使用方法及代码如下:

1 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:orientation="vertical" > 5 6 <android.support.v4.widget.swiperefreshlayout 7 android:id="@ id/swiperefreshlayouthomeblog" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" > 10 11 <listview 12 android:id="@ id/listviewhomeblog" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" > 15 listview> 16 android.support.v4.widget.swiperefreshlayout> 17 18 linearlayout>

需要注意的一点就是swiperefreshlayout中只能包含一个控件。其他属性和其他layout大体相同。

第一点,下拉刷新:

使用的话,需要在activity或者fragment中导入并实现android.support.v4.widget.swiperefreshlayout.onrefreshlistener接口,并且要重写onrefresh()方法。

第二点,分页处理:

需要实现onscrolllistener接口,并且重写和onscrollstatechangedonscroll和两个方法。

具体代码如下

1 package com.arlen.cnblogs.fragment; 2 3 import java.util.arraylist; 4 import java.util.list; 5 6 import android.content.intent; 7 import android.os.bundle; 8 import android.support.v4.app.fragment; 9 import android.support.v4.widget.swiperefreshlayout; 10 import android.support.v4.widget.swiperefreshlayout.onrefreshlistener; 11 import android.util.log; 12 import android.view.layoutinflater; 13 import android.view.view; 14 import android.view.viewgroup; 15 import android.widget.abslistview; 16 import android.widget.abslistview.onscrolllistener; 17 import android.widget.adapterview; 18 import android.widget.adapterview.onitemclicklistener; 19 import android.widget.adapterview.onitemlongclicklistener; 20 import android.widget.listview; 21 22 import com.arlen.cnblogs.blogactivity; 23 import com.arlen.cnblogs.r; 24 import com.arlen.cnblogs.adapter.bloglistadapter; 25 import com.arlen.cnblogs.entity.blog; 26 import com.arlen.cnblogs.task.bloglisttask; 27 import com.arlen.cnblogs.utils.appmacros; 28 import com.arlen.cnblogs.utils.apputils; 29 30 public class bloghomefragment extends fragment implements 31 onitemlongclicklistener, onitemclicklistener, onrefreshlistener, 32 onscrolllistener { 33 private static final string tag = bloghomefragment.class.getsimplename(); 34 35 private swiperefreshlayout swiperefreshlayout; 36 private listview listview; 37 38 private bloglistadapter adapter; 39 40 private string path; 41 private int pagesize; 42 private int pageindex = 1; 43 private list bloglist; 44 45 private int lastvisibleindex; 46 private int maxvisibleindex = 400; 47 48 private intent intent; 49 50 public bloghomefragment() { 51 super(); 52 } 53 54 @override 55 public void oncreate(bundle savedinstancestate) { 56 super.oncreate(savedinstancestate); 57 log.i(tag, "oncreate"); 58 } 59 60 @override 61 public view oncreateview(layoutinflater inflater, viewgroup container, 62 bundle savedinstancestate) { 63 log.i(tag, "oncreateview"); 64 view rootview = inflater.inflate(r.layout.fragment_blog_home, 65 container, false); 66 return rootview; 67 } 68 69 @override 70 public void onviewcreated(view view, bundle savedinstancestate) { 71 super.onviewcreated(view, savedinstancestate); 72 log.i(tag, "onviewcreated"); 73 initcomponent(); 74 initdata(); 75 } 76 77 @override 78 public void onitemclick(adapterview parent, view view, int position, 79 long id) { 80 log.i(tag, "onitemclick -- " position); 81 showblogitem(bloglist.get(position)); 82 } 83 84 @override 85 public boolean onitemlongclick(adapterview parent, view view, 86 int position, long id) { 87 log.i(tag, "onitemlongclick -- " position); 88 return false; 89 } 90 91 @override 92 public void onrefresh() { 93 new bloglisttask(bloglist, swiperefreshlayout, adapter).execute(path, 94 "refresh"); 95 } 96 97 @override 98 public void onscrollstatechanged(abslistview view, int scrollstate) { 99 if (adapter.getcount() < maxvisibleindex) { 100 if (scrollstate == onscrolllistener.scroll_state_idle 101 && lastvisibleindex == adapter.getcount() - 1) { 102 103 pageindex ; 104 initpath(pageindex); 105 swiperefreshlayout.setrefreshing(true); 106 new bloglisttask(bloglist, swiperefreshlayout, adapter) 107 .execute(path, "loadmore"); 108 } 109 } else { 110 // toast.maketext(getactivity(), "最后一页!", 111 // toast.length_short).show(); 112 } 113 } 114 115 @override 116 public void onscroll(abslistview view, int firstvisibleitem, 117 int visibleitemcount, int totalitemcount) { 118 lastvisibleindex = firstvisibleitem visibleitemcount - 1; 119 } 120 121 private void initcomponent() { 122 swiperefreshlayout = (swiperefreshlayout) this.getactivity() 123 .findviewbyid(r.id.swiperefreshlayouthomeblog); 124 swiperefreshlayout.setonrefreshlistener(this); 125 swiperefreshlayout.setcolorschemeresources( 126 android.r.color.holo_blue_bright, 127 android.r.color.holo_green_light, 128 android.r.color.holo_orange_light, 129 android.r.color.holo_red_light); 130 131 listview = (listview) this.getactivity().findviewbyid( 132 r.id.listviewhomeblog); 133 listview.setonitemclicklistener(this); 134 listview.setonitemlongclicklistener(this); 135 listview.setonscrolllistener(this); 136 } 137 138 private void initdata() { 139 bloglist = new arraylist(); 140 adapter = new bloglistadapter(getactivity(), bloglist); 141 listview.setadapter(adapter); 142 143 initpath(1); 144 swiperefreshlayout.setrefreshing(true); 145 new bloglisttask(bloglist, swiperefreshlayout, adapter).execute(path, 146 "init"); 147 } 148 149 private void initpath(int pageindex) { 150 // http://wcf.open.cnblogs.com/blog/sitehome/paged/{pageindex}/{pagesize} 151 path = appmacros.recent_blogs_paged; 152 pagesize = appmacros.page_size; 153 path = path.replace("{pageindex}", "" pageindex); 154 path = path.replace("{pagesize}", "" pagesize); 155 156 log.i(tag, "pageindex:" pageindex); 157 } 158 159 private void showblogitem(blog blogentry) { 160 intent = new intent(this.getactivity(), blogactivity.class); 161 162 if (blogentry.getauthoravatar() != null) { 163 intent.putextra("avatar", blogentry.getauthoravatar().tostring()); 164 } else { 165 intent.putextra("avatar", ""); 166 } 167 intent.putextra("title", blogentry.getblogtitle()); 168 intent.putextra("author", blogentry.getauthorname()); 169 intent.putextra("published", 170 apputils.parsedatetostring(blogentry.getpublisheddatedate())); 171 intent.putextra("id", blogentry.getblogid()); 172 intent.putextra("link", blogentry.getblogtitle()); 173 174 startactivity(intent); 175 } 176 }

在之前的项目中,使用了handler thread来加载数据刷新控件,但是感觉代码比较混乱。在新的版本中使用了asynctask来替换。具体实现如下:

1 package com.arlen.cnblogs.task; 2 3 import java.util.arraylist; 4 import java.util.list; 5 6 import com.arlen.cnblogs.adapter.bloglistadapter; 7 import com.arlen.cnblogs.entity.blog; 8 import com.arlen.cnblogs.utils.apputils; 9 import com.arlen.cnblogs.utils.httputil; 10 11 import android.os.asynctask; 12 import android.support.v4.widget.swiperefreshlayout; 13 14 public class bloglisttask extends asynctask { 15 private list bloglist; 16 private list newlist = new arraylist(); 17 private swiperefreshlayout swiperefreshlayout; 18 private bloglistadapter adapter; 19 20 public bloglisttask(list bloglist, 21 swiperefreshlayout swiperefreshlayout, bloglistadapter adapter) { 22 super(); 23 this.bloglist = bloglist; 24 this.swiperefreshlayout = swiperefreshlayout; 25 this.adapter = adapter; 26 } 27 28 protected void doinbackground(string... params) { 29 30 newlist = httputil.getbloglist(params[0]); 31 32 if (params[1].equals("init")) { 33 bloglist.addall(newlist); 34 } else if (params[1].equals("refresh")) { 35 newlist.addall(bloglist); 36 apputils.removeduplicate(newlist); 37 bloglist.clear(); 38 bloglist.addall(newlist); 39 } else if (params[1].equals("loadmore")) { 40 bloglist.addall(newlist); 41 apputils.removeduplicate(bloglist); 42 } 43 return null; 44 } 45 46 @override 47 protected void onpreexecute() { 48 super.onpreexecute(); 49 newlist.clear(); 50 } 51 52 @override 53 protected void onpostexecute(void result) { 54 super.onpostexecute(result); 55 56 swiperefreshlayout.setrefreshing(false); 57 adapter.updatabloglist(bloglist); 58 adapter.notifydatasetchanged(); 59 } 60 61 }

刷新数据后可能会出现数据重复,要去除重复,首先要在类中重写equals()方法,应为要考虑到不同类型,使用了泛型方法来对user、blog、news和comment来进行去重。具体如下:

1 @override 2 public boolean equals(object object) { 3 if (object instanceof blog) { 4 blog blog = (blog) object; 5 return string.valueof(blog.getblogid()).equals( 6 string.valueof(this.getblogid())); 7 } else { 8 return super.equals(object); 9 } 10 } 1 public static void removeduplicate(list list) { 2 for (int i = 0; i < list.size(); i ) { 3 for (int j = i 1; j < list.size(); j ) { 4 if (list.get(i).equals(list.get(j))) { 5 list.remove(j); 6 } 7 } 8 } 9 }

项目地址:https://github.com/zhangtingkuo/androidcnblogs

转载于:https://www.cnblogs.com/zhangtingkuo/p/4052623.html

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是ag凯发k8国际为你收集整理的android 博客园客户端 (八) 下拉刷新、分页,asynctask的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得ag凯发k8国际网站内容还不错,欢迎将ag凯发k8国际推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图