2012-07-20 58 views
2

我已經使用Gallery和ViewPager小部件,我從Web服務獲取圖像, 但我不能將它們放在ViewPager中,但在圖庫中很容易。 我用這個教程ViewPager http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/android畫廊滾動像查看傳呼機

我想Android圖庫部件滾動像ViewPager滾動,

這是可能的,我怎麼能做到這一點。

+0

注意,[廊插件](http://developer.android.com/reference/android/widget/Gallery.html)現在已經過時。你應該使用ViewPager。如果您可以編輯您的問題併發布您在嘗試ViewPager時遇到的麻煩,我們可以嘗試幫助您以這種方式解決問題。 – FoamyGuy 2012-07-20 13:46:02

+0

@Tim我編輯我的問題,檢查,我如何動態創建視圖並添加它們,我也想要一個點擊監聽器 – 2012-07-20 13:49:54

+0

如果你只使用ViewPager它解決了這個問題,你只需要在頁面視圖中指定實際上是佈局)你想要顯示什麼,然後處理點擊事件(在特定視圖上)。 – MobileEvangelist 2012-07-20 13:54:19

回答

6

這是您的PagerAdapter的instantiateItem()方法的一個簡單示例,它將動態地用圖像視圖填充它。

@Override 
public Object instantiateItem(final View pager, final int position) 
{ 
    //Note: if you do not have a local reference to the context make one and 
    //set it to the context that gets passed in to the constructor. 
    //Another option might be to use pager.getContext() which is how its 
    //done in the tutorial that you linked. 
    ImageView mImg = new ImageView(context); 

    /*Code to dynamically set your image goes here. 
    Exactly what it will be is going to depend on 
    how your images are stored. 
    In this example it would be if the images 
    are on the SD card and have filenames 
    with incrementing numbers like: (img0.png, img1.png, img2.png etc...)*/ 

    Bitmap mBitmap = BitmapFactory.decodeFile(
     Environment.getExternalStorageDirectory() + "/img" + position + ".png"); 
    mImg.setImageBitmap(mBitmap); 




    ((ViewPager) collection).addView(mImg, 0); 
    return mImg; 

} 
+0

嗯,我告訴你,如果這對我工作 – 2012-07-21 10:42:59

+0

感謝它工作完美, – 2012-07-23 04:57:56

+0

它完美的作品只有一個問題,我得到了,我不知道爲什麼它不顯示在右側的衰落效果,它只顯示淡化效果只對左側,我讀了一些博客,很少有問題,它只顯示在左側和頂部淡出,需要重寫正確的方法(getRightEdgeFading),任何建議? – 2012-08-08 04:56:27