ListFragment的onCreateView(),如何强制调用?

时间:2013-05-05 09:32:03

标签: android

我正在使用android示例应用程序FragmentPagerSupport.java来实现页面之间的幻灯片效果。每个页面都包含一个由线程加载的HTTP映像。我的片段onCreateView()正在为图像加载东西。现在我的问题是,我不知道如何将加载的图像位图绘制到onCreateView中创建的ImageView上,是否有任何想法强制要调用Fragment的onCreateView?

       @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("第 #" + mNum + " 篇");
        ImageView iv = (ImageView)v.findViewById(R.id.imageView);
        final String pic = FragmentPagerSupport.items.get(mNum).thumb;
        Log.e("tag ", "on create view on");
        MyItem mi = FragmentPagerSupport.items.get(mNum);
        if (mi.bm != null) {
            Log.e("tag", "setbitmap for " + mNum);
            iv.setImageBitmap(mi.bm);
        } else {
            new Thread(new Runnable() {
                public void run() {
                    int m = mNum;
                    final Bitmap bm = loadBitmap(pic);
                    MyItem mi = FragmentPagerSupport.items.get(m);
                    mi.bm = bm;                 
                    Log.e("tab", "finishing load bitmap for " + m);
                    // here I want the bitmap can be drawn to the ImageView which is inside the fragment.
                }
            }).start();
        }

        return v;
    }

1 个答案:

答案 0 :(得分:1)

您可以使用此AsyncTask类:

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
    this.bmImage = bmImage;
}

protected Bitmap doInBackground(Strings... args) {
    Bitmap mIcon11 = null;
                int m = Integer.parse(args[0]);
                String pic = args[1];
    try {
                final Bitmap bm = loadBitmap(pic);
                MyItem mi = FragmentPagerSupport.items.get(m);
                mi.bm = bm;                 
                Log.e("tab", "finishing load bitmap for " + m);
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return mIcon11;
}

protected void onPostExecute(Bitmap result) {
    bmImage.setImageBitmap(result);
}
}

并用作:

new DownloadImageTask(iv).execute(mNum.toString(),pic);