2014-01-08 37 views
3

我在main_layout一個viewpager。視圖尋呼機只包含由RelativeLayout的如何動態添加圖像到包含imageview的viewpager?

包裹的ImageView的我想什麼做的是,當一個用戶點擊按鈕,就會將圖像添加到特定viewpager。

我發現這個答案在這裏計算器(我認爲這是有關我的問題),但我完全與該代碼丟失。

Dynamically Adding View to viewpager

到目前爲止,這是我的代碼:

MainActivity.Java

private PagerAdapter pAdapter; 
private ViewPager photoPager; 

... 

pAdapter = new GalleryPagerAdapter(getApplicationContext()); 
photoPager.setAdapter(pAdapter); 

GalleryPagerAdapter.java 

public class GalleryPagerAdapter extends PagerAdapter{ 

    private final Context context; 
    LayoutInflater inflater; 
    private final int[] GalImages = new int[] { 
     R.drawable.ic_launcher, 
     R.drawable.ic_launcher, 
     R.drawable.ic_launcher 
    }; 

    public GalleryPagerAdapter(Context context){ 
     this.context=context; 
    } 

    @Override 
    public int getCount() { 
     return GalImages.length; 
    } 

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

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     ImageView imageView = new ImageView(context); 
     int padding = 10; 
     imageView.setPadding(padding, padding, padding, padding); 
     imageView.setScaleType(ImageView.ScaleType.MATRIX); 
     imageView.setImageResource(GalImages[position]); 
     container.addView(imageView, 0); 
     return imageView; 
    } 

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

這是我想動態添加

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:padding="10dp" > 
    <ImageView 
     android:id="@+id/photo_thumb" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="#000000" 
     android:scaleType="fitXY" 
     android:padding="0dp" /> 
</RelativeLayout> 

而這個觀點是我的ViewPager存在的MainActivity

MainActivity.xml 
<LinearLayout 
    android:id="@+id/bannerLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.4" 
    android:layout_margin="0dip" 
    android:orientation="vertical"> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/photoPager" 
     android:layout_width="fill_parent" 
     android:layout_height="125dp" 
     android:background="#4F4E4F"/> 
</LinearLayout> 

所以我的問題是如何提高我使用,使我可以新的圖像動態地添加到我的ViewPager

PS代碼:在引用鏈接(我堅信有關我的問題),我完全失去了對這個部分:

// Create an initial view to display; must be a subclass of FrameLayout. 
FrameLayout v0 = (FrameLayout) inflater.inflate (R.layout.one_of_my_page_layouts, null); 
pagerAdapter.addView (v0, 0); 

什麼是吹氣?我應該如何初始化它?

我希望有人能告訴我。如果我聽起來像在這裏問多個問題,我很抱歉。

+0

你想從SD卡中添加圖像? –

+0

是的,用戶應該能夠從他們的電話庫中瀏覽。 – Jeremy

+0

好吧,它會有一段時間爲你開發答案。 –

回答

3

充氣器是用來創建視圖對象出你的XML佈局。

因此,如果您lauout R.layout.one_of_my_page_layouts

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:padding="10dp" > 
    <ImageView 
     android:id="@+id/photo_thumb" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="#000000" 
     android:scaleType="fitXY" 
     android:padding="0dp" /> 
</RelativeLayout> 

吹氣會做這樣的事情(只是一個例子)

ImageView iv=new ImageView(context); 
RelativeLayout rl=new RelativeLayout(context); 
rl.add(iv) 
return rl; 

只需最小的代碼,但吹氣將會把所有的佈局PARAMS爲好。

因此,如果你有一個xml,而不是像你在instantiateItem中那樣動態地創建一個視圖,你可以簡單地給它充氣並獲得一個Type視圖的對象。

所以你可以改變instantiateItem到:

public Object instantiateItem(ViewGroup container, int position) { 
     RelativeLayout v0 = (RelativeLayout) inflater.inflate (R.layout.one_of_my_page_layouts, null); 

     ImageView imageView = vo.findViewById(R.id.photo_thumb); 
     int padding = 10; 
     imageView.setPadding(padding, padding, padding, padding); 
     imageView.setScaleType(ImageView.ScaleType.MATRIX); 
     imageView.setImageResource(GalImages[position]); 
     container.addView(v0, 0); 
     return v0; 
    } 
+0

謝謝vipul,讓我先試試你的解決方案.. – Jeremy