Listview中使用线程实现无限加载更多项目的功能
相關(guān)搜索:"Listview 加載更多",
在現(xiàn)在的SINA微博或者象twitter,dzone等網(wǎng)站中,當(dāng)加載一個(gè)很長(zhǎng)的列表時(shí),往往都是
先加載部分內(nèi)容,然后當(dāng)用戶用拖拉條往下拖動(dòng)時(shí),再加載更多的內(nèi)容.這里在android
中,可以用listview搭配線程實(shí)現(xiàn)同樣的功能,舉例如下: 人人
1)首先設(shè)置footer部分,即在listview的下面設(shè)置footview,
footview的模版如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:orientation="horizontal"
??? android:layout_width="fill_parent"
??? android:gravity="center_horizontal"
??? android:padding="3dp"
??? android:layout_height="fill_parent">
??? <TextView
??????? android:id="@id/android:empty"
??????? android:layout_width="wrap_content"
??????? android:layout_height="fill_parent"
??????? android:gravity="center"
??????? android:padding="5dp"
??????? android:text="Loading more days..."/>
</LinearLayout>
然后記得在加到adapter前,把footview加到listview中去
? View footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
this.getListView().addFooterView(footerView);
this.setListAdapter(adapter);
然后,listview的onscroll方法如下:
this.getListView().setOnScrollListener(new OnScrollListener(){
??? ??? ???
??? ??? ??? ???
??? ??? ??? public void onScrollStateChanged(AbsListView view, int scrollState) {}
??? ??? ???
??? ??? ??? ???
??? ??? ??? public void onScroll(AbsListView view, int firstVisibleItem,
??? ??? ??? ??? ??? int visibleItemCount, int totalItemCount) {
??? ??? ??? ???
??? ??? ??? ???
??? ??? ??? ??? ??? ??? ??? ??? int lastInScreen = firstVisibleItem + visibleItemCount;??? ??? ??? ???
??? ??? ??? ??? String last=Integer.toString(lastInScreen);
??? ??? ??? ??? Log.d("show",last);
??? ??? ??? ??? String total=Integer.toString(totalItemCount);
??? ??? ??? ??? Log.d("total",total);
??? ??? ??? ??? ??? ??? ???
if((lastInScreen == totalItemCount) && !(loadingMore)){??? ??? ??? ??? ???
??? ??? ??? ??? ??? Thread thread =? new Thread(null, loadMoreListItems);
??? ??? ??? ??????? thread.start();
??? ??? ??? ??? }
??? ??? ??? }
??? ??? });
??? ???
??? ???
??? ??? ???
Thread thread =? new Thread(null, loadMoreListItems);
??????? thread.start();
下面來(lái)分析下,其中,可以預(yù)先設(shè)置一個(gè)屏的listview能顯示多少條記錄,比如設(shè)置為5條,
這里itemsPerPage=15,然后首先
int lastInScreen = firstVisibleItem + visibleItemCount;
? 這里,獲得每屏中最后一條數(shù)據(jù)的位置,比如一次先顯示5條,則lastInScreen=5了,
if((lastInScreen == totalItemCount) && !(loadingMore)){
Thread thread =? new Thread(null, loadMoreListItems);
??????? thread.start();
}
? 這里是判斷,如果是滾動(dòng)了滾動(dòng)條,并且用戶以及功能瀏覽完了一次屏幕所需要的數(shù)目(比如5個(gè)),則啟用線程序loadMoreListItems去處理,加載另外的新的5個(gè)了.
而如果沒(méi)滾動(dòng),則第一次顯示時(shí),也要啟用線程序loadMoreListItems去處理.
.
? 下面看loadMoreListItems.
? private Runnable loadMoreListItems = new Runnable() {??? ??? ???
???
??? ??? public void run() {
??? ??? ??? ??? ??? ??? loadingMore = true;
??? ??? ???
??? ??? ??? ??? ??? ??? myListItems = new ArrayList<String>();
??? ??? ???
??? ??? ??? //Simulate a delay, delete this on a production environment!
??? ??? ??? try { Thread.sleep(1000);
??? ??? ??? } catch (InterruptedException e) {}
??? ??? ???
??? ??? ??? //Get 15 new listitems
??? ??? ??? for (int i = 0; i < itemsPerPage; i++) {??? ???
??? ??? ??? ???
???? myListItems.add("Date: " + (d.get(Calendar.MONTH)+ 1) + "/" + d.get(Calendar.DATE) + "/" + d.get(Calendar.YEAR) );????????? ??? ??? ??? ??? ??? ??????? ??? d.add(Calendar.DATE, 1);
??? ??? ??? ???
??? ??? ??? }
??? ??? ???
??? ??? ??? ??? ??????? runOnUiThread(returnRes);
??? ???????
??? ??? }
??? };???
在這個(gè)線程里,實(shí)際上就是用循環(huán)往listview中加日期,產(chǎn)生從當(dāng)天時(shí)間起的若干個(gè)日期格式,然后調(diào)用 runOnUiThread(returnRes);去更新UI主線程,注意 runOnUiThread是
android提供的方法,可以幫助你在線程中執(zhí)行UI更新操作.
在更新主線程中,十分簡(jiǎn)單,只不過(guò)往adapter中去增加元素,并且notifyDataSetChanged通知listview起變化了,并設(shè)置loadingMore=false,因?yàn)橐呀?jīng)加載完了一次了.風(fēng)之境地 java-javascript 蘑菇街女裝
轉(zhuǎn)載于:https://www.cnblogs.com/sky7034/archive/2011/04/25/2026789.html
總結(jié)
以上是生活随笔為你收集整理的Listview中使用线程实现无限加载更多项目的功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 安卓下的免费离线地图导航软件(老虎地图)
- 下一篇: Hadoop学习总结:Map-Reduc