How to schedule hard work for later display for Listviews?

I have a list of 200 items. I use a custom view for each row. There is a code that takes time to calculate, and because of this, the list freezes when scrolling and loading in slow (2-3 seconds).

I have subclasses of SimpleCursorAdapter with both Filterable and SectionIndexer.

I mean, first display the name of the record and insert the stream into the calculation and appear later when this is done.

How can I cancel some work and then refresh the list to include the calculated data? This should appear on the fly without user interaction.

+5
source share
2 answers

Since Android has a unique Android interface, it will be blocked whenever you perform a long operation.

You can perform regular java thread processing and have a handler () in the main class of the user interface to which you can return information. But..

The easiest way is to create AsyncTask (). They are java threads with minor changes. They can perform the user interface operation before / after the main calculation. For example:

 private class AndroidThread extends AsyncTask {
      protected Object doInBackground(Object... params) {
           return result;
      }

      protected void onPostExecute(Object result) {
           // do things here with the result of the doInBackground function
           // Like: "UiElement"."performTask(result)"
      }
 }

For more information about AsyncTask, see this.

+2
source

Hmm, interesting question. Here are some quick thoughts:

1) , , .. , , . , "...", , .

2) , , , ListView onScrollStateChanged listener. , . , , "fling", , .

3) , . - , , , , . , , , , . , . , .

4) , , notifyDatasetChanged() . , ListView , , , .

, !


: , , , , ListView.OnScrollListener. ApiDemos List13, :

public class YourClass extends ListActivity implements ListView.OnScrollListener {
    public void onCreate(Bundle sIS){
        (...)
        // Note, you may have to write your own list adapter, extending BaseAdapter
        // See List13 in Api Demos for an example if you're not sure how to do that
        setListAdapter(yourImageAdapter); 
        getListView().setOnScrollListener(this);
    }

    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {
        Log.d(LOG_TAG, "onScroll, firstVisibleItem = " + firstVisibleItem +
                ", visibleItemCount = " + visibleItemCount
                + ", totalItemCount = " + totalItemCount);
    }

    public void onScrollStateChanged(AbsListView view, int scrollState) {
        Log.d(LOG_TAG, "onScrollStateChanged called, scrollState = " + scrollState);
        switch (scrollState) {
        case OnScrollListener.SCROLL_STATE_IDLE:
            int first = view.getFirstVisiblePosition();
            int count = view.getChildCount();

            Log.d(LOG_TAG, "first = " + first + ", count = " + count);

            break;
        case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
            //Scrolling with the finger touching the screen
            break;

        case OnScrollListener.SCROLL_STATE_FLING:
            // User 'threw' the list up or down and it scrolling rapidly
            break;
        }
    }
}

: , .. , , .

BaseAdapter: - , , , , BaseAdapter , "" . , , getView. , - "Loading...", , .

, , ?

+1

All Articles