Android實現(xiàn)滑動加載數(shù)據(jù)的方法
本文實例講述了Android實現(xiàn)滑動加載數(shù)據(jù)的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
EndLessActivity.java如下:
package com.ScrollListView; import Android.app.ListActivity; import Android.os.Bundle; import Android.view.Gravity; import Android.view.View; import Android.view.ViewGroup; import Android.widget.AbsListView; import Android.widget.BaseAdapter; import Android.widget.LinearLayout; import Android.widget.ProgressBar; import Android.widget.TextView; import Android.widget.AbsListView.OnScrollListener; public class EndLessActivity extends ListActivity implements OnScrollListener { Aleph0 adapter = new Aleph0(); int mProgressStatus = 0 ; ProgressBar progressBar; protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); LinearLayout searchLayout = new LinearLayout( this ); searchLayout.setOrientation(LinearLayout.HORIZONTAL); progressBar = new ProgressBar( this ); progressBar.setPadding( 0 , 0 , 15 , 0 ); searchLayout.addView(progressBar, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT )); TextView textView = new TextView( this ); textView.setText( " 加載中... " ); textView.setGravity(Gravity.CENTER_VERTICAL); searchLayout.addView(textView, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT )); searchLayout.setGravity(Gravity.CENTER); LinearLayout loadingLayout = new LinearLayout( this ); loadingLayout.addView(searchLayout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT )); loadingLayout.setGravity(Gravity.CENTER); getListView().addFooterView(loadingLayout); setListAdapter(adapter); getListView().setOnScrollListener( this ); } public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) { boolean loadMore = /* maybe add a padding */ firstVisible + visibleCount >= totalCount; if (loadMore) { adapter.count += visibleCount; // or any other amount adapter.notifyDataSetChanged(); } } public void onScrollStateChanged(AbsListView v, int s) { } class Aleph0 extends BaseAdapter { int count = 40 ; /* starting amount */ public int getCount() { return count; } public Object getItem( int pos) { return pos; } public long getItemId( int pos) { return pos; } public View getView( int pos, View v, ViewGroup p) { TextView view = new TextView(EndLessActivity. this ); view.setText( " entry " + pos); return view; } } }
listview下部是按鈕控制:
package com.ScrollListView; import Android.app.ListActivity; import Android.os.Bundle; import Android.view.Gravity; import Android.view.View; import Android.view.ViewGroup; import Android.widget.AbsListView; import Android.widget.BaseAdapter; import Android.widget.Button; import Android.widget.LinearLayout; import Android.widget.ProgressBar; import Android.widget.TextView; import Android.widget.AbsListView.OnScrollListener; public class EndLessActivity extends ListActivity { Aleph0 adapter = new Aleph0(); int mProgressStatus = 0 ; protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); LinearLayout searchLayout = new LinearLayout( this ); searchLayout.setOrientation(LinearLayout.HORIZONTAL); Button textView = new Button( this ); textView.setText( " 加載中... " ); textView.setGravity(Gravity.CENTER_VERTICAL); searchLayout.addView(textView, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT )); searchLayout.setGravity(Gravity.CENTER); LinearLayout loadingLayout = new LinearLayout( this ); loadingLayout.addView(searchLayout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT )); loadingLayout.setGravity(Gravity.CENTER); getListView().addFooterView(loadingLayout); textView.setOnClickListener( new Button.OnClickListener() { @Override public void onClick(View v) { adapter.count += 10 ; adapter.notifyDataSetChanged(); } }); setListAdapter(adapter); // getListView().setOnScrollListener(this); } /* public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) { boolean loadMore = firstVisible + visibleCount >= totalCount; if(loadMore) { adapter.count += visibleCount; adapter.notifyDataSetChanged(); } } */ public void onScrollStateChanged(AbsListView v, int s) { } class Aleph0 extends BaseAdapter { int count = 40 ; /* starting amount */ public int getCount() { return count; } public Object getItem( int pos) { return pos; } public long getItemId( int pos) { return pos; } public View getView( int pos, View v, ViewGroup p) { TextView view = new TextView(EndLessActivity. this ); view.setText( " entry " + pos); return view; } } }
希望本文所述對大家的Android程序設(shè)計有所幫助。
- Android實現(xiàn)ListView分頁自動加載數(shù)據(jù)的方法
- Android中ListView如何分頁加載數(shù)據(jù)
- Android實現(xiàn)ListView數(shù)據(jù)動態(tài)加載的方法
- Android中如何加載數(shù)據(jù)緩存
- 解決Android ListView數(shù)據(jù)為空及加載錯誤的方法
- Android應(yīng)用中ListView利用OnScrollListener分頁加載數(shù)據(jù)
- Android異步加載數(shù)據(jù)和圖片的保存思路詳解
- Android自定義加載控件實現(xiàn)數(shù)據(jù)加載動畫
相關(guān)文章
Android手機通過rtp發(fā)送aac數(shù)據(jù)給vlc播放的實現(xiàn)步驟
這篇文章主要介紹了Android手機通過rtp發(fā)送aac數(shù)據(jù)給vlc播放的實現(xiàn)步驟,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下2021-04-04Android UI設(shè)計與開發(fā)之仿人人網(wǎng)V5.9.2最新版引導界面
這篇文章主要為大家詳細介紹了Android UI設(shè)計與開發(fā)之仿人人網(wǎng)V5.9.2最新版引導界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android通過HTTP協(xié)議實現(xiàn)斷點續(xù)傳下載實例
本篇文章主要介紹了Android通過HTTP協(xié)議實現(xiàn)斷點續(xù)傳下載實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04Android 調(diào)用系統(tǒng)應(yīng)用的方法總結(jié)
這篇文章主要介紹了Android 調(diào)用系統(tǒng)應(yīng)用的方法總結(jié)的相關(guān)資料,這里提供調(diào)用錄像,錄音,拍照等功能,需要的朋友可以參考下2017-08-08Flutter滾動組件之SingleChildScrollView使用詳解
這篇文章主要為大家詳細介紹了Flutter滾動組件之SingleChildScrollView使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03Kotlin中標準函數(shù)run、with、let、also與apply的使用和區(qū)別詳解
相比Java, Kotlin提供了不少高級語法特性。對于一個Kotlin的初學者來說經(jīng)常會寫出一些不夠優(yōu)雅的代碼,下面這篇文章主要給大家介紹了關(guān)于Kotlin中標準函數(shù)run、with、let、also與apply的使用和區(qū)別的相關(guān)資料,需要的朋友可以參考下。2018-03-03android12?SD如何動態(tài)申請讀寫權(quán)限
這篇文章主要給大家介紹了關(guān)于android12?SD如何動態(tài)申請讀寫權(quán)限的相關(guān)資料,從Android?6.0開始,權(quán)限不再是在manifest?件中粘貼?下即可,這時候權(quán)限也正式?進?家的視野,需要的朋友可以參考下2023-07-07