android的ListView的分页加载
main6.xml
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ListViewandroid:id="@+id/listView6_1_1"android:layout_width="match_parent"android:layout_height="fill_parent"></ListView></LinearLayout>
main6_item.xml
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/textView_title_1_1"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="28sp"android:text="title"android:textColor="#55ff88"/><TextViewandroid:id="@+id/textView_content_2_2"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="20sp"android:text="duanluosdfsdf"/></LinearLayout>
MainActivity6
packagecom.example.listview;importjava.util.ArrayList;importjava.util.List;importjava.util.Vector;importandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Handler;importandroid.os.Message;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.AbsListView;importandroid.widget.AbsListView.OnScrollListener;importandroid.widget.BaseAdapter;importandroid.widget.ListView;importandroid.widget.TextView;publicclassMainActivity6extendsActivityimplementsOnScrollListener{privateListViewlistView;privateList<News>news=newVector<News>();privateMyAdaptermyDapter;//线程之间通信的桥梁privateHandlerhandler=newHandler(){@OverridepublicvoidhandleMessage(Messagemsg){//TODOAuto-generatedmethodstubswitch(msg.what){case1://通知适配器,更新数据myDapter.notifyDataSetChanged();break;default:break;}}};@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.main6);listView=(ListView)findViewById(R.id.listView6_1_1);//设置底部视图ViewfootView=getLayoutInflater().inflate(R.layout.load,null);listView.addFooterView(footView);listView.setOnScrollListener(this);//initDate();newLoadDateThread().start();//加载数据的线程myDapter=newMyAdapter();listView.setAdapter(myDapter);}/***初始化数据**/intindex;//计数器voidinitDate(){for(inti=0;i<10;i++){Newsnewss=newNews();newss.title="title-"+index;newss.content="content-"+index;index++;news.add(newss);}}/***自定义适配器**/classMyAdapterextendsBaseAdapter{@OverridepublicintgetCount(){//TODOAuto-generatedmethodstubreturnnews.size();}@OverridepublicObjectgetItem(intposition){//TODOAuto-generatedmethodstubreturnnews.get(position);}@OverridepubliclonggetItemId(intposition){//TODOAuto-generatedmethodstubreturnposition;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){//TODOAuto-generatedmethodstubViewholerviewholer;if(convertView==null){convertView=getLayoutInflater().inflate(R.layout.main6_item,null);viewholer=newViewholer();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中取出数据并填充Newsnewss=news.get(position);viewholer.textView.setText(newss.title);viewholer.textContent.setText(newss.content);returnconvertView;}}staticclassViewholer{TextViewtextView;TextViewtextContent;}//intscrollState=0;intvisiableLastIndex=0;//滚动条状态发生变化事件方法@OverridepublicvoidonScrollStateChanged(AbsListViewview,intscrollState){//TODOAuto-generatedmethodstub/*this.scrollState=scrollState;System.out.println("scrollState:"+scrollState);*///滚动条在最下面并且滚动条是停止状态if(myDapter.getCount()==visiableLastIndex&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){//启动线程加载数据newLoadDateThread().start();}}//滚动事件方法@OverridepublicvoidonScroll(AbsListViewview,intfirstVisibleItem,intvisibleItemCount,inttotalItemCount){//TODOAuto-generatedmethodstubSystem.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组件安全)classLoadDateThreadextendsThread{@Overridepublicvoidrun(){//TODOAuto-generatedmethodstubsuper.run();initDate();try{Thread.sleep(2000);//通知主线程,数据已加载完毕handler.sendEmptyMessage(1);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}
News
packagecom.example.listview;/***新闻对象**/publicclassNews{Stringtitle;Stringcontent;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。