怎么在Android中利用Listview動(dòng)態(tài)加載數(shù)據(jù)?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
冠縣網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站開(kāi)發(fā)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)于2013年成立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
Java代碼:
package org.developerworks.android; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; 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.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import android.widget.AbsListView.OnScrollListener; import android.widget.LinearLayout.LayoutParams; public class ListViewForLoading extends Activity implements OnScrollListener { private listViewAdapter adapter = new listViewAdapter(); ListView listView; LinearLayout loadingLayout; private Thread mThread; /** * 設(shè)置布局顯示屬性 */ private LayoutParams mLayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); /** * 設(shè)置布局顯示目標(biāo)最大化屬性 */ private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); private ProgressBar progressBar; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { // TODO Auto-generated method stub // 線性布局 LinearLayout layout = new LinearLayout(this); // 設(shè)置布局 水平方向 layout.setOrientation(LinearLayout.HORIZONTAL); // 進(jìn)度條 progressBar = new ProgressBar(this); // 進(jìn)度條顯示位置 progressBar.setPadding(0, 0, 15, 0); // 把進(jìn)度條加入到layout中 layout.addView(progressBar, mLayoutParams); // 文本內(nèi)容 TextView textView = new TextView(this); textView.setText("加載中..."); textView.setGravity(Gravity.CENTER_VERTICAL); // 把文本加入到layout中 layout.addView(textView, FFlayoutParams); // 設(shè)置layout的重力方向,即對(duì)齊方式是 layout.setGravity(Gravity.CENTER); // 設(shè)置ListView的頁(yè)腳layout loadingLayout = new LinearLayout(this); loadingLayout.addView(layout, mLayoutParams); loadingLayout.setGravity(Gravity.CENTER); // 得到一個(gè)ListView用來(lái)顯示條目 listView = (ListView) findViewById(R.id.tv); // 添加到腳頁(yè)顯示 listView.addFooterView(loadingLayout); // 給ListView添加適配器 listView.setAdapter(adapter); // 給ListView注冊(cè)滾動(dòng)監(jiān)聽(tīng) listView.setOnScrollListener(this); } /** * 要用用于生成顯示數(shù)據(jù) * * @author huangbq */ class listViewAdapter extends BaseAdapter { int count = 10; 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; if (v == null) { view = new TextView(ListViewForLoading.this); } else { view = (TextView) v; } view.setText("ListItem " + pos); view.setTextSize(20f); view.setGravity(Gravity.CENTER); view.setHeight(60); return view; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub if (firstVisibleItem + visibleItemCount == totalItemCount) { // 開(kāi)線程去下載網(wǎng)絡(luò)數(shù)據(jù) if (mThread == null || !mThread.isAlive()) { mThread = new Thread() { @Override public void run() { try { // 這里放你網(wǎng)絡(luò)數(shù)據(jù)請(qǐng)求的方法,我在這里用線程休眠5秒方法來(lái)處理 Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; mThread.start(); } } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case 1: if (adapter.count <= 41) { adapter.count += 10; int currentPage = adapter.count / 10; Toast.makeText(getApplicationContext(), "第" + currentPage + "頁(yè)", Toast.LENGTH_LONG).show(); } else { listView.removeFooterView(loadingLayout); } // 重新刷新Listview的adapter里面數(shù)據(jù) adapter.notifyDataSetChanged(); break; default: break; } } }; }
main.xml別忘了加這段了
關(guān)于怎么在Android中利用Listview動(dòng)態(tài)加載數(shù)據(jù)問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。