myoutputpage我在viewpager中有一个疑问,在这里我使用Viewpager,我从API网络服务动态调用图像,我无法在Activity中查看图像。这是我查看寻呼机的习惯用户我有一个像这样的API String图像:"http://"xxx"/xxx/images/apple.png"
,抱歉我的英文不好,我是android学习者的初学者。
public class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
String[] mResources = {"", ""};
List<String> list;
public CustomPagerAdapter(Context context, List<String> images) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list = images;
Log.i("listlist", "" + list);
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
for (int i = 0; i < list.size(); i++) {
// imageView.setImageResource(Integer.parseInt(list.get(i)));
try {
i = Integer.parseInt(list.get(i));
imageView.setImageResource(i);
} catch (NumberFormatException nfe) {
// Handle the condition when str is not a number.
Log.i("nummmberfromae", "" + nfe);
}
}
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((LinearLayout) object);
}
}
答案 0 :(得分:0)
找到解决方案:
添加此app / build.gradle
dependencies {
compile 'com.squareup.picasso:picasso:2.5.2'
}
这样调用以从网址
加载图片Picasso.with(mContext ).load(list.get(i)).into(imageView);
答案 1 :(得分:0)
for (int i = 0; i < list.size(); i++) {
// imageView.setImageResource(Integer.parseInt(list.get(i)));
try {
i = Integer.parseInt(list.get(i));
imageView.setImageResource(i);
} catch (NumberFormatException nfe) {
// Handle the condition when str is not a number.
Log.i("nummmberfromae", "" + nfe);
}
}
为什么在实例化方法中需要循环?此方法本身会调用列表中的每个项目。使用for循环设置最后一个位置意味着将列表的最后一个图像放入ViewPager的每个图像中。
它应该是这样的:
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
try {
String imageURL = list.get(position);
Picasso.with(mContext).load(imageURL).into(imageView);
} catch (NumberFormatException nfe) {
// Handle the condition when str is not a number.
Log.i("nummmberfromae", "" + nfe);
}
container.addView(itemView);
return itemView;
}
在build.gradle文件中添加依赖项(模块级别)
dependencies {
compile 'com.squareup.picasso:picasso:2.5.2'
}
您可以使用任何第三方库从URL加载图片。你可以使用Volley,Glide或Picasso。