2012-07-20 64 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