2015-10-14 4 views
2

У меня есть фрагмент, который содержит карту (вид), которая находится внутри прокрутки моего макета.Как правильно прокручивать Карты Google внутри прокрутки?

Проблема в том, что карта не прокручивается правильно (трудно прокручивать) вверх или вниз. Кажется, что вид прокрутки мешает вертикальной прокрутке карты. Он прокручивается точно (плавно) горизонтально, а не вертикально.

Как это исправить?

МОЯ XML Layout:

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


    } 
} 
+0

Не используйте карту google внутри scrollview! Поскольку MapView уже является прокруткой макета! –

+0

Но если я не помещу его внутри прокрутки, как он будет прокручиваться вместе с другим содержимым? – Equivocal

ответ

8

Вы можете добавить прозрачную ImageView, который соответствует MapFragment:

  <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"/> 

И тогда, когда пользователь прикасается его, запретит Scrollview:

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 ответит.

+0

Что такое «прозрачность» в transparent.setOnTouchListener? – Equivocal

+0

прозрачный прозрачный ImageView, очевидно. Вы можете инициализировать его так же, как вы инициализируете любой другой компонент: ImageView transparent = findViewById (R.id.imagetrans). И прокрутка ... это scrollview. Вам нужна ссылка на него, чтобы запретить прокрутку. Вы ссылаетесь на него так же, как и на что-либо еще: ScrollView scroll = findViewById (R.id.scroll) – Fustigador

+0

Да, я взволнована из-за усталости, я сразу же исправил это. Оцените это, хотя и пометили свой ответ как правильный. Образ изображения в сочетании с слушателем сделал трюк. – Equivocal

0

Для Google Map v2, выполните решение этот учебник

http://www.londatiga.net/it/programming/android/how-to-make-android-map-scrollable-inside-a-scrollview-layout/

+0

Вызвано: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment не может быть перенесено на pi.com.mymapsapplication.WorkaroundMapFragment – Equivocal

+0

Я пробовал это сейчас, и он дал мне приведенную выше ошибку в этой строке: mMap = ((WorkaroundMapFragment) getSupportFragmentManager(). FindFragmentById (R.id.map)). GetMap(); – Equivocal

+0

пожалуйста, измените XML-файл <фрагмент андроида: тег = "fragment_map" андроид: идентификатор = "@ + идентификатор/fragment_map" андроид: layout_width = "match_parent" андроид: layout_height = "175dp" андроид: layout_marginTop = "@ dimen/activity_horizontal_margin" class = "your.package.name.WorkaroundMapFragment" /> – Sunny

Смежные вопросы