Nothing left to do today,
Then we tested the listview image loading optimization function,
When we use Sina Weibo, our careful students must have discovered that during the Sliding Process, the images were not loaded,
The image is loaded only when the slide stops.
Let's make such an effect today.
We should first consider two issues:
1. How do I obtain the image control to be loaded when sliding stops?
2. Because the OnScrollListener onScrollStateChanged and onScroll are not triggered when the listiew Initialization is complete, how do I initialize the images on the first page?
Package com. test. list; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. viewGroup; import android. widget. absListView; import android. widget. absListView. onScrollListener; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. listView; public class TestAndroidListActivity extends Activity {private in T start_index, end_index; // determines whether the private boolean isInit = false; private ListView list; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); this. initList ();} private BaseAdapter adapter = new BaseAdapter () {@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubView v; I F (convertView = null) {v = getLayoutInflater (). inflate (R. layout. list_item, null); v. setTag (v);} else {v = (View) convertView. getTag (); ImageView iv = (ImageView) v. findViewById (R. id. img); iv. setImageResource (R. drawable. ic_launcher);} ImageView iv = (ImageView) v. findViewById (R. id. img); // set the corresponding position ID for the image control iv. setTag (position); if (! IsInit) {iv. setImageResource (R. drawable. update_log) ;}return v ;}@ Overridepublic long getItemId (int position) {// TODO Auto-generated method stubreturn 0 ;}@ Overridepublic Object getItem (int position) {// TODO Auto-generated method stubreturn null;} @ Overridepublic int getCount () {// TODO Auto-generated method stubreturn 140 ;}; private void initList () {// TODO Auto-generated method stublist = (ListView) this. findViewById (R. id. list); list. setAdapter (adapter); list. listener (new OnScrollListener () {@ Overridepublic void onScrollStateChanged (AbsListView view, int scrollState) {// TODO Auto-generated method stubisInit = true; switch (scrollState) {case OnScrollListener. SCROLL_STATE_IDLE: // slide stop for (; start_index <end_index; start_index ++) {ImageView img = (ImageView) list. findViewWithTag (start_index); img. setImageResource (R. drawable. update_log) ;}break; default: break ;}@ Overridepublic void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {// TODO Auto-generated method stub // set the start index and end indexstart_index = firstVisibleItem; end_index = firstVisibleItem + visibleItemCount ;}});}}
In fact, it is very simple. You can study it!