1.ListView的footer 布局 item_demo6_list_head.xml
2.activity_demo布局(主布局)
Demo6Activity(主界面代码)
public class Demo6Activity extends AppCompatActivity {private ListView listView;private List data;private ArrayAdapter adapter;//分页加载的数据的数量private int pageSize=10;private int currentPage = 0;private final int pageType=1;//查看更多private TextView moreTextView;//正在加载进度条private LinearLayout loadProgressBar;private ListView listview ;//
//用来判断是否加载完成private boolean loadfinish=true;private View footerView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_demo6);footerView =LayoutInflater.from(this).inflate(R.layout.item_demo6_list_head, null);footerView.findViewById(R.id.footer_layout).setVisibility(View.GONE);data = new ArrayList();addMore();listView=(ListView) findViewById(R.id.demo6_lv_id);//添加"加载更多"一定要在设置Adapter之前adapter= new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);listView.addFooterView(footerView);listView.setAdapter(adapter);//当用户滑动listview到最后一项是,动态的加载第二页的数据listView.setOnScrollListener(new AbsListView.OnScrollListener(){public void onScrollStateChanged(AbsListView view, int scrollState){// TODO Auto-generated method stub}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, final int totalItemCount){//得到listview最后一项的idint lastItemId=listView.getLastVisiblePosition();//判断用户是否滑动到最后一项,因为索引值从零开始所以要加上1if((lastItemId+1)==totalItemCount){footerView.findViewById(R.id.footer_layout).setVisibility(View.VISIBLE);if(totalItemCount>0){//判断当前页是否超过最大页,以及上一页的数据是否加载完成if( loadfinish ){loadfinish=false;new Thread(new Runnable(){public void run(){try {Thread.sleep(4000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}addMore();//通知listview改变UI中的数据handler.sendEmptyMessage(0);}}).start();}}}}});}private void addMore(){for (int i=0;i}private Handler handler=new Handler(){@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case 0://改变适配器的数目
// adapter.count += pageSize;//通知适配器,发现改变操作adapter.notifyDataSetChanged();loadfinish=true;footerView.findViewById(R.id.footer_layout).setVisibility(View.GONE);break;default:break;}super.handleMessage(msg);}};}