在片段中使用asynctask loader以在gridview中显示图像时未显示动画加载程序

时间:2012-04-12 08:29:10

标签: android actionbarsherlock

我正在尝试在片段内的网格视图中显示图像,并且在异步任务加载器中的运行时下载图像,并且我在同一片段中使用基本适配器。

现在,除了加载图像但没有显示加载器动画时,一切正常。 My Fragment类包含一个BaseAdapter和AsyncLoader

package friends.appModules.mainClasses;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import friends.appModules.commonClasses.Cheeses;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class PeopleNearYou extends Fragment implements     LoaderManager.LoaderCallbacks<List<Images>>{

private GridView pnuGridView;
ImageAdapter myImageAdapter;
boolean isLoadFinished = false;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.d("Where am I?", "in onActivityCreated PeopleNearYou");

    getLoaderManager().initLoader(1, null, this);
}

/* First Method call after onActivityCreated*/
@Override
public Loader<List<Images>> onCreateLoader(int id, Bundle args) {
    Log.d("Where am I?", "in onCreateLoader PeopleNearYou");
    return new PeopleNearYouLoader(getActivity());
}


@Override
/*Fourth Method Called*/
public void onLoadFinished(Loader<List<Images>> loader,
        List<Images> data) {
    Log.d("Where am I?", "in onLoadFinished PeopleNearYou");

    final Context c = this.getActivity().getApplicationContext();
    myImageAdapter = new ImageAdapter(c);

    myImageAdapter.setData(data);

    pnuGridView.setAdapter(myImageAdapter);
}

@Override
public void onLoaderReset(Loader<List<Images>> loader) {
    Log.d("Where am I?", "in onLoaderReset PeopleNearYou");
    myImageAdapter.setData(null);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.d("Where am I?", "in onCreateView PeopleNearYou");

    View view = inflater.inflate(R.layout.people_near_you, container, false);

    pnuGridView = (GridView) view.findViewById(R.id.gridview);
    //pnuGridView.setBackgroundColor(R.color.dark_gry_txt);
    return view;
}



public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public class ImageAdapter extends BaseAdapter{
    private Context mContext;
    private List<Images> finallyListCreated;

    public ImageAdapter(Context c) {
        Log.d("Where am I?", "in ImageAdapter Constructor PeopleNearYou");
        mContext = c;

        pnuGridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(mContext, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public int getCount() {
        Log.d("Where am I?", "in getCount PeopleNearYou");
        return 20;
    }

    public Object getItem(int position) {
        Log.d("Where am I?", "in getItem PeopleNearYou");
        return null;
    }

    public long getItemId(int position) {
        Log.d("Where am I?", "in getItemId PeopleNearYou");
        return 0;
    }

    public void setData(List<Images> data) {
        Log.d("Where am I ??", "in onSetData() PeopleNearYou");
        if (data != null) {
            finallyListCreated = data;
        }
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d("Where am I?", "in getView PeopleNearYou");
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
            imageView.setBackgroundColor(R.color.dark_gry_txt);
        } else {
            imageView = (ImageView) convertView;
        }

        Images item = finallyListCreated.get(position);
        imageView.setImageDrawable(item.getImageUrl());
        return imageView;
    }
}

public static class PeopleNearYouLoader extends AsyncTaskLoader<List<Images>>{

    Context loaderContext;
    List<Images> loaderImageList;

    public PeopleNearYouLoader(Context context) {
        super(context);
        Log.d("Where am I?", "in PeopleNearYouLoader Constructor PeopleNearYou");
        loaderContext = context;
    }

    @Override
    /*Third method called if there are no images in the list else Deliver Result method will be called*/
    public List<Images> loadInBackground() 
    {
        Log.d("Where am I ??", "in loadInBackground() PeopleNearYou");

        List<Images> peopleNearYouImagesList = new ArrayList<Images>(20);

        for (int i=0; i<20; i++) {
            Log.d("Where am I ??", "For loop to set Image objects PeopleNearYou");
            Images peopleNearYouImages = new Images(loadImageFromUrl(Cheeses.imageUrls[i]));
            peopleNearYouImagesList.add(peopleNearYouImages);
        }

        return peopleNearYouImagesList;
    }

    @Override
    public void onCanceled(List<Images> data) {
        super.onCanceled(data);
        onReleaseResources(data);
    }

    @Override
    public void deliverResult(List<Images> images) {
        if (isReset()) {
            if (images != null) {
                onReleaseResources(images);
            }
        }

        List<Images> oldImages = images;
        loaderImageList = images;

        if (isStarted()) {
            super.deliverResult(images);
            Log.d("Where am I ??", "in isStarted = true PeopleNearYou");
        }

        if (oldImages != null) {
            onReleaseResources(oldImages);
            Log.d("Where am I ??", "in oldImages != null PeopleNearYou");
        }
    }

    @Override
    /* Second method called*/
    protected void onStartLoading() {
        if (loaderImageList != null) {
            deliverResult(loaderImageList);
            Log.d("Where am I ??", "in onStartLoading() and list is not null");
        }
        else {
            Log.d("Where am I ??", "in onStartLoading() and list is null");
            forceLoad();
        }
    }

    @Override
    protected void onStopLoading() {
        cancelLoad();
        Log.d("Where am I ??", "in onStopLoading() PeopleNearYou");
    }

    @Override
    protected void onReset() {
        super.onReset();

        onStopLoading();

        if (loaderImageList != null) {
            onReleaseResources(loaderImageList);
            loaderImageList = null;
        }
    }

    protected void onReleaseResources(List<Images> data) {
        Log.d("Where am I ??", "in onReleaseResources() PeopleNearYou");
    }

    public Drawable loadImageFromUrl(String url) {
        InputStream inputStream;
        try {
            inputStream = new URL(url).openStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Drawable.createFromStream(inputStream, "src");
    }

}
}

和我的gridview布局是:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

我的代码中使用的图像类也是:

package friends.appModules.mainClasses;

import android.app.Application;
import android.graphics.drawable.Drawable;

public class Images extends Application{
    private Drawable imageUrl;

    public Images(Drawable imageUrl) {
        this.imageUrl = imageUrl;
    }
    public Drawable getImageUrl() {
        return imageUrl;
    }
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

查看ListFragment.setListShown()的源代码。它完全符合您对GridView的要求。