在Android中,从URL填充简单的ViewPager Gallery

时间:2013-10-23 17:42:07

标签: android android-viewpager

我使用ViewPager创建了一个简单的图库。现在,它正在抓取drawables文件夹中的图像。我想让ViewPager加载来自URL的图像。故障来自setImageResource的Adapter类。它不接受字符串变量。那么如何调整代码以便它可以接受字符串数组呢?我的代码:

    //--- Main Activity Class:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;

public class WCBCGallery51 extends Activity {
Button back_BTN;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery5_main);
    back_BTN = (Button) findViewById(R.id.back_gal5_btn);


    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    WCBCGallery5Adapter adapter = new WCBCGallery5Adapter(this);
    viewPager.setAdapter(adapter);



}//--- END onCreate

@Override
protected void onPause() {
    super.onPause();
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    // finish();
}

}

这是适配器类:

    //--- Adapter Class
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class WCBCGallery51Adapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] { R.drawable.image01,
        R.drawable.image02, R.drawable.image03, R.drawable.image04,
        R.drawable.image25, R.drawable.image26, R.drawable.image27,
        R.drawable.image28 };

String[] GalImagesStr = {
        "http://www.website.com/images/image01.png",
        "http://www.website.com/images/image02.png",
        "http://www.website.com/images/image03.png",
        "http://www.website.com/images/image04.png",
        "http://www.website.com/images/image25.png",
        "http://www.website.com/images/image26.png",
        "http://www.website.com/images/image27.png",
        "http://www.website.com/images/image28.png" };


WCBCGallery51Adapter(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return GalImagesStr.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imageView = new ImageView(context);
    int padding = context.getResources().getDimensionPixelSize(
            R.dimen.padding_medium);
    imageView.setPadding(padding, padding, padding, padding);
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    //--- here's where the problem is:
    imageView.setImageResource(GalImages[position]);
    ((ViewPager) container).addView(imageView, 0);
    return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((ImageView) object);
}
}

1 个答案:

答案 0 :(得分:0)

您必须下载图像并向视图提供回调,以便下载完成后可以放置图像。

检查this示例。您可以在那里下载完整的应用示例。