使用图库在视图之间滑动

发布于 2024-09-10 13:17:58 字数 2285 浏览 5 评论 0原文

我正在关注 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-09-17 13:17:58

这是因为 a) 这不是获取 ScrollView 的方式,b) 不是实现 getView 的方式。您看到该异常是因为 getLayout 返回的是 XmlResourceParser 而不是 ScrollView。要执行您想要的操作,您需要:

在适配器的构造函数中,您需要:

inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

这将为您提供对布局充气器的引用。

在您的 getView 中:

View view = inflater.inflate(R.layout.main, parent, false);
return view;

在您的layout_main 中,您应该将所有内容包装在 ScrollView 中,因为整个布局正在膨胀。

That's because a) that's not how you get the ScrollView and b) not the way you should implement getView. You're seeing that exeception because getLayout is returning an XmlResourceParser not a ScrollView. To do what you want you need:

In your adapter's constructor you need:

inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

That will give you a reference to the layout inflater.

In your getView:

View view = inflater.inflate(R.layout.main, parent, false);
return view;

In your layout_main you should wrap everything in a ScrollView since the entire layout is being inflated.

十年不长 2024-09-17 13:17:58

我看来和你的布局有关系。如果您只加载一个空活动并将布局设置为该活动,会发生什么?你的布局是什么样的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文