android的ListView的分页加载
main6.xml
桥东ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
main6_item.xml
MainActivity6
package com.example.listview; import java.util.ArrayList; import java.util.List; import java.util.Vector; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity6 extends Activity implements OnScrollListener{ private ListView listView; private Listnews=new Vector (); private MyAdapter myDapter; //线程之间通信的桥梁 private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case 1: //通知适配器,更新数据 myDapter.notifyDataSetChanged(); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main6); listView=(ListView) findViewById(R.id.listView6_1_1); //设置底部视图 View footView=getLayoutInflater().inflate(R.layout.load, null); listView.addFooterView(footView); listView.setOnScrollListener(this); //initDate(); new LoadDateThread().start(); //加载数据的线程 myDapter=new MyAdapter(); listView.setAdapter(myDapter); } /** * 初始化数据 * */ int index; //计数器 void initDate(){ for(int i=0;i<10;i++){ News newss=new News(); newss.title="title-"+index; newss.content="content-"+index; index++; news.add(newss); } } /** * 自定义适配器 * */ class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return news.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return news.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Viewholer viewholer; if(convertView==null){ convertView=getLayoutInflater().inflate(R.layout.main6_item, null); viewholer=new Viewholer(); viewholer.textView=(TextView) convertView.findViewById(R.id.textView_title_1_1); viewholer.textContent=(TextView) convertView.findViewById(R.id.textView_content_2_2); convertView.setTag(viewholer); }else{ viewholer=(Viewholer) convertView.getTag(); } //从veclor中取出数据并填充 News newss=news.get(position); viewholer.textView.setText(newss.title); viewholer.textContent.setText(newss.content); return convertView; } } static class Viewholer{ TextView textView; TextView textContent; } // int scrollState=0; int visiableLastIndex=0; //滚动条状态发生变化事件方法 @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub /* this.scrollState=scrollState; System.out.println("scrollState:"+scrollState);*/ //滚动条在最下面并且滚动条是停止状态 if(myDapter.getCount()==visiableLastIndex&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){ //启动线程加载数据 new LoadDateThread().start(); } } //滚动事件方法 @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub System.out.println("view:"+view); System.out.println("firstVisibleItem:"+firstVisibleItem); System.out.println("visibleItemCount:"+visibleItemCount); System.out.println("totalItemCount"+totalItemCount); visiableLastIndex=firstVisibleItem+visibleItemCount-1; } //模拟加载数据 //子线程不允许访问主线程的UI组件(保证主线程UI组件安全) class LoadDateThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub super.run(); initDate(); try { Thread.sleep(2000); //通知主线程,数据已加载完毕 handler.sendEmptyMessage(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
News
package com.example.listview; /** * 新闻对象 * */ public class News { String title; String content; }
分享文章:android的ListView的分页加载
链接分享:http://ybzwz.com/article/gjoppj.html