Android的刷卡水平视图分页与图形页面 [英] android swipe Horizontal View Paging with MapView

查看:172
本文介绍了Android的刷卡水平视图分页与图形页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3页其间用户可以通过滑动手指(向左或向右 - 水平视图分页)导航视图中的一个Android应用程序。我下载了android.support.v4.view.ViewPager,发现实行刷卡部分主要是由于本教程来这里的路上:<一href=\"http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/\" rel=\"nofollow\">http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/

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/

我的问题是,我想我的观点之一,推出了MapView类。但推出的MapView我需要一个扩展MapActivity的活动。如果我有一个包含所有3次一MapActivity我所有的RecordAdapter类,那么我有一个问题,因为每个MapActivity只能有一个图形页面分配给它。所以,如果我刷卡回另一页,然后回到地图页面项目的崩溃。我有它在code的方法是:

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;  
            }

在哪里RECORD3包含我的MapView。从实验崩溃时,我就又回到RECORD1再到RECORD2再次发生。它似乎启动一个新的图形页面时,它进入RECORD2(?)!这一定是这种情况,因为奇怪的是,如果我在中间的MapView:

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;  
            }

它的工作原理OK!但是,这不是我想要的(也许我会在将来需要4rth页面太)。

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?

编辑:
我找到了答案。我需要添加
myPager.setOffscreenPageLimit(3);
当我把我的ViewPager从我的code。这样的观点保持活力!

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!

推荐答案

我猜测你的崩溃是由多个实例mapviews一个IllegalStateException?

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

您是正确的,的MapView从第二视图启动时,viewpager prepares对任何一方的意见,使其能顺利当用户想要改变标签过渡。它通过实际加载它们做到这一点。 (使用hierarchyviewer工具在Android SDK中的某个时候检查了这一点)。

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).

不管怎么说,尝试使用这种方法的onDestroy:

Anyways, try using this onDestroy method:

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

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

这应该删除每当视图被破坏的MapView,使之可以再利用。

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

如果失败,请尝试也让你的MapView静态的:

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.

这篇关于Android的刷卡水平视图分页与图形页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆