实现ViewPager以显示来自服务器的图像(Implementing ViewPager for displaying the images that comes from server)

254 IT屋

I want to implement viewpager. In my view pager i need to show the images that comes from the Rest API. Please give me necessary suggestions and advice so that i can be successful in that.

解决方案

First you need a custom viewpager adapter : Picasso is a great library to load images from. I will give you a link in case you need further help understanding it : http://square.github.io/picasso/

public class ViewPagerAdapter extends PagerAdapter {
Context c;
private List<String> _imagePaths;
private LayoutInflater inflater;

public ViewPagerAdapter(Context c, List<String> imagePaths) {
    this._imagePaths = imagePaths;
    this.c = c;
}

@Override
public int getCount() {
    return this._imagePaths.size();
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imgDisplay;

    inflater = (LayoutInflater) c
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.pager_item, container,
            false);

    imgDisplay = (ImageView) viewLayout.findViewById(R.id.image);

    Picasso.with(c).load(_imagePaths.get(position)).into(imgDisplay);
    (container).addView(viewLayout);

    return viewLayout;
}

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

}
}

This is the pager_item.xml :

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

From your activity:

After fetching the list urls from Rest : This is what you do :

List<String> urls;

public class MainPage extends Activity {
 public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.main_page);
urls= new ArrayList<>();
    pager = (ViewPager) findViewById(R.id.pager);
urls.add("www.image1.com");
urls.add("www.image2.com");
        pager.setAdapter(new ViewPagerAdapter(getApplicationContext(), urls));

}
}

我想实现viewpager。在我的视图寻呼机中,我需要显示来自Rest API的图像。请给我必要的建议,以使我能够成功。


解决方案

首先,您需要一个自定义的viewpager适配器:
毕加索是一个从中加载图像的好库。如果您需要进一步的帮助来理解它,我会给您一个链接: http://square.github.io/picasso /



 公共类ViewPagerAdapter扩展了PagerAdapter {
上下文c;
private List< String> _imagePaths;
专用LayoutInflater充气机;

public ViewPagerAdapter(Context c,List< String> imagePaths){
this._imagePaths = imagePaths;
this.c = c;
}

@Override
public int getCount(){
返回this._imagePaths.size();
}

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

@Override
public Object InstantiateItem(ViewGroup container,int position){
ImageView imgDisplay;

inflater =(LayoutInflater)c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.pager_item,container,
false);

imgDisplay =(ImageView)viewLayout.findViewById(R.id.image);

Picasso.with(c).load(_imagePaths.get(position))。into(imgDisplay);
(容器).addView(viewLayout);

返回viewLayout;
}

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

}
}


这是pager_item.xml:



 <?xml version = 1.0 encoding = utf-8?> 
< RelativeLayout xmlns:android = http://schemas.android.com/apk/res/android
android:layout_width = match_parent
android:layout_height = match_parent
android:orientation = vertical>

< ImageView
android:id = @ + id / image
android:layout_centerInParent = true
android:layout_width = match_parent
android:layout_height = match_parent />
< / RelativeLayout>


来自您的活动:



获取后来自Rest的列表URL:这就是您要做的:



  List< String>网址; 

公共类MainPage扩展了活动{
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.main_page);
urls = new ArrayList<>();
pager =(ViewPager)findViewById(R.id.pager);
urls.add( www.image1.com);
urls.add( www.image2.com);
pager.setAdapter(new ViewPagerAdapter(getApplicationContext(),urls));

}
}

本文地址:IT屋 » 实现ViewPager以显示来自服务器的图像