6

Tôi muốn đặt hình ảnh trong mỗi trang của ViewPager của tôi (như một cuốn sách). Những hình ảnh xuất thân từ một danh sách các url:Imageview và PagerAdapter

My Adaptor trông như thế này:

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) {} 

} 

và tôi lấy những hình ảnh nhờ vào một 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ều là nó doesn' t làm việc cả.

Trả lời

2

Tôi xin lỗi nó là một sai lầm nhưng wow tôi thậm chí không thể diễn tả -> tôi không đưa Permission:

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

và tôi đặt

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

int các onPostExecute vì tôi đã không wan Các hình ảnh trở nên lạ lùng.

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

Tôi không nghĩ bạn phải làm điều đó trong vòng lặp. Bạn có lẽ có thể xem mẫu trong thư viện tương thích. Tôi nghĩ rằng nó sẽ tự động khởi tạo một mục cho một vị trí. Dưới đây là một mẫu tôi tìm thấy sau khi googling-

@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

Tôi biết ví dụ này bởi vì đó là ví dụ tôi thay thế bằng. Bây giờ tôi sử dụng một inflater bố trí để thổi phồng một cái nhìn tùy chỉnh, nhưng nó vẫn không hoạt động. – Tsunaze

2

Tôi chưa bao giờ sử dụng một PagerAdapter trước nhưng tôi nghi ngờ bạn có thể trở lại một cái nhìn trong instantiateItem và sau đó chỉ sau khi tải về tài nguyên (trong AsyncTask). Khi AsyncTask đặt hình nền, tôi nghĩ rằng nó quá muộn, adapter đã trở lại quan điểm ...

Bạn có lẽ sẽ cần phải vô hiệu hóa cái nhìn tại một số điểm ...

+0

okay, đó là lý do tại sao, tôi để lại một Toast trong onPostExecute và nó xuất hiện. Nhưng làm cách nào tôi có thể vô hiệu hóa chế độ xem? – Tsunaze

+0

Chỉ cần với ['invalidate()'] (http://developer.android.com/reference/android/view/View.html#invalidate()). – rds

+0

tôi đã cố gắng để vô hiệu hóa xem nhưng nó đã không làm việc ở tất cả, tôi đã cố gắng để invalidateDrawable trong Asynctask, nó đã không làm việc hoặc. – Tsunaze

Các vấn đề liên quan