Google Maps Android API V2 blacking out part of layout

前端 未结 8 1408
-上瘾入骨i
-上瘾入骨i 2021-02-04 13:48

I\'m trying to integrate Google Maps Android API v2 into my Android Application. I\'m placing the Google Map in the middle of my layout. It works great when the layout is able t

相关标签:
8条回答
  • 2021-02-04 14:27

    I was able to work around the issue with a hack. I extended ScrollView so I could get notified on scroll events. I used the ObservableScrollView implementation from Synchronise ScrollView scroll positions - android.

    I added to my onCreate():

    ObservableScrollView sv = (ObservableScrollView) findViewById(R.id.scroll);
    sv.setScrollViewListener(this);
    

    Then, when I receive a scroll changed event, I toggle the visibility of the whole view, which forces a re-layout, which prevents the black box from appearing.

    public void onScrollChanged(ObservableScrollView sv, int x, int y, int oldx, int oldy) {
        sv.setVisibility(View.GONE);
        sv.setVisibility(View.VISIBLE);
    }
    

    I tried to invalidate() the map view, and toggling the visibility of just the map view, but neither worked. Thankfully toggling the visibility of the whole view doesn't cause any flicker or other strange behavior.

    There probably is a better solution, but at this point, this is the only thing I've found that seems to work.

    0 讨论(0)
  • 2021-02-04 14:29

    My workaround: make use of the new snapshot interface from GoogleMap and display a snapshot of the map while scrolling the page.

    Here is my code to setting the snapshot (it's in the same fragment as map, called FragmentMap.java):

    public void setSnapshot(int visibility) {
        switch(visibility) {
        case View.GONE:
            if(mapFragment.getView().getVisibility() == View.VISIBLE) {
                getMap().snapshot(new SnapshotReadyCallback() {
                    @Override
                    public void onSnapshotReady(Bitmap arg0) {
                        iv.setImageBitmap(arg0);
                    }
                });
                iv.setVisibility(View.VISIBLE);
                mapFragment.getView().setVisibility(View.GONE);
            }
            break;
        case View.VISIBLE:
            if(mapFragment.getView().getVisibility() == View.GONE) {
                mapFragment.getView().setVisibility(View.VISIBLE);
                iv.setVisibility(View.GONE);
            }
            break;
        }
    }
    

    Where "mapFragment" is my SupportedMapFragment and "iv" is an ImageView (make it match_parent).

    And here I am controlling the scroll:

    pager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position == 0 && positionOffsetPixels > 0 || position == 1 && positionOffsetPixels > 0) {
                    ((FragmentMap)adapter.getRegisteredFragment(1)).setSnapshot(View.GONE);
                } else if(position == 1 && positionOffsetPixels == 0) {
                    ((FragmentMap)adapter.getRegisteredFragment(1)).setSnapshot(View.VISIBLE);
                }
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {}
            @Override
            public void onPageSelected(int arg0) {}
        });
    

    My fragment with map (FragmentMap) is on position 1, so I need to controll the scroll from position 0 to 1 and from position 1 to 2 (the first if-clause). "getRegisteredFragment()" is a function in my custom FragmentPagerAdapter, in which I have a SparseArray(Fragment) called "registeredFragments".

    So, whenever you scroll to or from your map, you always see a snapshot of it. This works very well for me.

    0 讨论(0)
  • 2021-02-04 14:31

    I was able to adapt the above for the same issue in a Viewpager by overriding onPageScrolled in the FragmentPagerAdapter:

    @Override
    public void onPageScrolled(int position, float positionOffset, 
                               int positionOffsetPixels) {
        if ((position == 0 && positionOffsetPixels > 0) ||
            (position == 1 && positionOffsetPixels == 0)) {
            MyFragment blackedOutFragment = (MyFragment) getSupportFragmentManager()
                    .findFragmentByTag("android:switcher:" + R.id.pager + ":1");
            if (blackedOutFragment != null) {
                blackedOutFragment.getView().setVisibility(View.GONE);
                blackedOutFragment.getView().setVisibility(View.VISIBLE);
            }
        }
    }
    

    Where position 0 in the Viewpager is a Fragment holding a MapFragment and position 1 is the Fragment getting blacked out after the scroll.

    0 讨论(0)
  • 2021-02-04 14:31

    You can use this library,

    https://github.com/NyxDigital/NiceSupportMapFragment/

    This work fine for me.

    0 讨论(0)
  • 2021-02-04 14:39

    This should fix it by making the surface view transparent

    https://github.com/NyxDigital/NiceSupportMapFragment

    0 讨论(0)
  • 2021-02-04 14:43

    Just an experiment, but have you tried adding the TextView to the screen programmatically in setupMap() after the Map has already been initialized?

    // First, get a handle on the parent RelativeLayout, 
    // call it parentRelativeLayout, then…                  
    
    TextView textView = new TextView(getActivity());
    RelativeLayout.LayoutParams textLayout = new RelativeLayout.LayoutParams(
        RelativeLayout.MATCH_PARENT, 
        RelativeLayout.MATCH_PARENT
    );
    textLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1);
    textView.setLayoutParams(textLayout);
    
    parentRelativeLayout.addView(textView)
    
    0 讨论(0)
提交回复
热议问题