1 importandroid.content.DialogInterface;2 importandroid.os.Handler;3 importandroid.os.Message;4 importandroid.support.v7.app.AlertDialog;5 importandroid.support.v7.app.AppCompatActivity;6 importandroid.os.Bundle;7 importandroid.view.View;8 importandroid.view.ViewGroup;9 importandroid.widget.AbsListView;10 importandroid.widget.AdapterView;11 importandroid.widget.BaseAdapter;12 importandroid.widget.ListView;13 importandroid.widget.TextView;14 importjava.util.ArrayList;15 importjava.util.List;16 /**
17 * Created by panchengjia on 2016/11/29.18 */
19 public class MainActivity extends AppCompatActivity implementsAbsListView.OnScrollListener{20 privateListView lv;21 private List news;//声明存储新闻标题与内容的List
22 private int total=1;//计数器(设置默认从1开始)用于集合内数据初始化
23 MyAdapter adapter;24 @Override25 protected voidonCreate(Bundle savedInstanceState) {26 super.onCreate(savedInstanceState);27 setContentView(R.layout.activity_main);28 lv=(ListView) findViewById(R.id.lv);29 //为当前ListView设置OnScrollListener实现分页刷新
30 lv.setOnScrollListener(this);31 //将login_item(下拉刷新效果的item)通过布局 填充器声明
32 View v = getLayoutInflater().inflate(R.layout.login_item,null);33 //将login_item设置到ListView页脚
34 lv.addFooterView(v);35 //实例化存储内容资源的List
36 news = new ArrayList<>();37 //调用初始化List的方法
38 initList();39 adapter = newMyAdapter();40 //设置单击item的事件
41 lv.setOnItemClickListener(newAdapterView.OnItemClickListener() {42 @Override43 public void onItemClick(AdapterView> parent, View view, int position, longid) {44 show(view);//事件处理为调用show方法(显示AlertDialog对话框)
45 }46 });47 lv.setAdapter(adapter);48 }49 //AlertDialog对话框的调用这里就不多说了,前期有专门的博文解释
50 public voidshow(View v){51 AlertDialog.Builder builder = new AlertDialog.Builder(this);52 TextView title =(TextView) v.findViewById(R.id.textView);53 TextView message =(TextView) v.findViewById(R.id.textView2);54 builder.setTitle(title.getText().toString());55 builder.setMessage(message.getText().toString());56 builder.setPositiveButton("已经浏览完毕", newDialogInterface.OnClickListener() {57 @Override58 public void onClick(DialogInterface dialog, intwhich) {59
60 }61 });62 builder.show();63 }64 //初始化List内的元素,模拟每次可刷新10条信息
65 private voidinitList() {66 for(int i=1;i<=10;i++){67 News n = newNews();68 //加total是因为total在刷新页面后不会继续从一开始
69 n.title = "Title--"+total;70 n.message="Message"+total;71 news.add(n);72 total++;73 }74 }75 //int currenVisibleItemCount;//声明截止当前页面看到的item总数(演示用)
76 boolean isLastRow=false;//判断是否到ListView的最后一个item
77 @Override78 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, inttotalItemCount) {79 //firstVisibleItem位可见页面的第一条在Arraylist中的下标,visibleItemCount为当前页面item数80 //currenVisibleItemCount = firstVisibleItem+visibleItemCount-1=totalItemCount;(演示用)
81 if(firstVisibleItem+visibleItemCount==totalItemCount&&totalItemCount>0){82 isLastRow=true;//判断已经到最后一个item(即为footerView)
83 }84 }85 @Override86 public void onScrollStateChanged(AbsListView view, intscrollState) {87 /*判断是否刷新页面之前,解释一下scrollState的三种状态88 * 1.scrollState = SCROLL_STATE_TOUCH_SCROLL为手指按住屏幕滚动(未脱离屏幕);89 * 2.scrollState = SCROLL_STATE_FLING可以理解为手指离开屏幕前,用力滑了一下,90 * 手指离开后,页面已然保持滚动;91 * 3.scrollState = SCROLL_STATE_IDLE手指未接触屏幕,且屏幕页面保持静止92 * 开启刷新页面的线程前,确保ListView已经到最后一行(Item)并且屏幕页面保持静止93 **/
94 if(isLastRow&&scrollState==SCROLL_STATE_IDLE){95 new Thread(newMyThread()).start();96 }97 }98 //创建分页刷新线程(模拟刷新)
99 class MyThread implementsRunnable{100
101 @Override102 public voidrun() {103 try{104 Thread.sleep(500);//设置线程休眠时间为500毫秒刷新一次
105 } catch(InterruptedException e) {106 e.printStackTrace();107 }108 initList();//重新初始化List109 //线程内调用Handler执行页面刷新(后面会写文对handler进行详细剖析)
110 handler.sendEmptyMessage(1);111 }112 }113 Handler handler = newHandler(){114 @Override115 public voidhandleMessage(Message msg) {116 super.handleMessage(msg);117 switch(msg.what){118 case 1:119 //强制调用适配器的getView来刷新每个Item的内容。
120 adapter.notifyDataSetChanged();121 break;122 }123 }124 };125 //自定义适配器
126 class MyAdapter extendsBaseAdapter{127 @Override128 public intgetCount() {129 returnnews.size();130 }131 @Override132 public Object getItem(intposition) {133 returnnews.get(position);134 }135 @Override136 public long getItemId(intposition) {137 returnposition;138 }139 @Override140 public View getView(intposition, View convertView, ViewGroup parent) {141 ViewHolder vh;142 if(convertView==null){143 convertView = getLayoutInflater().inflate(R.layout.item,null);144 vh=newViewHolder();145 vh.message =(TextView) convertView.findViewById(R.id.textView2);146 vh.title=(TextView) convertView.findViewById(R.id.textView);147 convertView.setTag(vh);148 }149 vh=(ViewHolder) convertView.getTag();150 vh.title.setText(news.get(position).title);151 vh.message.setText(news.get(position).message);152 returnconvertView;153 }154 classViewHolder{155 TextView title;156 TextView message;157 }158 }159 }