6

내 ViewPager의 각 페이지 (책과 같은)에 이미지를 넣고 싶습니다.Imageview 및 PagerAdapter

내 어댑터는 다음과 같습니다 : 그 이미지는 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); 
} 

것은 그것이 '아무튼이다 전혀 일하지 않는다.

답변

2

난이 돌아갈 생각 미안 그것은 실수했지만 와우도 설명 할 수없는 -> 내가 권한 넣지 않았다

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

을하고 난 완하지 않았기 때문에 나는 onPostExecute하는 int

d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicWidth()); 

를 넣어 모든 이상한 이미지.

2

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]); }

난 당신이 for 루프에서 그것을 할 필요가 있다고 생각하지 않습니다. 아마도 호환성 라이브러리에서 샘플을 볼 수 있습니다. 나는 그것이 자동적으로 위치에 대한 항목을 인스턴스화한다고 생각한다. 여기에 내가 전에 PagerAdapter을 사용한 적이

@Override 
    public Object instantiateItem(View collection, int position) { 
     TextView tv = new TextView(cxt); 
     tv.setText("Bonjour PAUG " + position); 
     tv.setTextColor(Color.WHITE); 
     tv.setTextSize(30); 

     ((ViewPager) collection).addView(tv,0); 

     return tv; 
    } 
+0

나는이 예제를 바꿀 수 있기 때문에이 예제를 알고있다. 이제 레이아웃 인플레이터를 사용하여 사용자 정의보기를 확장했지만 여전히 작동하지 않습니다. – Tsunaze

2

을 googling- 후 발견되는 샘플입니다하지만 난 당신이 instantiateItem에서보기를 반환하고 후에 만합니다 (AsyncTask에) 리소스를 다운로드 할 수 있습니다 의심한다. AsyncTask 배경 이미지를 설정하면, 나는 너무 늦게, 어댑터가 이미보기 ... 당신은 아마 어떤 점에서보기를 무효화해야합니다

...

+0

좋아, 그 이유는, 나는 onPostExecute에 토스트를 남겼다. 그러나 나는 어떻게 그 견해를 무효화 할 수 있습니까? – Tsunaze

+0

['invalidate()'] (http://developer.android.com/reference/android/view/View.html#invalidate())를 사용하면됩니다. – rds

+0

나는보기를 무효화하려했지만 전혀 작동하지 않았다. Asynctask에서 invalidateDrawable을 시도했으나 작동하지 않았다. – Tsunaze