使用图库在视图之间滑动
我正在关注 Android Gallery 教程 (http://developer.android. com/guide/tutorials/views/hello-gallery.html),但我希望能够水平移动到一个完整的新屏幕,例如 LinearLayout,而不是简单的图像。这个想法是一种选项卡行为,但用户可以滑动屏幕。
我创建了一个画廊,创建了一个从 BaseAdapter 扩展的 GalleryAdapter,并在 getItem() 方法中尝试返回复杂视图:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*ImageView i = new ImageView(this.c);
i.setImageResource(R.drawable.login);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
i.setScaleType(ImageView.ScaleType.FIT_XY);
return i;*/
ScrollView view = (ScrollView)this.c.getResources().getLayout(R.layout.main);
return view;
}
在上面的示例中,如果我使用 ImageView 它将工作,但一旦我使用更多视图,它就会失败复杂的视图,如从资源加载的滚动视图。
知道为什么吗?例外是:
07-15 13:47:36.498: ERROR/AndroidRuntime(446): java.lang.ClassCastException: android.content.res.XmlBlock$Parser
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at de.flavor.myviews.GalleryAdapter.getView(GalleryAdapter.java:44)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.View.measure(View.java:8171)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.View.measure(View.java:8171)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.View.measure(View.java:8171)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
谢谢 斯文
I am following the Android Gallery tutorial (http://developer.android.com/guide/tutorials/views/hello-gallery.html) but instead of simple images I'd like to be able to horizontally move to a complete new screen, e.g. a LinearLayout. The idea is a kind of tabbing behaviour but the user can swipe though the screens.
I created a gallery, created a GalleryAdapter that extends from BaseAdapter and in the getItem() method I try to return the complex view:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*ImageView i = new ImageView(this.c);
i.setImageResource(R.drawable.login);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
i.setScaleType(ImageView.ScaleType.FIT_XY);
return i;*/
ScrollView view = (ScrollView)this.c.getResources().getLayout(R.layout.main);
return view;
}
In the above example, it will work if I am using an ImageView, but it fails once I use a more complex view like a scrollView loaded from resources.
Any idea why? The exception is:
07-15 13:47:36.498: ERROR/AndroidRuntime(446): java.lang.ClassCastException: android.content.res.XmlBlock$Parser
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at de.flavor.myviews.GalleryAdapter.getView(GalleryAdapter.java:44)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.View.measure(View.java:8171)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.View.measure(View.java:8171)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.View.measure(View.java:8171)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
07-15 13:47:36.498: ERROR/AndroidRuntime(446): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
Thanx
Sven
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为 a) 这不是获取
ScrollView
的方式,b) 不是实现getView
的方式。您看到该异常是因为getLayout
返回的是XmlResourceParser
而不是ScrollView
。要执行您想要的操作,您需要:在适配器的构造函数中,您需要:
这将为您提供对布局充气器的引用。
在您的 getView 中:
在您的layout_main 中,您应该将所有内容包装在 ScrollView 中,因为整个布局正在膨胀。
That's because a) that's not how you get the
ScrollView
and b) not the way you should implementgetView
. You're seeing that exeception becausegetLayout
is returning anXmlResourceParser
not aScrollView
. To do what you want you need:In your adapter's constructor you need:
That will give you a reference to the layout inflater.
In your
getView
:In your layout_main you should wrap everything in a
ScrollView
since the entire layout is being inflated.我看来和你的布局有关系。如果您只加载一个空活动并将布局设置为该活动,会发生什么?你的布局是什么样的。
I looks like it has something to do with your layout. What happens if you just load an empty activity and set the layout to that? What does your layout look like.