MapFragment in ScrollView

前端 未结 8 1796
再見小時候
再見小時候 2020-12-07 17:17

I have one of the new MapFragments in a ScrollView. Actually it\'s a SupportMapFragment, but anyway. It works, but there are two problems:

8条回答
  •  心在旅途
    2020-12-07 17:57

    For me adding a transparent ImageView did not help remove the black mask completely. The top and bottom parts of map still showed the black mask while scrolling.

    So the solution for it, I found in this answer with a small change. I added,

    android:layout_marginTop="-100dp"
    android:layout_marginBottom="-100dp"
    

    to my map fragment since it was vertical scrollview. So my layout now looked this way:

    
    
        
    
        
    
       
    

    To solve the second part of the question I set requestDisallowInterceptTouchEvent(true) for my main ScrollView. When the user touched the transparent image and moved I disabled the touch on the transparent image for MotionEvent.ACTION_DOWN and MotionEvent.ACTION_MOVE so that map fragment can take Touch Events.

    ScrollView mainScrollView = (ScrollView) findViewById(R.id.main_scrollview);
    ImageView transparentImageView = (ImageView) findViewById(R.id.transparent_image);
    
    transparentImageView.setOnTouchListener(new View.OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
               case MotionEvent.ACTION_DOWN:
                    // Disallow ScrollView to intercept touch events.
                    mainScrollView.requestDisallowInterceptTouchEvent(true);
                    // Disable touch on transparent view
                    return false;
    
               case MotionEvent.ACTION_UP:
                    // Allow ScrollView to intercept touch events.
                    mainScrollView.requestDisallowInterceptTouchEvent(false);
                    return true;
    
               case MotionEvent.ACTION_MOVE:
                    mainScrollView.requestDisallowInterceptTouchEvent(true);
                    return false;
    
               default: 
                    return true;
            }   
        }
    });
    

    This worked for me. Hope it helps you..

提交回复
热议问题