android swipe Horizontal View Paging with MapView

2019-05-13 17:22发布

问题:

I have an android app with a view that contains 3 pages between which the user can navigate through swiping his finger (to the left or to the right - Horizontal View Paging). I downloaded “android.support.v4.view.ViewPager” and found the way to implement the swiping part mostly thanks to this tutorial here: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/

My problem is that I want one of my views to launch a MapView. But to launch a MapView I need an activity that extends the MapActivity. If I have all my “RecordAdapter” class in a MapActivity that contains all the 3 views then I have a problem because each MapActivity can have only one MapView assigned to it. So if I swipe back to another page and then back to the map page the project crashes. The way I have it in the code is:

            switch (position) {
            case 0:
                resId = R.layout.record1;
                break;
            case 1:
                resId = R.layout.record2;
                break;
            case 2:
                resId = R.layout.record3;                   
                break;  
            }

Where “record3” contains my MapView. From experimenting crashing happens when I go back to “record1” and then to “record2” again. It seems that it launches a new MapView when it goes to record2 (?)! This must be the case because strangely enough if I have the MapView in the middle:

            switch (position) {
            case 0:
                resId = R.layout.record1;
                break;
            case 1:
                resId = R.layout.record3;
                break;
            case 2:
                resId = R.layout.record2;                   
                break;  
            }

it works ok! But this is not what I want (maybe I’ll need a 4rth page in the future too).

I hope the way I wrote it is not too complicated..

So what should I do? Any ideas?

EDIT: I found an answer to this. I needed to add "myPager.setOffscreenPageLimit(3);" when I call my ViewPager from my code. This way the views are kept alive!

回答1:

I am guessing that your crashing is an IllegalStateException caused by instantiating multiple mapviews?

You are correct that the mapview is launching from the second view, the viewpager prepares the views on either side so that it can transition smoothly when the user wants to change tabs. It does this by actually loading them. (Use the hierarchyviewer tool in your android sdk to check this out sometime).

Anyways, try using this onDestroy method:

@Override
public void onDestroy() {
    super.onDestroy();

    ((ViewGroup) map.getParent()).removeView(map);
}

It should remove the mapview whenever the view is destroyed, making it ready for re-use.

If that fails, try also making your mapview static:

private static MapView mapview;

then, if multiple instances of your map tab are somehow created, they can use the same mapview.