Imageview 和 PagerAdapter
我想将图像放在 ViewPager 的每个页面中(就像一本书)。这些图像来自 url 列表:
我的适配器看起来像这样:
private class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
/**
* Create the page for the given position. The adapter is responsible
* for adding the view to the container given here, although it only
* must ensure this is done by the time it returns from
* {@link #finishUpdate()}.
*
* @param container The containing View in which the page will be shown.
* @param position The page position to be instantiated.
* @return Returns an Object representing the new page. This does not
* need to be a View, but can be some other container of the page.
*/
@Override
public Object instantiateItem(View collection, int position) {
// Create Views
ScrollView view = new ScrollView(cxt);
RelativeLayout container = new RelativeLayout(cxt);
TextView text = new TextView(cxt);
text.setId(1);
ImageView[] image = new ImageView[18];
// Parameters
LayoutParams container_params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LayoutParams text_params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams content_params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
content_params.addRule(RelativeLayout.BELOW, text.getId());
view.setLayoutParams(container_params);
container.setLayoutParams(container_params);
text.setLayoutParams(text_params);
//image.setLayoutParams(content_params);
// set
for(int i = 0; i < position; i++){
image[i] = new ImageView(cxt);
image[i].setLayoutParams(content_params);
createimage(image[i], list_url.get(position));
container.addView(image[i]);
}
text.setText(list_url.get(position).toString());
// add
view.addView(container);
container.addView(text);
//container.addView(image);
((ViewPager) collection).addView(view,0);
return view;
}
/**
* Remove a page for the given position. The adapter is responsible
* for removing the view from its container, although it only must ensure
* this is done by the time it returns from {@link #finishUpdate()}.
*
* @param container The containing View from which the page will be removed.
* @param position The page position to be removed.
* @param object The same object that was returned by
* {@link #instantiateItem(View, int)}.
*/
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((ScrollView) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((ScrollView)object);
}
/**
* Called when the a change in the shown pages has been completed. At this
* point you must ensure that all of the pages have actually been added or
* removed from the container as appropriate.
* @param container The containing View which is displaying this adapter's
* page views.
*/
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}
我通过 asynctask 获取这些图像:
private class CreateImage extends AsyncTask<String, Void, Drawable> {
ImageView image;
public CreateImage(ImageView img) {
image = img;
}
protected void onPreExecute() {
}
protected Drawable doInBackground(String... urls) {
InputStream is;
Drawable d = null ;
try {
is = (InputStream)new URL(urls[0]).getContent();
d = Drawable.createFromStream(is, "Image");
return d;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return d;
}
protected void onPostExecute(Drawable d) {
image.setBackgroundDrawable(d);
}
private Drawable ImageOperations(Context ctx, String url) {
try {
URL imageUrl = new URL(url);
InputStream is = (InputStream) imageUrl.getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
public void createimage(ImageView img, String url){
new CreateImage(img).execute(url);
}
问题是它根本不起作用。
I want to put images in each pages of my ViewPager (like a book). Those images came from a list of url :
My Adapter looks like this :
private class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
/**
* Create the page for the given position. The adapter is responsible
* for adding the view to the container given here, although it only
* must ensure this is done by the time it returns from
* {@link #finishUpdate()}.
*
* @param container The containing View in which the page will be shown.
* @param position The page position to be instantiated.
* @return Returns an Object representing the new page. This does not
* need to be a View, but can be some other container of the page.
*/
@Override
public Object instantiateItem(View collection, int position) {
// Create Views
ScrollView view = new ScrollView(cxt);
RelativeLayout container = new RelativeLayout(cxt);
TextView text = new TextView(cxt);
text.setId(1);
ImageView[] image = new ImageView[18];
// Parameters
LayoutParams container_params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LayoutParams text_params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams content_params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
content_params.addRule(RelativeLayout.BELOW, text.getId());
view.setLayoutParams(container_params);
container.setLayoutParams(container_params);
text.setLayoutParams(text_params);
//image.setLayoutParams(content_params);
// set
for(int i = 0; i < position; i++){
image[i] = new ImageView(cxt);
image[i].setLayoutParams(content_params);
createimage(image[i], list_url.get(position));
container.addView(image[i]);
}
text.setText(list_url.get(position).toString());
// add
view.addView(container);
container.addView(text);
//container.addView(image);
((ViewPager) collection).addView(view,0);
return view;
}
/**
* Remove a page for the given position. The adapter is responsible
* for removing the view from its container, although it only must ensure
* this is done by the time it returns from {@link #finishUpdate()}.
*
* @param container The containing View from which the page will be removed.
* @param position The page position to be removed.
* @param object The same object that was returned by
* {@link #instantiateItem(View, int)}.
*/
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((ScrollView) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((ScrollView)object);
}
/**
* Called when the a change in the shown pages has been completed. At this
* point you must ensure that all of the pages have actually been added or
* removed from the container as appropriate.
* @param container The containing View which is displaying this adapter's
* page views.
*/
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}
and i take those images thanks to a asynctask :
private class CreateImage extends AsyncTask<String, Void, Drawable> {
ImageView image;
public CreateImage(ImageView img) {
image = img;
}
protected void onPreExecute() {
}
protected Drawable doInBackground(String... urls) {
InputStream is;
Drawable d = null ;
try {
is = (InputStream)new URL(urls[0]).getContent();
d = Drawable.createFromStream(is, "Image");
return d;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return d;
}
protected void onPostExecute(Drawable d) {
image.setBackgroundDrawable(d);
}
private Drawable ImageOperations(Context ctx, String url) {
try {
URL imageUrl = new URL(url);
InputStream is = (InputStream) imageUrl.getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
public void createimage(ImageView img, String url){
new CreateImage(img).execute(url);
}
the thing is that it doesn't work at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
for(int i = 0; i < 位置; i++){
图像[i] = 新的ImageView(cxt);
image[i].setLayoutParams(content_params);
createimage(image[i], list_url.get(position));
容器.addView(图像[i]);
我认为
您不必在 for 循环中执行此操作。您也许可以查看兼容性库中的示例。我认为它会自动实例化一个职位的项目。这是我在谷歌搜索后找到的一个样本-
for(int i = 0; i < position; i++){
image[i] = new ImageView(cxt);
image[i].setLayoutParams(content_params);
createimage(image[i], list_url.get(position));
container.addView(image[i]);
}
I don't think you have to do it in a for loop. You can perhaps look at the sample in compatibility library. I think it automatically instantiate an item for a position. Here is an sample I found after googling-
我以前从未使用过 PagerAdapter,但我怀疑您可以在
instantiateItem
中返回视图,然后仅在下载资源后(在AsyncTask
中)返回视图。当AsyncTask
设置背景图像时,我认为为时已晚,适配器已经返回了视图...您可能需要在某个时候使视图无效...
I have never used a
PagerAdapter
before but I doubt you can return a view ininstantiateItem
and then only after download the resource (in theAsyncTask
). When theAsyncTask
sets the background image, I think it's too late, the adapter has already returned the view...You'll probably need to invalidate the view at some point...
很抱歉这是一个错误,但哇我什至无法描述 ->我没有添加 Permission :
并且添加了
onPostExecute 因为我不希望图像变得很奇怪。
I'm sorry it was a mistake but wow i can't even describe -> I didn't put the Permission :
and i put
int the onPostExecute because i didn't want the images to be all weird.