otak info official Logo   
OTAK.INFO
Personal Blog

Hariyanto Lim

정길상 / 鄭吉祥
Home   Last Posts  
 
Please login to see more topics and discussion categories.
 
goto main category list >> Software programming >> Android

Title : How to make ListView showing Images thumbnail from remote server flicking/flinging faster ?
Total Reply : 0
Total View : 1904


Harry
Harry
Total thread: 131
Total reply: 50

Post #170
How to make ListView showing Images thumbnail from remote server flicking/flinging faster ?
In ListView or GridView, sometimes we need to put thumbnail image and the image is retrieved from remote server, if we use Volley's NetworkImageView to display image then we use ImageLoader and RequestQueue such as:
  private RequestQueue mRequestQueue;
  private ImageLoader mImageLoader;
  
  private init() {
    mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
      //...
    }
  }
  
  private boolean isRequestQueueStarted = false;

  public void stopRequestQueue() {
    //LogHelper.d(TAG, "stopRequestQueue()");

    if(isRequestQueueStarted) {
      //LogHelper.d(TAG, "stopRequestQueue(), current state is started, so going to stop it now");
      isRequestQueueStarted = false;
      mRequestQueue.stop();

      /**
       * 20160126 : Harry implement AUTOMATIC RE-START RequestQueue by other Thread
       * REASON: sometime during ListView flinging then stop RequestQueue BUT
       * move to other Activity without restarting this RequestQueue
       */
      // use Handler to avoid creating new Thread & new Runnable !!
      mAutomaticRestartRequestQueueHandler.removeMessages(MESSAGE_TO_AUTOMATIC_RESTART_REQUEST_QUEUE);
      mAutomaticRestartRequestQueueHandler.sendEmptyMessageDelayed(MESSAGE_TO_AUTOMATIC_RESTART_REQUEST_QUEUE, AUTOMATIC_RESTART_REQUEST_QUEUE_DELAY_TIME);

    } else {
      //LogHelper.d(TAG, "stopRequestQueue(), current state is ALREADY stopped");
    }
  }

  public void startRequestQueue() {
    //LogHelper.d(TAG, "startRequestQueue()");

    if(isRequestQueueStarted) {
      //LogHelper.d(TAG, "startRequestQueue(), current state is ALREADY started");
    } else {
      //LogHelper.d(TAG, "startRequestQueue(), current state is stopped, so going to start it now");
      isRequestQueueStarted = true;
      mRequestQueue.start();
    }
  }

  private final long AUTOMATIC_RESTART_REQUEST_QUEUE_DELAY_TIME = 2500;
  private final int MESSAGE_TO_AUTOMATIC_RESTART_REQUEST_QUEUE = 0;

  private Handler mAutomaticRestartRequestQueueHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      LogHelper.d(TAG, "*** RECEIVED request to restart RequestQueue ***");

      if( ! isRequestQueueStarted) {
        LogHelper.d(TAG, "*** isRequestQueueStarted: " + isRequestQueueStarted + ", so restart RequestQueue now ***");
        startRequestQueue();
      }
    }
  };
In our ListView / GridView setup, we need to detect fling action, eg:
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
      //LogHelper.d(TAG, "flinging .. stop RequestQueue");
      HVolleyImageCacheSingleton.getInstance().stopRequestQueue();
    } else {
      //LogHelper.d(TAG, "NOT flinging .. start RequestQueue");
      HVolleyImageCacheSingleton.getInstance().startRequestQueue();
    }
  }

  @Override
  public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    // 20150604 : display TOP button AFTER the SECOND ITEM is over the top screen
    if (firstVisibleItem > 1) {
      topButton.setVisibility(View.VISIBLE);
    } else {
      topButton.setVisibility(View.GONE);
    }
  }
});
After the above implementation, our ListView will fling faster because there is NO "lazy loading" in the background.
Falling in love with the world

Write : 2016-01-27 14:12:52
Last edit : 2016-03-24 14:16:34

If you want to create a new reply then please login first.



www.OTAK.INFO
Since 19 January 2007
Page hit : 712,098

Code update 24th June 2013
Brain is a very capable to solve big problems
but requires constant reminders about how to.
peace bird