简体   繁体   English

Android的懒加载画廊问题

[英]android lazy load gallery issue

im trying to asyncronously download some images in an asynctask and then in the onPostExecute() set the images to a gallery adapter. 我试图异步地在asynctask中下载一些图像,然后在onPostExecute()中将图像设置为图库适配器。 I have hard coded the images to be donwloaded in the asyntask since I cant even get to the part where i will be passing the images to the constructor of the adapter :/ No errors are thrown and the postexecute Log() doesnt post anything to logcat 我已经硬编码了要在asyntask中下载的图像,因为我什至无法到达将图像传递给适配器的构造函数的部分:/没有引发错误,并且postexecute Log()不会将任何内容发布到logcat

Can anybody tell me where I am going wrong please? 有人可以告诉我我要去哪里错吗?

public class GallerythreadedtestActivity extends Activity { 公共类GallerythreadedtestActivity扩展了Activity {

private Gallery gallery;
private AddImgAdp adapter;
private Drawable[] image;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new downloadImages().execute();
}

public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    // Adding images.
    private Integer[] Imgid = {
    R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon
    };

    public AddImgAdp(Context c) {
    cont = c;
    TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
    GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
    typArray.recycle();
    }

    public int getCount() {
    return Imgid.length;
    }

    public Object getItem(int position) {
    return position;
    }

    public long getItemId(int position) {
    return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imgView = new ImageView(cont);

    imgView.setImageDrawable(image[position]);
    // Fixing width & height for image to display
    imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(GalItemBg);

    return imgView;
    }
    }

public class downloadImages extends AsyncTask<String, Void, Void> {

    protected void onPreExecute(){
        image=new Drawable[2];
        adapter = new AddImgAdp(getApplicationContext());
        gallery = (Gallery) findViewById(R.id.examplegallery);
    }

    protected void onProgressUpdate(Void... v){

    }

    protected Void doInBackground(final String... args) {


        image[0] = ImageOperations(getApplicationContext(),"http://t3.gstatic.com/images?q=tbn:ANd9GcQidl6KX2jRWNeCA6jT_TjWG7NlI3aRiB_AcDsA9Y5owS2cr9G6","image.jpg");
        image[1] = ImageOperations(getApplicationContext(),"http://t3.gstatic.com/images?q=tbn:ANd9GcQidl6KX2jRWNeCA6jT_TjWG7NlI3aRiB_AcDsA9Y5owS2cr9G6","image.jpg");

        return null;

    }

    protected void onPostExecute(){
        Log.d("onpost","");
        gallery.setAdapter(adapter);
        gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
            Toast.makeText(GallerythreadedtestActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show();
            }
            });
    }

}

private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
    try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}

} }

I can give you a suggestion please try this also as an supplementary solution, its better lazy load the images: Lazy load of images in ListView . 我可以给您一个建议,也可以尝试作为补充解决方案,它更好地延迟加载图像: ListView中的图像延迟加载 In that if you change the list view element in the xml to Gallery view, I think that will solve your issue. 如果您将xml中的list view元素更改为Gallery视图,那么我认为这将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM