android--------自定义控件ListView实现下拉刷新和上拉加载
开发项目过程中基本都会用到listView的下拉刷新和上滑加载更多,为了方便重写的ListView来实现下拉刷新,同时添加了上拉自动加载更多的功能。
Android下拉刷新可以分为两种情况:
1.获取更多的数据,按服务器数据库时间顺序存储入情况,此刻我们是获取是显示在我们应用中的数据更早前的数据,这也是最常见的情况,比如(微博获取更多信息,就是获取更多更早前的信息,然后动态的添加到已有的数据的下方);
2.获取更多的最新的数据,其实还是一种获取更多的操作方式。
1 public class MainActivity extends Activity implements IXListViewListener { 2 3 private XListView listView; 4 private int in = 6; 5 private Adapter adapter; 6 private Handler mHandler; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 // TODO Auto-generated method stub 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 initView(); 14 15 } 16 17 private void initView() { 18 // TODO Auto-generated method stub 19 listView = (XListView) findViewById(R.id.list); 20 listView.setPullLoadEnable(true); 21 adapter = new Adapter(MainActivity.this); 22 listView.setAdapter(adapter); 23 listView.setXListViewListener(this); 24 mHandler = new Handler(); 25 26 } 27 28 private void onLoad() { 29 listView.stopRefresh(); 30 listView.stopLoadMore(); 31 listView.setRefreshTime("刚刚"); 32 } 33 34 @Override 35 public void onRefresh() { 36 mHandler.postDelayed(new Runnable() { 37 @Override 38 public void run() { 39 onLoad(); 40 } 41 }, 2000); 42 } 43 44 @Override 45 public void onLoadMore() { 46 mHandler.postDelayed(new Runnable() { 47 @Override 48 public void run() { 49 in += 4; 50 onLoad(); 51 } 52 }, 2000); 53 } 54 55 private static String[] string = new String[] { "民政局于,请带好相关证件文书 ", 56 "财政部发布公告,由于资金调整信息未完善,下周在发放工资,请各位见谅", 57 "信息部门于2016-11-11日在某某地方开会,特此通知,请带好相关证件", 58 "党政办发放通知,有关贫困地区扶贫政策,希望每个部门做出相应的方案,于2016-11-11开会讨论" }; 59 60 public class Adapter extends BaseAdapter { 61 62 private Context context; 63 private LayoutInflater inflater; 64 65 public Adapter(Context context) { 66 // TODO Auto-generated constructor stub 67 this.context = context; 68 inflater = LayoutInflater.from(context); 69 } 70 71 @Override 72 public int getCount() { 73 // TODO Auto-generated method stub 74 return in; 75 } 76 77 @Override 78 public Object getItem(int position) { 79 // TODO Auto-generated method stub 80 return position; 81 } 82 83 @Override 84 public long getItemId(int position) { 85 // TODO Auto-generated method stub 86 return position; 87 } 88 89 @Override 90 public View getView(int position, View convertView, ViewGroup parent) { 91 // TODO Auto-generated method stub 92 ViewHolder holder = null; 93 final int index = position; 94 if (convertView == null) { 95 convertView = inflater.inflate(R.layout.item_news_mass, null); 96 holder = new ViewHolder(convertView); 97 convertView.setTag(holder); 98 } else { 99 holder = (ViewHolder) convertView.getTag(); 100 } 101 holder.mName.setText(string[(position % 4)]); 102 103 return convertView; 104 } 105 106 class ViewHolder { 107 private ImageView mImageView; 108 private TextView mName; 109 110 public ViewHolder(View view) { 111 mName = (TextView) view.findViewById(R.id.item_news_msg); 112 113 } 114 } 115 116 } 117 } 118
由于代码太多,完整代码未给出,源码直接下载即可
源码下载: https://github.com/DickyQie/android-list-control