1

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

    //--- 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);
}
}
4

1 回答 1

0

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

检查这个例子。您可以在那里下载完整的应用程序示例。

于 2013-10-23T17:50:58.203 回答