开发者

Using a Gallery to swipe between Views

开发者 https://www.devze.com 2023-01-07 18:16 出处:网络
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

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.Gallery开发者_如何转开发Adapter.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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消