您好,登錄后才能下訂單哦!
ListView中怎么動態(tài)加載數(shù)據(jù)模板,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
對于做Android應用程序來說,ListView一定用得非常多,經(jīng)常解析xml文件然后在ListView中顯示,往往我們需要讓它動態(tài)地加載內(nèi)容,也就是不一次性地加載完成,對于手機來說,這點很重要!筆者之前也是弄的不明白,用的時候直接在網(wǎng)上找代碼,復制粘貼,再修改一下就是自己的了,筆者的這個小Demo靈感來自于新浪或者騰訊微博手機客戶端,比如***頁加載十條Item,當翻到***一個Item的時候,會出來一個Button,當點擊Button的時候就會繼續(xù)加載更多的,當然如果不點,就不會加載的,節(jié)約用戶流量,先看效果圖
如果覺得滿意的話就繼續(xù)看下面的代碼吧,筆者這里沒有用到main.xml文件,全部在java文件中定義的,如果理解了的話自己可以在xml文件里面寫布局
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;// 全局變量,用來記錄ScrollView的滾動狀態(tài),1表示開始滾動,2表示正在滾動,0表示滾動停止 int visibleItemCount;// 當前可見頁面中的Item總數(shù) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * "加載項"布局,此布局被添加到ListView的Footer中。 */ mLoadLayout = new LinearLayout(this); mLoadLayout.setMinimumHeight(30); mLoadLayout.setGravity(Gravity.CENTER); mLoadLayout.setOrientation(LinearLayout.VERTICAL); /* * 當點擊按鈕的時候顯示這個View,此View使用水平方式布局,左邊是一個進度條,右邊是文本,默認設為不可見 */ 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);// 為布局添加進度條 TextView mTipContent = new TextView(this); mTipContent.setText("加載中..."); mProgressLoadLayout.addView(mTipContent, mLayoutParams);// 為布局添加文本 mProgressLoadLayout.setVisibility(View.GONE);// 默認設為不可見,注意View.GONE和View.INVISIBLE的區(qū)別 mLoadLayout.addView(mProgressLoadLayout);// 把之前的布局以View對象添加進來 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) { // 當點擊時把帶進度條的Layout設為可見,把Button設為不可見 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ù)成功時把Layout設為不可見,把Button設為可見 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中怎么動態(tài)加載數(shù)據(jù)模板的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。