首页 > 代码库 > 1、ListView自定义控件下拉刷新(一)
1、ListView自定义控件下拉刷新(一)
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:background="#f5f5f5" > 5 6 <zrc.widget.ZrcListView 7 android:id="@+id/zListView" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" >10 </zrc.widget.ZrcListView>11 12 <RelativeLayout13 android:layout_width="match_parent"14 android:layout_height="50dp"15 android:layout_alignParentTop="true"16 android:background="#ccffffff" >17 18 <TextView19 android:layout_width="wrap_content"20 android:layout_height="wrap_content"21 android:layout_centerInParent="true"22 android:text="常见国家"23 android:textSize="20sp" />24 25 <View26 android:layout_width="match_parent"27 android:layout_height="1px"28 android:layout_alignParentBottom="true"29 android:layout_centerHorizontal="true"30 android:background="#cccccc" />31 32 </RelativeLayout>33 34 </RelativeLayout>
1 import java.util.ArrayList; 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.os.Handler; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 import android.widget.TextView; 11 import zrc.widget.SimpleFooter; 12 import zrc.widget.SimpleHeader; 13 import zrc.widget.ZrcListView; 14 import zrc.widget.ZrcListView.OnStartListener; 15 16 public class MainActivity extends Activity { 17 private ZrcListView listView; 18 private Handler handler; 19 private ArrayList<String> msgs; 20 private int pageId = -1; 21 private MyAdapter adapter; 22 23 private static final String[][] names = new String[][]{ 24 {"加拿大","瑞典","澳大利亚","瑞士","新西兰","挪威","丹麦","芬兰","奥地利","荷兰","德国","日本","比利时","意大利","英国"}, 25 {"德国","西班牙","爱尔兰","法国","葡萄牙","新加坡","希腊","巴西","美国","阿根廷","波兰","印度","秘鲁","阿联酋","泰国"}, 26 {"智利","波多黎各","南非","韩国","墨西哥","土耳其","埃及","委内瑞拉","玻利维亚","乌克兰"}, 27 {"以色列","海地","中国","沙特阿拉伯","俄罗斯","哥伦比亚","尼日利亚","巴基斯坦","伊朗","伊拉克"} 28 }; 29 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(R.layout.activity_main); 34 35 listView = (ZrcListView) findViewById(R.id.zListView); 36 handler = new Handler(); 37 38 // 设置默认偏移量,主要用于实现透明标题栏功能。(可选) 39 float density = getResources().getDisplayMetrics().density; 40 listView.setFirstTopOffset((int) (50 * density)); 41 42 // 设置下拉刷新的样式(可选,但如果没有Header则无法下拉刷新) 43 SimpleHeader header = new SimpleHeader(this); 44 header.setTextColor(0xff0066aa); 45 header.setCircleColor(0xff33bbee); 46 listView.setHeadable(header); 47 48 // 设置加载更多的样式(可选) 49 SimpleFooter footer = new SimpleFooter(this); 50 footer.setCircleColor(0xff33bbee); 51 listView.setFootable(footer); 52 53 // 设置列表项出现动画(可选) 54 listView.setItemAnimForTopIn(R.anim.topitem_in); 55 listView.setItemAnimForBottomIn(R.anim.bottomitem_in); 56 57 // 下拉刷新事件回调(可选) 58 listView.setOnRefreshStartListener(new OnStartListener() { 59 @Override 60 public void onStart() { 61 refresh(); 62 } 63 }); 64 65 // 加载更多事件回调(可选) 66 listView.setOnLoadMoreStartListener(new OnStartListener() { 67 @Override 68 public void onStart() { 69 loadMore(); 70 } 71 }); 72 73 adapter = new MyAdapter(); 74 listView.setAdapter(adapter); 75 listView.refresh(); // 主动下拉刷新 76 } 77 78 private void refresh(){ 79 handler.postDelayed(new Runnable() { 80 @Override 81 public void run() { 82 // 随机数模拟成功失败。这里从有数据开始。 83 int rand = (int) (Math.random() * 2); 84 if(rand == 0 || pageId == -1){ 85 pageId = 0; 86 msgs = new ArrayList<String>(); 87 for(String name:names[0]){ 88 msgs.add(name); 89 } 90 adapter.notifyDataSetChanged(); 91 listView.setRefreshSuccess("加载成功"); // 通知加载成功 92 listView.startLoadMore(); // 开启LoadingMore功能 93 }else{ 94 listView.setRefreshFail("加载失败"); 95 } 96 } 97 }, 2 * 1000); 98 } 99 100 private void loadMore(){101 handler.postDelayed(new Runnable() {102 @Override103 public void run() {104 pageId++;105 if(pageId<names.length){106 for(String name:names[pageId]){107 msgs.add(name);108 }109 adapter.notifyDataSetChanged();110 listView.setLoadMoreSuccess();111 }else{112 listView.stopLoadMore();113 }114 }115 }, 2 * 1000);116 }117 118 private class MyAdapter extends BaseAdapter{119 @Override120 public int getCount() {121 return msgs==null ? 0 : msgs.size();122 }123 @Override124 public Object getItem(int position) {125 return msgs.get(position);126 }127 128 @Override129 public long getItemId(int position) {130 return position;131 }132 133 @Override134 public View getView(int position, View convertView, ViewGroup parent) {135 TextView textView;136 if(convertView==null) {137 textView = (TextView) getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null);138 }else{139 textView = (TextView) convertView;140 }141 textView.setText(msgs.get(position));142 return textView;143 }144 }145 }
开源ListView自定义下载:http://download.csdn.net/detail/androidsj/7948481
1、ListView自定义控件下拉刷新(一)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。