ListView中怎么動(dòng)態(tài)加載數(shù)據(jù)模板,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
目前創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、瓊中黎族網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
對(duì)于做Android應(yīng)用程序來(lái)說(shuō),ListView一定用得非常多,經(jīng)常解析xml文件然后在ListView中顯示,往往我們需要讓它動(dòng)態(tài)地加載內(nèi)容,也就是不一次性地加載完成,對(duì)于手機(jī)來(lái)說(shuō),這點(diǎn)很重要!筆者之前也是弄的不明白,用的時(shí)候直接在網(wǎng)上找代碼,復(fù)制粘貼,再修改一下就是自己的了,筆者的這個(gè)小Demo靈感來(lái)自于新浪或者騰訊微博手機(jī)客戶端,比如***頁(yè)加載十條Item,當(dāng)翻到***一個(gè)Item的時(shí)候,會(huì)出來(lái)一個(gè)Button,當(dāng)點(diǎn)擊Button的時(shí)候就會(huì)繼續(xù)加載更多的,當(dāng)然如果不點(diǎn),就不會(huì)加載的,節(jié)約用戶流量,先看效果圖
如果覺(jué)得滿意的話就繼續(xù)看下面的代碼吧,筆者這里沒(méi)有用到main.xml文件,全部在java文件中定義的,如果理解了的話自己可以在xml文件里面寫(xiě)布局
package com.focus.loading; import android.app.ListActivity; import android.os.Bundle; import android.os.Handler; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; public class ListViewActivity extends ListActivity implements OnScrollListener { private LinearLayout mLoadLayout; private LinearLayout mProgressLoadLayout; private ListView mListView; private ListViewAdapter mListViewAdapter = new ListViewAdapter(); private int mLastItem = 0; private int mCount = 41; private final Handler mHandler = new Handler();// 在Handler中加載數(shù)據(jù) private final LayoutParams mLayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); int scrollState;// 全局變量,用來(lái)記錄ScrollView的滾動(dòng)狀態(tài),1表示開(kāi)始滾動(dòng),2表示正在滾動(dòng),0表示滾動(dòng)停止 int visibleItemCount;// 當(dāng)前可見(jiàn)頁(yè)面中的Item總數(shù) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * "加載項(xiàng)"布局,此布局被添加到ListView的Footer中。 */ mLoadLayout = new LinearLayout(this); mLoadLayout.setMinimumHeight(30); mLoadLayout.setGravity(Gravity.CENTER); mLoadLayout.setOrientation(LinearLayout.VERTICAL); /* * 當(dāng)點(diǎn)擊按鈕的時(shí)候顯示這個(gè)View,此View使用水平方式布局,左邊是一個(gè)進(jìn)度條,右邊是文本,默認(rèn)設(shè)為不可見(jiàn) */ mProgressLoadLayout = new LinearLayout(this); mProgressLoadLayout.setMinimumHeight(30); mProgressLoadLayout.setGravity(Gravity.CENTER); mProgressLoadLayout.setOrientation(LinearLayout.HORIZONTAL); ProgressBar mProgressBar = new ProgressBar(this); mProgressBar.setPadding(0, 0, 15, 0); mProgressLoadLayout.addView(mProgressBar, mLayoutParams);// 為布局添加進(jìn)度條 TextView mTipContent = new TextView(this); mTipContent.setText("加載中..."); mProgressLoadLayout.addView(mTipContent, mLayoutParams);// 為布局添加文本 mProgressLoadLayout.setVisibility(View.GONE);// 默認(rèn)設(shè)為不可見(jiàn),注意View.GONE和View.INVISIBLE的區(qū)別 mLoadLayout.addView(mProgressLoadLayout);// 把之前的布局以View對(duì)象添加進(jìn)來(lái) final Button button = new Button(this); button.setText("加載更多"); // 添加按鈕 mLoadLayout.addView(button, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mLastItem == mListViewAdapter.count && scrollState == OnScrollListener.SCROLL_STATE_IDLE) { // 當(dāng)點(diǎn)擊時(shí)把帶進(jìn)度條的Layout設(shè)為可見(jiàn),把Button設(shè)為不可見(jiàn) mProgressLoadLayout.setVisibility(View.VISIBLE); button.setVisibility(View.GONE); if (mListViewAdapter.count <= mCount) { mHandler.postDelayed(new Runnable() { @Override public void run() { mListViewAdapter.count += 10; mListViewAdapter.notifyDataSetChanged(); mListView.setSelection(mLastItem - visibleItemCount + 1); // 獲取數(shù)據(jù)成功時(shí)把Layout設(shè)為不可見(jiàn),把Button設(shè)為可見(jiàn) mProgressLoadLayout.setVisibility(View.GONE); button.setVisibility(View.VISIBLE); } }, 2000); } } } }); mListView = getListView(); mListView.addFooterView(mLoadLayout); setListAdapter(mListViewAdapter); mListView.setOnScrollListener(this); } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { this.visibleItemCount = visibleItemCount; mLastItem = firstVisibleItem + visibleItemCount - 1; if (mListViewAdapter.count > mCount) { mListView.removeFooterView(mLoadLayout); } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { this.scrollState = scrollState; } class ListViewAdapter extends BaseAdapter { int count = 10; public int getCount() { return count; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View view, ViewGroup parent) { TextView mTextView; if (view == null) { mTextView = new TextView(ListViewActivity.this); } else { mTextView = (TextView) view; } mTextView.setText("Item " + position); mTextView.setTextSize(20f); mTextView.setGravity(Gravity.CENTER); mTextView.setHeight(60); return mTextView; } } }
看完上述內(nèi)容,你們掌握ListView中怎么動(dòng)態(tài)加載數(shù)據(jù)模板的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!