android Listview分批載入+自動載入(改進版)(附源碼下載)

來源:互聯網
上載者:User

這次在代碼上比上一次改進了一些,並且載入完全部資料後會在lisview底部顯示一個“已載入全部“的textview。大家可以對比我的上一篇部落格

android Listview分批載入+自動載入(附源碼下載)看看代碼有啥不同


直接上代碼:

public class TestForListviewActivity extends Activity implementsOnScrollListener {private View mFooterView;private LinearLayout mloadingLinear;//正在載入時顯示的viewprivate TextView mLoadFinishTextView;//載入全部資料後顯示的viewprivate final int LOAD_STATE_IDLE=0;//沒有在載入,並且伺服器上還有資料沒載入private final int LOAD_STATE_LOADING=1;//正在載入狀態private final int LOAD_STATE_FINISH=2;//表示伺服器上的全部資料都已載入完畢private int loadState=LOAD_STATE_IDLE;//記錄載入的狀態private final int MAX_COUNT=15;//表示伺服器總共有MAX_COUNT條資料private final int EACH_COUNT=10;//表示每次載入的條數private ListView mListview = null;private PaginationAdapter mAdapter;private Handler handler=new Handler();@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mFooterView = getLayoutInflater().inflate(R.layout.loadmore, null);mloadingLinear=(LinearLayout) mFooterView.findViewById(R.id.loading_linear);mLoadFinishTextView=(TextView) mFooterView.findViewById(R.id.load_finish_textview);mListview = (ListView) findViewById(R.id.listview);mListview.addFooterView(mFooterView);// 設定列表底部視圖List news=new ArrayList();mAdapter = new PaginationAdapter(news);;mListview.setAdapter(mAdapter);//設定setOnScrollListener會自動調用onscroll方法。mListview.setOnScrollListener(this);}public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {Log.i("onS", "firstVisibleItem"+firstVisibleItem+" visibleItemCount"+visibleItemCount+" totalItemCount"+totalItemCount);if (firstVisibleItem+visibleItemCount==totalItemCount) {if(loadState==LOAD_STATE_IDLE){Log.i("onScroll", "firstVisibleItem"+firstVisibleItem+" visibleItemCount"+visibleItemCount+" totalItemCount"+totalItemCount);loadState=LOAD_STATE_LOADING;loadMore();}}}public void onScrollStateChanged(AbsListView arg0, int scrollState) {Log.i("onScrollStateChanged", scrollState+"");}private void loadMore(){//等待2秒之後才載入,類比網路等待時間為2shandler.postDelayed(new Runnable() {public void run() {loadData();mAdapter.notifyDataSetChanged();if (loadState==LOAD_STATE_FINISH) {//載入全部資料後在底部顯示一個“已載入全部”的textviewmloadingLinear.setVisibility(View.GONE);mLoadFinishTextView.setVisibility(View.VISIBLE);}}},2000);}private void loadData() {int dataIndex;//要載入的資料的index(從0開始)int count = mAdapter.getCount();//如果服務端還有資料,則繼續載入更多for (dataIndex = count; dataIndex < Math.min(count+EACH_COUNT, MAX_COUNT); dataIndex++) {News item = new News();item.setTitle("Title" + dataIndex);item.setContent("This is News Content" + dataIndex);mAdapter.addNewsItem(item);}//如果伺服器上的全部資料都已載入完畢if (dataIndex==MAX_COUNT) {loadState=LOAD_STATE_FINISH;}else {loadState=LOAD_STATE_IDLE;}}class PaginationAdapter extends BaseAdapter {List newsItems;public PaginationAdapter(List newsitems) {this.newsItems = newsitems;}public int getCount() {return newsItems==null?0:newsItems.size();}public Object getItem(int position) {return newsItems.get(position);}public long getItemId(int position) {return position;}public void addNewsItem(News newsitem) {newsItems.add(newsitem);}public View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) {convertView = getLayoutInflater().inflate(R.layout.list_item,null);}// 新聞標題TextView tvTitle = (TextView) convertView.findViewById(R.id.newstitle);tvTitle.setText(newsItems.get(position).getTitle());// 新聞內容TextView tvContent = (TextView) convertView.findViewById(R.id.newscontent);tvContent.setText(newsItems.get(position).getContent());return convertView;}}}


改進版demo下載:源碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.