我有一个只能水平滚动而不是垂直滚动的SupportMapFragment。我尝试在线搜索但是每个人都遇到水平滚动问题,这在我的情况下很好。任何帮助,将不胜感激。地图不会动态移动。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
SetupViewPager(pager);
TabLayout tab = (TabLayout) findViewById(R.id.tabs);
tab.setupWithViewPager(pager);
public void SetupViewPager(ViewPager pager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Map(),"Map");
adapter.addFragment(new CustomAlarm(),"2nd Tab");
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String Title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(Title);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
pager.setAdapter(adapter);
}
这是我的Map Fragment。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_blank, container, false);
SupportMapFragment support = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
support.getMapAsync(this);
return v;
}
@Override
public void onMapReady(GoogleMap googleMap) {
setmap(googleMap);
}
public void setmap(final GoogleMap map) {
googlemap = map;
googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googlemap.getUiSettings().setMyLocationButtonEnabled(true);
googlemap.setOnMapLongClickListener(this);
}
这是我的XML。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Map"
android:orientation="horizontal">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
/>
MainActivity.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="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:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/whatever"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
style="@style/CustomTabTheme"
/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight ="1"
android:background="@android:color/white"
/>
</android.support.design.widget.AppBarLayout>