【问题标题】:How to make Google Maps scroll properly inside Scroll View?如何让谷歌地图在滚动视图中正确滚动?
【发布时间】:2015-10-14 05:24:55
【问题描述】:

我有一个片段,其中包含一个地图(视图),它位于我的布局的滚动视图中。

问题是地图不能正确滚动(难以滚动)向上或向下。滚动视图似乎干扰了地图的垂直滚动。它水平滚动很好(平滑)而不是垂直滚动。

我该如何解决这个问题?

我的 XML 布局:

<ScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/imageViewContactEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="250dp"
                android:contentDescription="@string/contactEmailIcon"
                android:src="@drawable/ic_action_contactemail" />

            <TextView
                android:id="@+id/textViewContactEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignEnd="@+id/imageViewContactEmail"
                android:layout_alignRight="@+id/imageViewContactEmail"
                android:layout_marginEnd="-50dp"
                android:layout_marginRight="-50dp"
                android:layout_marginTop="255dp"
                android:text="@string/contactEmail"
                android:textColor="#bdbdbd" />

            <TextView
                android:id="@+id/textViewContactEmailLink"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="61dp"
                android:layout_marginStart="61dp"
                android:layout_marginTop="275dp"
                android:text="@string/emailNDTC"
                android:textColor="#2196F3" />

            <ImageView
                android:id="@+id/imageViewContactLine"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewContactEmailLink"
                android:layout_marginTop="20dp"
                android:contentDescription="@string/contactLine"
                android:src="@drawable/line" />

            <ImageView
                android:id="@+id/imageViewContactPhone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageViewContactLine"
                android:layout_marginLeft="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:contentDescription="@string/contactPhoneIcon"
                android:src="@drawable/ic_action_contactphone" />

            <TextView
                android:id="@+id/textViewContactPhone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignEnd="@+id/imageViewContactEmail"
                android:layout_alignRight="@+id/imageViewContactEmail"
                android:layout_below="@+id/imageViewContactLine"
                android:layout_marginEnd="-50dp"
                android:layout_marginRight="-50dp"
                android:layout_marginTop="22dp"
                android:text="@string/contactPhone"
                android:textColor="#bdbdbd" />

            <TextView
                android:id="@+id/textViewContactPhoneLink"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageViewContactLine"
                android:layout_marginLeft="61dp"
                android:layout_marginStart="61dp"
                android:layout_marginTop="43dp"
                android:text="@string/phoneNDTC"
                android:textColor="#2196F3" />

            <ImageView
                android:id="@+id/imageViewContactLine2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewContactPhoneLink"
                android:layout_marginTop="20dp"
                android:contentDescription="@string/contactLine2"
                android:src="@drawable/line" />

            <ImageView
                android:id="@+id/imageViewContactLocationPin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageViewContactLine2"
                android:layout_marginLeft="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="18dp"
                android:contentDescription="@string/locationPinIcon"
                android:src="@drawable/ic_action_contactlocation" />

            <TextView
                android:id="@+id/textViewContactLocationPin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageViewContactLine2"
                android:layout_marginLeft="55dp"
                android:layout_marginStart="55dp"
                android:layout_marginTop="20dp"
                android:text="@string/contactAddress"
                android:textColor="#bdbdbd" />

            <TextView
                android:id="@+id/textViewContactStreetAddress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewContactLocationPin"
                android:layout_marginLeft="56dp"
                android:layout_marginStart="56dp"
                android:layout_marginTop="4dp"
                android:text="@string/ndtcStreetAddress"
                android:textColor="@color/primaryTextColor" />

            <RelativeLayout
                android:id="@+id/map_container"
                android:layout_width="320dp"
                android:layout_height="200dp"
                android:layout_below="@+id/textViewContactStreetAddress"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="30dp"
                android:layout_marginStart="30dp"
                android:layout_marginTop="12dp"
                tools:context=".ContactsActivity">

                <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:map="http://schemas.android.com/apk/res-auto"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:id="@+id/map"
                    android:name="com.google.android.gms.maps.SupportMapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:context="pi.com.ndtc.MapsActivity" />

            </RelativeLayout>

            <ImageView
                android:id="@+id/imageViewContactLine3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/map_container"
                android:layout_marginLeft="55dp"
                android:layout_marginStart="55dp"
                android:layout_marginTop="20dp"
                android:contentDescription="@string/contactLine3"
                android:src="@drawable/line" />

            <TextView
                android:id="@+id/textViewContactTitleSocial"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageViewContactLine3"
                android:layout_marginTop="5dp"
                android:text="@string/contactTitleSocial"
                android:textColor="#bdbdbd" />
        </RelativeLayout>
    </ScrollView>

我的活动:

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));


    }
}

【问题讨论】:

  • 不要在滚动视图中使用谷歌地图!因为 MapView 已经是滚动布局了!
  • 但是如果我不把它放在滚动视图中,它如何与我的其他内容一起滚动?

标签: android google-maps scrollview android-scrollview


【解决方案1】:

可以添加适合mapFragment的透明ImageView:

           <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imagetrans"
            android:layout_alignTop="@+id/mapfragment"
            android:layout_alignBottom="@+id/mapfragment"
            android:layout_alignEnd="@+id/mapfragment"
            android:layout_alignRight="@+id/mapfragment"
            android:layout_alignLeft="@+id/mapfragment"
            android:layout_alignStart="@+id/mapfragment"
            android:src="@color/transparent"/>

然后,当用户触摸它时,禁止滚动视图:

final ScrollView scroll = (ScrollView) findViewById(R.id.scroll);
ImageView transparent = (ImageView)findViewById(R.id.imagetrans);

transparent.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.
                    scroll.requestDisallowInterceptTouchEvent(true);
                    // Disable touch on transparent view
                    return false;

                case MotionEvent.ACTION_UP:
                    // Allow ScrollView to intercept touch events.
                    scroll.requestDisallowInterceptTouchEvent(false);
                    return true;

                case MotionEvent.ACTION_MOVE:
                    scroll.requestDisallowInterceptTouchEvent(true);
                    return false;

                default:
                    return true;
            }
        }
    });

这样,只有当用户触摸 ImageView 时,地图才会响应触摸事件。其余时间,ScrollView 会响应。

【讨论】:

  • transparent.setOnTouchListener 中的“透明”是什么?
  • transparent 显然是透明的 ImageView。您可以像初始化任何其他组件一样初始化它:ImageView transparent=findViewById(R.id.imagetrans)。而滚动......是滚动视图。您需要对其进行引用才能禁止滚动。引用它的方式与处理其他任何内容的方式相同:ScrollView scroll=findViewById(R.id.scroll)
  • 是的,我因为累而心慌,我询问后立即解决了这个问题。不过欣赏它并将您的答案标记为正确。图像视图与侦听器相结合就成功了。
  • 出色的解决方案
  • 真是个好主意,先生。喜欢它
【解决方案2】:

【讨论】:

  • 引起:java.lang.ClassCastException:com.google.android.gms.maps.SupportMapFragment 无法转换为 pi.com.mymapsapplication.WorkaroundMapFragment
  • 我刚才试过了,它在这一行给了我上面的错误:mMap = ((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();跨度>
  • 请更改您的 xml 文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-18
  • 2015-08-12
  • 1970-01-01
  • 2011-12-29
  • 2018-01-23
  • 1970-01-01
  • 2016-10-03
相关资源
最近更新 更多