Recently, the listview page displays images and text (the parsed text content is downloaded first, and the image is loaded asynchronously). After you click the next page, the text is loaded and slide down immediately, at this time, the background uses the thread pool to asynchronously download images. I have 20 images on each page, that is, 20 images. This will cause the listview to slide and slow down!
This is what users don't want to see. I have referred to Netease news and e-market applications and found that they only load images on the screen, but it is unrealistic not to load, so I also made one. I'm a cainiao, and I admit that although I don't necessarily have the same experience with them, it does solve the choppy phenomenon at the moment of page turning.
Because I have not found any related articles on the internet, I hope it will be useful to my friends ~
The following is the relevant code (the page is not displayed ):
Java code
- /**
- * List rolling listener
- */
- Listview. setonscrolllistener (New onscrolllistener (){
- @ Override
- Public void onscrollstatechanged (abslistview view,
Int scrollstate ){
- // Todo auto-generated method stub
- // Asynchronously load the image
- If (scrollstate = onscrolllistener. scroll_state_idle) {// The image is loaded when the list stops scrolling.
- Pageimgload (_ start_index, _ end_index );
- }
- }
- @ Override
- Public void onscroll (abslistview view,
Int firstvisibleitem,
- Int visibleitemcount,
Int totalitemcount ){
- // Todo auto-generated method stub
- // Set the start index and end index displayed on the current screen
- _ Start_index = firstvisibleitem;
- _ End_index = firstvisibleitem + visibleitemcount;
- If (_ end_index> = totalitemcount ){
- _ End_index = totalitemcount-1;
- }
- }
- });
/*** List rolling listener */listview. setonscrolllistener (New onscrolllistener () {@ overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {// todo auto-generated method stub // asynchronously load image if (scrollstate = onscrolllistener. objects) {// when the list stops scrolling, The pageimgload (_ start_index, _ end_index) ;}@ overridepublic void onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {// todo auto-generated method stub // set the start index and end index_start_index = firstvisibleitem; _ end_index = firstvisibleitem + visibleitemcount; If (_ end_index> = totalitemcount) {_ end_index = totalitemcount-1 ;}}});
Java code
- /**
- * Only images from start_index to end_index are loaded.
- * @ Param start_index
- * @ Param end_index
- */
- Private void pageimgload (INT start_index,
Int end_index ){
- For (; start_index <end_index; start_index ++ ){
- Hashmap <string, Object> curr_item = Adapter. getitem (start_index );
- If (curr_item.get (constant. news_icon_url )! =
Null
- & Curr_item.get (constant. news_icon) = NULL ){
- LoadImage (curr_item );
- }
- }
- }
/*** Load only images from start_index to end_index * @ Param start_index * @ Param end_index */private void pageimgload (INT start_index, int end_index) {for (; start_index <end_index; start_index ++) {hashmap <string, Object> curr_item = adapter. getitem (start_index); If (curr_item.get (constant. news_icon_url )! = NULL & curr_item.get (constant. news_icon) = NULL) {LoadImage (curr_item );}}}
I used asynctask to load the Image Code asynchronously. However, after inheriting asynctask, it cannot be executed multiple times. Therefore, I used a thread to call handler to update the UI:
Java code
- /**
- * Asynchronously load images
- * @ Param curr_item
- */
- Private void LoadImage (final hashmap <string, Object> curr_item ){
- Executorservice. Submit (New runnable (){
- Public void run (){
- Try {
- Drawable curr_icon = NULL;
- String icon_url = (string) curr_item
- . Get (constant. news_icon_url );
- String newsid = (string) curr_item.get (constant. news_id );
- If (imagecache. containskey (icon_url) {// soft reference
- Softreference <drawable> softreference = imagecache
- . Get (icon_url );
- Curr_icon = softreference. Get ();
- System. Out. println ("case using softreference !!!!!!!!!!!!!!!!!!!! ");
- }
- If (curr_icon = NULL ){
- Httputils Hu = new httputils ();
- Fileutils Fu = new fileutils ();
- If (Hu. is_intent (home_activity.this )){
- Fu. write2localfromis (home_activity.this, newsid
- + Constant. save_news_icon_name
- + Constant. save_img_suffix,
- Hu. getisfromurl (icon_url ));
- }
- // Load images from the local machine. If there is no network, load the local image directly.
- Curr_icon = Fu. readdrawablefromlocal (
- Home_activity.this, newsid
- + Constant. save_news_icon_name
- + Constant. save_img_suffix );
- Imagecache. Put (icon_url, new softreference <drawable> (
- Curr_icon ));
- }
- Curr_item.put (constant. news_icon, curr_icon );
- // Send the UI to handler for update
- Message MSG = _ viewhandler. obtainmessage ();
- MSG. arg1 = constant. msg_list_img_ OK;
- MSG. sendtotarget ();
- } Catch (exception e ){
- Throw new runtimeexception (E );
- }
- }
- });
- }
/*** Asynchronously load the image * @ Param curr_item */private void LoadImage (final hashmap <string, Object> curr_item) {executorservice. submit (New runnable () {public void run () {try {drawable curr_icon = NULL; string icon_url = (string) curr_item.get (constant. news_icon_url); string newsid = (string) curr_item.get (constant. news_id); If (imagecache. containskey (icon_url) {// soft reference softreference <drawable> softreference = imageca Che. get (icon_url); curr_icon = softreference. get (); system. out. println ("case using softreference !!!!!!!!!!!!!!!!!!!! ");} If (curr_icon = NULL) {httputils Hu = new httputils (); fileutils Fu = new fileutils (); If (Hu. is_intent (home_activity.this) {Fu. write2localfromis (home_activity.this, newsid + constant. save_news_icon_name + constant. save_img_suffix, Hu. getisfromurl (icon_url);} // load the image locally. If no network is available, load the local image curr_icon = Fu. readdrawablefromlocal (home_activity.this, newsid + constant. save_news_icon_name + constant. save_img_suffix); imagecache. put (icon_url, new softreference <drawable> (curr_icon);} curr_item.put (constant. news_icon, curr_icon); // send the UI to handler to update message MSG = _ viewhandler. obtainmessage (); MSG. arg1 = constant. msg_list_img_ OK; MSG. sendtotarget ();} catch (exception e) {Throw new runtimeexception (e );}}});}
Java code
- Handler code:
Handler code:
Java code
- Handler _ viewhandler = new handler (){
Handler _ viewhandler = new handler (){
Java code
- @ Override
- Public void handlemessage (Message MSG ){
- Switch (msg. arg1 ){
- Case constant. msg_list_img_ OK:
- // Update the UI
- Adapter. notifydatasetchanged ();
- Break;
- }
- Super. handlemessage (MSG );
- }
- };
@ Overridepublic void handlemessage (Message MSG) {Switch (MSG. arg1) {Case constant. msg_list_img_ OK: // update uiadapter. yydatasetchanged (); break;} super. handlemessage (MSG );}};
The previous figure: