AnyncTaskLoader写法

 

以加载图库的照片为例
解释都在代码中,看吧

代码块

package auggie.gallery;



import android.content.ContentResolver;

import android.content.Context;

import android.database.Cursor;

import android.provider.MediaStore;

import android.support.v4.content.AsyncTaskLoader;

import android.util.SparseArray;



import java.util.ArrayList;

import java.util.List;



/**

 * Created With Android Studio

 * User @47

 * Date 2014-08-25

 * Time 10:29

 * 加载 相册的图片

 */

public class GalleryImageLoader extends AsyncTaskLoader<List<OImageBean>> {



    /** 需要查询的列**/

    public static final String[] QUERY_COLUMN_THUMBS = {

            MediaStore.Images.Thumbnails._ID,

            MediaStore.Images.Thumbnails.DATA,//图片在内存卡地址

            MediaStore.Images.Thumbnails.IMAGE_ID

    };





    /** 需要查询的列**/

    public static final String[] QUERY_COLUMN_ORIGINAL = {

            MediaStore.Images.ImageColumns._ID,

            MediaStore.Images.ImageColumns.DATA,//图片在内存卡地址

            MediaStore.Images.ImageColumns.SIZE,//图片大小

            MediaStore.Images.ImageColumns.DISPLAY_NAME,//显示的名字

            MediaStore.Images.ImageColumns.DATE_MODIFIED,//修改日期

            MediaStore.Images.ImageColumns.BUCKET_ID,//文件夹ID

            MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME//文件夹名字

    };



    private List<OImageBean> mImageList;



    private List<BucketBean> mBucketList;

    private SparseArray<BucketBean> mBucketArray;



    public List<BucketBean> getBucketList() {

        if(mBucketList == null){

            mBucketList = new ArrayList<BucketBean>();

            int size = mBucketArray.size();

            for(int i = 0 ; i < size ; i++){

                mBucketList.add(mBucketArray.get(mBucketArray.keyAt(i)));

            }

        }

        return mBucketList;

    }



    public GalleryImageLoader(Context context) {

        super(context);

    }



    @Override

    public List<OImageBean> loadInBackground() {

        mBucketList = null;//清理

        mImageList = new ArrayList<OImageBean>();

        ContentResolver contentResolver = getContext().getContentResolver();

        SparseArray<OImageBean> indexById  = new SparseArray<OImageBean>();

        Cursor cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,

                QUERY_COLUMN_ORIGINAL,

                null,null,MediaStore.Images.Media.DATE_MODIFIED+" DESC");

        if(!cursor.moveToFirst()) return mImageList;



        mBucketArray = new SparseArray<BucketBean>();

        do {

            OImageBean bean = new OImageBean(cursor);

            mImageList.add(bean);

            indexById.append(bean.id, bean);

            //添加文件夹列表中

            BucketBean bucketBean =

                    mBucketArray.get(

                            bean.bucketID,

                            new BucketBean(bean.bucketID,bean.bucketDisplayName));

            mBucketArray.append(bucketBean.buckID,bucketBean);

            bucketBean.oImageBeanList.add(bean);

        } while (cursor.moveToNext());

        cursor.close();



        //查询缩略图d

        cursor = contentResolver.query(

                MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,

                QUERY_COLUMN_THUMBS,null,null,

                MediaStore.Images.Thumbnails.IMAGE_ID + " DESC");

        //获取缩略图

        if(cursor.moveToFirst()){

            do{

                int imageId = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Thumbnails

                        .IMAGE_ID));

                OImageBean bean  =  indexById.get(imageId);

                if(bean == null) {

                    continue;

                }

                bean.setThumb(cursor);

            }while (cursor.moveToNext());

        }

        cursor.close();

        return mImageList;

    }





    @Override

    protected void onReset() {

        super.onReset();

        //要重置的时候,停止加载

        onStopLoading();



        if(mImageList != null){//释放之前的资源

            onReleaseResource(mImageList);

            mImageList = null;

        }

    }



    @Override

    protected void onStopLoading() {

        super.onStopLoading();

        cancelLoad();

    }





    @Override

    public void onCanceled(List<OImageBean> data) {

        super.onCanceled(data);

        onReleaseResource(data);

    }



    @Override

    public void deliverResult(List<OImageBean> data) {

        if(isReset()){//如果已经关闭了,我们不需要结果

            if(data != null) onReleaseResource(data);

        }



        List<OImageBean> oldList = mImageList;

        mImageList = data;



        //如果是刚开始,我们立刻传递结果给人家

        if(isStarted()){

            super.deliverResult(data);

        }



        if(oldList != null){

            onReleaseResource(oldList);

        }



    }



    @Override

    protected void onStartLoading() {

        //如果mImageList 不是空,就立刻发放数据

        if(mImageList != null) deliverResult(mImageList);



        //如果数据有改变过,立刻加载数据

        if(takeContentChanged() || mImageList == null){

            forceLoad();

        }



    }



    private void onReleaseResource(List<OImageBean> mList) {

        //如果是cursor 这里需要释放资源

    }





}

更好的排版请看

http://www.evernote.com/l/AQwL7x_IOA9AbYM0LEPQJ5QvxpuVVspOzGg/

你可能感兴趣的:(loader)