Android Open Source - RZAndroidBaseUtils Async Drawable List Example Activity






From Project

Back to project page RZAndroidBaseUtils.

License

The source code is released under:

MIT License

If you think the Android project RZAndroidBaseUtils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.raizlabs.baseutils.examples.asyncdrawable;
// ww  w  .  j  av a 2s . co m
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.raizlabs.baseutils.examples.R;
import com.raizlabs.graphics.drawable.async.AsyncDrawableTask;
import com.raizlabs.graphics.drawable.async.AsyncDrawableUtils;
import com.raizlabs.graphics.drawable.async.BaseAsyncDrawableTask;
import com.raizlabs.widget.adapters.SimpleGenericAdapter;

public class AsyncDrawableListExampleActivity extends Activity {
  private static final int LIST_SIZE = 200;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_asyncdrawable_list);
    
    // Populate the list view with delayed drawables for each item
    ListView listView = (ListView) findViewById(R.id.asyncdrawable_list_list);
    // Attach an adapter that loads cells with a delayed image in them
    // See the comments on DelayedDrawableAdapter and DelayedDrawable below for more info
    DelayedDrawableAdapter adapter = new DelayedDrawableAdapter(this, R.layout.simpleviewholderstrategy_row);
    listView.setAdapter(adapter);
    
    // Populate a list of 200 integers, which will be the ID of each item
    List<Integer> items = new ArrayList<Integer>(LIST_SIZE);
    for (int i = 0; i < LIST_SIZE; ++i) {
      items.add(i);
    }
    
    adapter.loadItemList(items);
  }  
  
  // Pool to run our background execution/fetching
  ExecutorService threadPool = Executors.newFixedThreadPool(5);
  
  /**
   * Adapter implementation which binds rows to a DelayedDrawable as declared below
   */
  private class DelayedDrawableAdapter extends SimpleGenericAdapter<Integer> {

    public DelayedDrawableAdapter(Context context, int layoutResID) {
      super(context, layoutResID);
    }

    @Override
    protected void populateView(final Integer item, View view) {
      // Grab the view that we'll be putting the image in
      View imgView = view.findViewById(R.id.simpleStrategy_row_color);
      // Create a drawable for that view, and bind it to the 
      final DelayedDrawable drawable = new DelayedDrawable(imgView, item, 2000);
      // If there is work to do, execute the task on our thread pool
      if (drawable.bind()) {
        threadPool.execute(new Runnable() {
          @Override
          public void run() {
            drawable.execute();
          }
        });
      }
      // Bind our click listener so clicking the image cancels the task
      imgView.setOnClickListener(imageViewClickListener);
      
      ((TextView) view.findViewById(R.id.simpleStrategy_row_title)).setText(String.format("Item %s", item));
      ((TextView) view.findViewById(R.id.simpleStrategy_row_subTitle)).setText("");
    }
    
    private OnClickListener imageViewClickListener = new OnClickListener() {
      @Override
      public void onClick(View v) {
        // On click, try to grab the async drawable and cancel it
        AsyncDrawableTask<?> task = AsyncDrawableUtils.getTask(v);
        if (task != null) {
          task.cancel();
        }
      }
    };
  }

  /**
   * A cache for the DelayedDrawables to know which items are already loaded 
   */
  HashSet<Integer> loadedCache = new HashSet<Integer>();
  
  /**
   * Implementation of an {@link AsyncDrawableTask} which just pauses for a given
   * period of time to simulate work and then loads a given image resource, but is 
   * unique by some ID. Also checks against the local cache for items which are
   * already loaded, and returns their drawable immediately.
   * 
   * Note that in cases where we are reassigning the Drawable to the same view we
   * will automatically see that the existing task is already set up to do the work.
   * In this example though, the views are being recycled by the ListView. This means
   * when we scroll away from Item X, when we scroll back to it we may get a different
   * view which was bound to X+1 or X+2 etc. and the work will not match. Because of
   * this, we are caching which IDs have been loaded already.
   */
  private class DelayedDrawable extends BaseAsyncDrawableTask<Integer> {
    private Integer id;
    private long delay;

    /**
     * Constructs a DelayedDrawable for the given parameters
     * @param view The view to bind to
     * @param id The unique identifier of the item, to be used by the cache
     * @param delayMillis
     */
    public DelayedDrawable(View view, Integer id, long delayMillis) {
      super(view);
      this.id = id;
      delay = delayMillis;
    }

    @Override
    public Integer getKey() {
      // Use the resource ID as our key
      return id;
    }

    @Override
    protected Drawable onBind() {
      // If our ID is already in the cache, this row has already been
      // loaded, return the image instantly
      if (loadedCache.contains(id)) {
        return getResources().getDrawable(R.drawable.ic_launcher);
      }
      return null;
    }

    @Override
    protected Drawable doExecute() {
      // If our ID still isn't in the cache, simulate work -> sleep
      if (!loadedCache.contains(id)) {
        // We will be done at the current time + the delay
        final long endTime = SystemClock.elapsedRealtime() + delay;
        // Sleep until the end time to simulate work
        while (SystemClock.elapsedRealtime() < endTime) {
          try {
            synchronized (this) {
              // Wait on this for the remaining time
              // Do this through a wait to allow ourselves to be
              // woken up via a notify
              this.wait(endTime - SystemClock.elapsedRealtime());
            }
          } catch (InterruptedException e) { }

          // If we get woken up because of a cancel, return early
          if (isCancelled()) return null;
        }
      }
      // Add the id to the cache of loaded ids
      loadedCache.add(id);
      // Use the view to gain access to the resources.
      // If it's null, we won't be setting any drawable anyway
      View view = getView();
      if (view != null) {
        return view.getResources().getDrawable(R.drawable.ic_launcher);
      }
      return null;
    }

    @Override
    protected Drawable getLoadingDrawable() {
      // Use BLUE to represent loading
      return new ColorDrawable(Color.BLUE);
    }

    @Override
    protected Drawable getCancelledDrawable() {
      // Use RED to represent cancelled
      return new ColorDrawable(Color.RED);
    }

    @Override
    protected void onCancel() {
      // Notify ourselves to allow us to break early
      synchronized (this) {
        notify();
      }
    }

  }
}




Java Source Code List

com.raizlabs.baseutils.CompatibilityUtils.java
com.raizlabs.baseutils.IOUtils.java
com.raizlabs.baseutils.Logger.java
com.raizlabs.baseutils.Math.java
com.raizlabs.baseutils.StringUtils.java
com.raizlabs.baseutils.ThreadingUtils.java
com.raizlabs.baseutils.Wrapper.java
com.raizlabs.baseutils.examples.MainActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableExampleActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableListExampleActivity.java
com.raizlabs.baseutils.examples.simplegenericadapter.SimpleGenericAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewgroupadapter.ViewGroupAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewholderstrategy.SimpleViewHolderStrategyExampleActivity.java
com.raizlabs.collections.ListUtils.java
com.raizlabs.collections.MappableSet.java
com.raizlabs.collections.TransactionalHashSet.java
com.raizlabs.concurrent.BasePrioritizedRunnable.java
com.raizlabs.concurrent.ConcurrencyUtils.java
com.raizlabs.concurrent.PrioritizedRunnable.java
com.raizlabs.concurrent.Prioritized.java
com.raizlabs.content.sharing.SharingUtils.java
com.raizlabs.database.CursorIterable.java
com.raizlabs.database.CursorIterator.java
com.raizlabs.events.EventListener.java
com.raizlabs.events.Event.java
com.raizlabs.events.ProgressListener.java
com.raizlabs.events.SimpleEventListener.java
com.raizlabs.functions.Delegate.java
com.raizlabs.functions.Predicate.java
com.raizlabs.functions.Provider.java
com.raizlabs.graphics.ImageFactory.java
com.raizlabs.graphics.drawable.async.AsyncDrawableTask.java
com.raizlabs.graphics.drawable.async.AsyncDrawableUtils.java
com.raizlabs.graphics.drawable.async.AsyncDrawableWrapper.java
com.raizlabs.graphics.drawable.async.AsyncDrawable.java
com.raizlabs.graphics.drawable.async.BaseAsyncDrawableTask.java
com.raizlabs.imagecaching.ImageCache.java
com.raizlabs.imagecaching.PrefixedImageCacheAdapter.java
com.raizlabs.imagecaching.StubImageCache.java
com.raizlabs.json.JSONArrayParserDelegate.java
com.raizlabs.json.JSONHelper.java
com.raizlabs.synchronization.OneShotLock.java
com.raizlabs.tasks.RZAsyncTaskEvent.java
com.raizlabs.tasks.RZAsyncTaskListener.java
com.raizlabs.tasks.RZAsyncTask.java
com.raizlabs.util.observable.ObservableData.java
com.raizlabs.util.observable.ObservableListAdapter.java
com.raizlabs.util.observable.ObservableList.java
com.raizlabs.view.ViewCompatibility.java
com.raizlabs.view.animation.AnimationListenerWrapper.java
com.raizlabs.view.animation.RelativeLayoutParamsAnimation.java
com.raizlabs.view.animation.ResizeAnimation.java
com.raizlabs.widget.EvenLinearLayout.java
com.raizlabs.widget.ImageMixView.java
com.raizlabs.widget.SlideRevealLayout.java
com.raizlabs.widget.ViewUtils.java
com.raizlabs.widget.adapters.ListBasedAdapter.java
com.raizlabs.widget.adapters.SimpleGenericAdapter.java
com.raizlabs.widget.adapters.ViewGroupAdapter.java
com.raizlabs.widget.adapters.ViewHolderStrategyAdapter.java
com.raizlabs.widget.utils.SimpleViewHolderStrategy.java
com.raizlabs.widget.utils.ViewHolderStrategyConverter.java
com.raizlabs.widget.utils.ViewHolderStrategyUtils.java
com.raizlabs.widget.utils.ViewHolderStrategy.java