您好,登錄后才能下訂單哦!
怎么在Android中利用Listview動態加載數據?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
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; /** * 設置布局顯示屬性 */ private LayoutParams mLayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); /** * 設置布局顯示目標最大化屬性 */ 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); // 設置布局 水平方向 layout.setOrientation(LinearLayout.HORIZONTAL); // 進度條 progressBar = new ProgressBar(this); // 進度條顯示位置 progressBar.setPadding(0, 0, 15, 0); // 把進度條加入到layout中 layout.addView(progressBar, mLayoutParams); // 文本內容 TextView textView = new TextView(this); textView.setText("加載中..."); textView.setGravity(Gravity.CENTER_VERTICAL); // 把文本加入到layout中 layout.addView(textView, FFlayoutParams); // 設置layout的重力方向,即對齊方式是 layout.setGravity(Gravity.CENTER); // 設置ListView的頁腳layout loadingLayout = new LinearLayout(this); loadingLayout.addView(layout, mLayoutParams); loadingLayout.setGravity(Gravity.CENTER); // 得到一個ListView用來顯示條目 listView = (ListView) findViewById(R.id.tv); // 添加到腳頁顯示 listView.addFooterView(loadingLayout); // 給ListView添加適配器 listView.setAdapter(adapter); // 給ListView注冊滾動監聽 listView.setOnScrollListener(this); } /** * 要用用于生成顯示數據 * * @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) { // 開線程去下載網絡數據 if (mThread == null || !mThread.isAlive()) { mThread = new Thread() { @Override public void run() { try { // 這里放你網絡數據請求的方法,我在這里用線程休眠5秒方法來處理 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 + "頁", Toast.LENGTH_LONG).show(); } else { listView.removeFooterView(loadingLayout); } // 重新刷新Listview的adapter里面數據 adapter.notifyDataSetChanged(); break; default: break; } } }; }
main.xml別忘了加這段了
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ListView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
關于怎么在Android中利用Listview動態加載數據問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。