Android ListView 的动态加载数据
您的评价: |
|
|
001 | package com.src.hero; |
002 |
003 | import android.app.Activity; |
004 | import android.content.Context; |
005 | import android.os.Bundle; |
006 | import android.os.Handler; |
007 | import android.os.Message; |
008 | import android.view.Gravity; |
009 | import android.view.View; |
010 | import android.view.ViewGroup; |
011 | import android.view.ViewGroup.LayoutParams; |
012 | import android.widget.AbsListView; |
013 | import android.widget.AbsListView.OnScrollListener; |
014 | import android.widget.BaseAdapter; |
015 | import android.widget.LinearLayout; |
016 | import android.widget.ListView; |
017 | import android.widget.ProgressBar; |
018 | import android.widget.TextView; |
019 | import android.widget.Toast; |
020 | /** |
021 | * @author Hero |
022 | * 2012-3-8 下午11:04:21 |
023 | * 实现动态加载的效果 |
024 | */ |
025 | public class DynamicLoadActivity extends Activity { |
026 | private ListView lv_main; |
027 | private LinearLayout linearlayout = null ; |
028 | private LayoutParams wlayoutparams, flayoutparams; |
029 | listAdapter adpter = null ; |
030 | Thread thread; |
031 |
032 | /** Called when the activity is first created. */ |
033 | @Override |
034 | public void onCreate(Bundle savedInstanceState) { |
035 | super .onCreate(savedInstanceState); |
036 | setContentView(R.layout.main); |
037 | lv_main = (ListView) findViewById(R.id.listView1); |
038 | adpter = new listAdapter( this ); |
039 | wlayoutparams = new LinearLayout.LayoutParams( 320 , 50 ); // 定义的是FootView的宽高 |
040 | flayoutparams = new LinearLayout.LayoutParams( |
041 | LinearLayout.LayoutParams.WRAP_CONTENT, |
042 | LinearLayout.LayoutParams.WRAP_CONTENT); // 定义FootView中控件的布局 |
043 | ProgressBar progressbar = new ProgressBar( this ); // 定义一个ProgressBar |
044 | progressbar.setPadding( 50 , 0 , 0 , 0 ); // left, top, right, bottom |
045 | TextView tv_wait = new TextView( this ); // 定义一个TextView |
046 | tv_wait.setText( "正在动态加载……" ); |
047 | tv_wait.setTextSize(20f); |
048 | tv_wait.setGravity(Gravity.CENTER_VERTICAL); |
049 | tv_wait.setPadding( 10 , 0 , 0 , 0 ); |
050 | LinearLayout layout = new LinearLayout( this ); |
051 | layout.setOrientation(LinearLayout.HORIZONTAL); |
052 | layout.addView(progressbar, flayoutparams); |
053 | layout.addView(tv_wait, flayoutparams); |
054 | layout.setGravity(Gravity.CENTER); |
055 | linearlayout = new LinearLayout( this ); |
056 | linearlayout.addView(layout, wlayoutparams); |
057 | linearlayout.setGravity(Gravity.CENTER_VERTICAL); |
058 | lv_main.addFooterView(linearlayout); |
059 | lv_main.setAdapter(adpter); |
060 | lv_main.setOnScrollListener(scroller); |
061 | } |
062 | /** |
063 | * 滚动监听器 |
064 | */ |
065 | OnScrollListener scroller = new OnScrollListener() { |
066 | @Override |
067 | public void onScroll(AbsListView view, int firstVisibleItem, |
068 | int visibleItemCount, int totalItemCount) { |
069 | // TODO Auto-generated method stub |
070 | if (firstVisibleItem + visibleItemCount == totalItemCount) { |
071 | // 启动一个线程去加载网络数据 |
072 | if (thread == null || !thread.isAlive()) { |
073 | thread = new Thread() { |
074 | @Override |
075 | public void run() { |
076 | try { |
077 | // 处理网络方法,在这里用线程休眠3秒方法来处理 |
078 | Thread.sleep( 3000 ); |
079 | } catch (InterruptedException e) { |
080 | e.printStackTrace(); |
081 | } |
082 | Message message = new Message(); |
083 | message.what = 1 ; |
084 | handler.sendMessage(message); |
085 | } |
086 | }; |
087 | thread.start(); |
088 | } |
089 | } |
090 | } |
091 |
092 | @Override |
093 | public void onScrollStateChanged(AbsListView view, int scrollState) { |
094 | // TODO Auto-generated method stub |
095 |
096 | } |
097 |
098 | }; |
099 | /** |
100 | * 消息队列 |
101 | */ |
102 | private Handler handler = new Handler() { |
103 | @Override |
104 | public void handleMessage(Message msg) { |
105 | // TODO Auto-generated method stub |
106 | switch (msg.what) { |
107 | case 1 : |
108 | if (adpter.count <= 101 ) { |
109 | adpter.count += 10 ; |
110 | int currentPage = adpter.count / 10 ; |
111 | Toast.makeText(getApplicationContext(), |
112 | "第" + currentPage + "页" , Toast.LENGTH_LONG).show(); |
113 | } else { |
114 | lv_main.removeFooterView(linearlayout); |
115 | } |
116 | // 重新刷新Listview的adapter里面数据 |
117 | adpter.notifyDataSetChanged(); |
118 | break ; |
119 | default : |
120 | break ; |
121 | } |
122 | } |
123 | }; |
124 |
125 | public class listAdapter extends BaseAdapter { |
126 |
127 | int count = 10 ; |
128 | Context context; |
129 |
130 | public listAdapter(Context context) { |
131 | this .context = context; |
132 | } |
133 |
134 | @Override |
135 | public int getCount() { |
136 | // TODO Auto-generated method stub |
137 | return count; |
138 | } |
139 |
140 | @Override |
141 | public Object getItem( int position) { |
142 | // TODO Auto-generated method stub |
143 | return position; |
144 | } |
145 |
146 | @Override |
147 | public long getItemId( int item) { |
148 | // TODO Auto-generated method stub |
149 | return item; |
150 | } |
151 |
152 | @Override |
153 | public View getView( int position, View convertView, ViewGroup parent) { |
154 | // TODO Auto-generated method stub |
155 | TextView tv_content; |
156 | if (convertView == null ) { |
157 | tv_content = new TextView(context); |
158 | } else { |
159 | tv_content = (TextView) convertView; |
160 | } |
161 | tv_content.setText( "Item " + position); |
162 | tv_content.setTextSize(20f); //字体的大小20f |
163 | tv_content.setGravity(Gravity.CENTER); //显示在中间 |
164 | tv_content.setHeight( 50 ); //高度为50 |
165 | return tv_content; |
166 | } |
167 |
168 | } |
169 | } |
![Android ListView 的动态加载效果](https://i-blog.csdnimg.cn/blog_migrate/1c2bbc9b2ca1acdecf7ed05abf986853.gif)
![Android ListView 的动态加载效果](https://i-blog.csdnimg.cn/blog_migrate/55133e35879b45686a95ae9885e73384.gif)