从url下载图像到viewpager

时间:2017-03-01 11:40:08

标签: android image url android-viewpager

我有一组图像url在字符串数组中,现在我想在视图寻呼机中显示它们,我试图显示文件夹中的图像并且它工作。但是当我使用字符串数组时,图像不显示。所以有什么问题?这是我的代码的一部分。

 ViewPager viewPager;
CustomerAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String [] urls={"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/pia20645_main.jpg?itok=dLn7SngD","http://www.gettyimages.pt/gi-resources/images/Homepage/Hero/PT/PT_hero_42_153645159.jpg","http://humminglove.com/wp-content/uploads/2013/08/l-is-l.jpg"};

    viewPager = (ViewPager)findViewById(R.id.view_pager);
    adapter = new CustomerAdapter(this,urls);
    viewPager.setAdapter(adapter);


}

是适配器

public class CustomerAdapter extends PagerAdapter{
private int[] images = {R.mipmap.img1,R.mipmap.img2,R.mipmap.img3,R.mipmap.img4};
private Context ctx;
private  String[] urls;
private LayoutInflater inflater;

public CustomerAdapter(Context ctx,String []urls){
    this.ctx = ctx;
    this.urls=urls;
}
@Override
public int getCount() {
    return urls.length;
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    inflater =  (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.swip,container,false);
    ImageView img =(ImageView)v.findViewById(R.id.imagee);
    TextView tv  = (TextView)v.findViewById(R.id.textView);
    Picasso.with(ctx).load(urls[position]).into(img);
    tv.setText("Image :"+position);
    container.addView(v);
    return v;
}

@Override
public void destroyItem(View container, int position, Object object) {
    container.refreshDrawableState();
}

}

这是我的main_activity xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="web.blu_ray91111.example.commyc.newwww.MainActivity">


<android.support.v4.view.ViewPager
    android:id ="@+id/view_pager"
    android:layout_height="300dp"

    android:layout_width="match_parent"

    >
</android.support.v4.view.ViewPager>

swip.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:text="Hello world!"
    android:id="@+id/textView"
    android:textStyle="bold"
    android:layout_marginTop="30dp"
    android:gravity="center" />

<ImageView
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imagee"
    />

1 个答案:

答案 0 :(得分:0)

在你的情况下,不清楚它为什么没有加载。 1)加载大图像可能需要一些时间 2)加载图像时发生了一些网络错误

所以我是你使用占位符来掩盖错误和加载图像的时间。

Picasso.with(mContext).load(fileImage)
                .placeholder(R.drawable.placeholder_img)
                .error(R.drawable.error_placeholder_img).into(img)
  

检查您是否可以通过普通浏览器访问该网址