Android listview does not load images during slide process. images are loaded when they are stopped.

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.