2013-07-15 3 views
2

Android 프로젝트에서 Google지도 V2를 사용하고 있습니다. 나는 (마법사를 사용하여) 스크롤 할 수있는 탭 + 스 와이프를 사용하여 내 레이아웃 중 하나에서 SupportMapFragment를 사용하고 있지만지도에서 가로로 스크롤 할 수는 없지만지도 내부를 스 와이프하면 화면이 나에게 표시됩니다. 다른보기. 여기에 내 코드입니다 :Android -지도가 가로로 스크롤되지 않습니다.

MainActivity

public class MainActivity extends FragmentActivity { 

    private List<Fragment> paginas; 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
    * will keep every loaded fragment in memory. If this becomes too memory 
    * intensive, it may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     if(paginas==null){ 
      paginas = new ArrayList<Fragment>(); 
      paginas.add(new LocalizacaoPesquisadorActivity()); 
      paginas.add(new VotosPorRegiaoActivity()); 
     } 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 



    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 
      switch (position) { 
      case 0: 
       return paginas.get(0); 
      case 1: 
       return paginas.get(1); 
      default: return null; 

      }   
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return paginas.size(); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
      case 0: 
       return getString(R.string.title_activity_localizacao_pesquisador).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_activity_votos_por_regiao).toUpperCase(l);   
      } 
      return null; 
     } 
    } 



} 

LocalizacaoPesquisadorActivity

public class LocalizacaoPesquisadorActivity extends Fragment { 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.activity_localizacao_pesquisador, 
      container, false); 
    System.out.println(); 
    return rootView; 
} 


} 

activity_main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <android.support.v4.view.PagerTitleStrip 
     android:id="@+id/pager_title_strip" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:background="#33b5e5" 
     android:paddingBottom="4dp" 
     android:paddingTop="4dp" 
     android:textColor="#fff" /> 

</android.support.v4.view.ViewPager> 

에게

<RelativeLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".LocalizacaoPesquisadorActivity" > 

    <EditText 
     android:id="@+id/editTextDataLocalizacaoPesquisador" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:inputType="none" 
     android:hint="Data" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="4dp" 
     android:layout_marginBottom="4dp"/> 

    <Spinner 
     android:id="@+id/spinnerHorarioLocalizacaoPesquisador" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editTextDataLocalizacaoPesquisador" 
     android:layout_alignRight="@+id/editTextDataLocalizacaoPesquisador" 
     android:layout_below="@+id/editTextDataLocalizacaoPesquisador" 
     android:layout_marginTop="4dp" 
     android:layout_marginBottom="4dp" /> 

    <Button 
     android:id="@+id/buttonAtualizarLocalizacaoPesquisador" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editTextDataLocalizacaoPesquisador" 
     android:layout_alignRight="@+id/editTextDataLocalizacaoPesquisador" 
     android:layout_below="@+id/spinnerHorarioLocalizacaoPesquisador" 
     android:text="Atualizar" 
     android:layout_marginTop="4dp" 
     android:layout_marginBottom="4dp" 
     android:onClick="atuarlizarMapaLocalizacaoPesquisador" /> 

    <fragment android:id="@+id/mapaLocalizacaoPesquisador" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_below="@+id/buttonAtualizarLocalizacaoPesquisador" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/buttonAtualizarLocalizacaoPesquisador" 
     android:layout_alignRight="@+id/buttonAtualizarLocalizacaoPesquisador" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="4dp" 
     android:layout_marginTop="4dp"   
     map:cameraTargetLat="-23.531152" 
     map:cameraTargetLng="-46.789876" 
     map:cameraZoom="1" 
     /> 

</RelativeLayout> 

는지도에있을 때 난 요격에 슬쩍 작업을 생각, 당신은 무엇을 activity_localizacao_pesquisador.xml 생각?

+0

[GoogleMap 하위 컨트롤이 가로로 스크롤 할 수있게하는 [Custom ViewPager] 복제본 가능 (http://stackoverflow.com/questions/14388494/custom-viewpager-to-allow-child-googlemap-control-to-scroll-horizontally)) – CommonsWare

답변

4

먼저, 안드로이드 SDK를 업데이트하고 <android-sdk>\extras\android\support\v4\android-support-v4.jar에서 복사하여 안드로이드 지원 라이브러리의 최신 버전을 사용하고 있는지 확인하십시오. 이전 버전의 지원 라이브러리는이를 올바르게 처리하지 못합니다.

<com.your.package.ViewPagerMapBevelScroll 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

:합니다 (ViewPagerMapBevelScroll 객체의 위치를이 일치하는지 확인이 패키지의 이름을 변경하기) ViewPagerMapBevelScroll를 사용하도록 레이아웃을 변경, 그리고

/** 
* Extension of ViewPager that allows the user to move the map 
* inside the fragment, but still supports swiping to the 
* next fragment via "bezel swipe" (i.e., swiping from the edge) 
*/ 
public class ViewPagerMapBevelScroll extends android.support.v4.view.ViewPager { 

    private static final int DEFAULT_SWIPE_MARGIN_WIDTH_DIP = 20; 
    private int swipeMarginWidth; 

    public ViewPagerMapBevelScroll(Context context) { 
     super(context); 
     setDefaultSwipeMargin(context); 
    } 

    public ViewPagerMapBevelScroll(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setDefaultSwipeMargin(context); 
    } 

    private void setDefaultSwipeMargin(final Context context) { 
     swipeMarginWidth = (int) (DEFAULT_SWIPE_MARGIN_WIDTH_DIP * context 
       .getResources().getDisplayMetrics().density); 
    } 

    @Override 
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) { 
      if (v instanceof ViewPagerMapBevelScroll) { 
       if (getCurrentItem() == SectionsPagerAdapter.MAP_TAB) { 
        return !isAllowedMapSwipe(x, dx); 
       } 
      } 
      return super.canScroll(v, checkV, dx, x, y); 
     } 

    /** 
    * Determines if the pointer movement event at x and moved pixels is 
    * considered an allowed swipe movement overriding the inner horizontal 
    * scroll content protection. 
    * 
    * @param x X coordinate of the active touch point 
    * @param dx Delta scrolled in pixels 
    * @return true if the movement should start a page swipe 
    */ 
    protected boolean isAllowedMapSwipe(final float x, final float dx) {   
     return ((x < swipeMarginWidth) && (dx > 0)) 
      || ((x > (getWidth() - swipeMarginWidth)) && (dx < 0)); 
    } 
} 

:

그런 다음 ViewPager 객체를 확장 그런 다음 SectionsPagerAdapter을 변경하여 ViewPagerMapBevelScroll에서 참조하는 MAP_TAB 값을 추가하고이 값을지도가 포함 된 탭의 색인으로 설정합니다.

public class SectionsPagerAdapter extends FragmentStatePagerAdapter { 

    public static final int MAP_TAB = 0; // Set this equal to 
             // the index of your map tab 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 
... 
} 

오픈 오피스 GPSTest app의 전체 작동 버전을 보려면 여기를 클릭하십시오. isAllowedMapSwipe 방법

신용

은 [업데이트 11 월 (8), 2013] (http://goo.gl/Hyl2E 참조) 마틴 Hochstrasser로 이동 - 참고 ViewPager 및 구글과 가장 최근의 지원 라이브러리와이 답변에 BornToCode의 의견에 따라 Play 서비스 라이브러리에서이 해결 방법은 Android 3.0 (Honeycomb) 이상에서는 필요하지 않지만 Android 2.3 (Gingerbread) 및 Android 2.2 (Froyo)에서 올바른 동작을 수행해야합니다.

+0

기본 스 와이프 또한 20dp (DrawerLayout (http://developer.android.com/design/patterns/navigation-drawer.html) 참조) 일 수 있습니다 생각합니다. 하나의 질문 : "v instanceof ViewPagerMapBevelScroll' 또는'v == this'가 가장 적절합니다. – BornToCode

+0

내가 20dp에이 예제를 변경하고 내 앱에서 해보자, 좋은 점을 @BornToCode. 저는 그것이 'instanceof'가되어야한다는 것을 상기하고 있습니다. 그러나 빠른 견해에서 나는 왜 그런지 기억하지 못합니다. '== this'을 시도 했습니까? –

+0

나는'== this'을 시도해 보았습니다. – BornToCode

관련 문제