[英]GoogleMap inside of a Fragment in a ViewPager, keep all touch events in the GoogleMap
[英]Get GoogleMap from ViewPager's fragment
我正在嘗試從Fragment
獲取GoogleMap
對象,這是ViewPager
的第二頁,其內容通過FragmentPagerAdapter
給出。
我的ViewPager
包含兩個頁面:普通布局和Google地圖。
我可以訪問第一個布局:
View tempView = LayoutInflater.from(getApplication())
.inflate(R.layout.start_activity, null);
tempTextView = (TextView) tempView.findViewById(R.id.timer);
這里一切都好。 但我也想訪問谷歌地圖。
當地圖處於單獨的活動中時,我可以讓GoogleMap
對象執行此操作:
map=((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
但是現在上面的行返回null
問題是如何從片段中獲取GoogleMap
對象?
他們建議這樣做(有些人說這有效),但它對我不起作用:
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentByTag("android:switcher:" + R.id.pager + ":1")).getMap();
我在這里提供代碼。
我已經在這里找到了解決方案,但也會在這里發布,以便其他用戶查看代碼並且不會像我那樣被困住數周。
MyPagerAdapter.java
public class MyPagerAdapter extends FragmentPagerAdapter
{
private Context context;
final LatLng YEREVAN = new LatLng(40.181, 44.513);
public MyPagerAdapter(FragmentManager fm, Context context)
{
super(fm);
this.context = context;
}
@Override
public Fragment getItem(int position)
{
switch (position)
{
case 0:
return new MyFragment();
case 1:
return MyMapFragment.newInstance(YEREVAN);
}
return null;
}
@Override
public int getCount()
{
return 2;
}
@Override
public CharSequence getPageTitle(int position)
{
Locale l = Locale.getDefault();
switch (position)
{
case 0:
return context.getString(R.string.start).toUpperCase(l);
case 1:
return context.getString(R.string.map).toUpperCase(l);
}
return null;
}
}
MyMapFragment.java
public class MyMapFragment extends SupportMapFragment
{
private LatLng latLon;
public MyMapFragment()
{
super();
}
public static MyMapFragment newInstance(LatLng position)
{
MyMapFragment frag = new MyMapFragment();
frag.latLon = position;
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = super.onCreateView(inflater, container, savedInstanceState);
initMap();
return v;
}
private void initMap()
{
UiSettings settings = getMap().getUiSettings();
settings.setAllGesturesEnabled(false);
settings.setMyLocationButtonEnabled(false);
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(latLon, 16));
getMap().addMarker(new MarkerOptions().position(latLon).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
}
}
問題是你的MyFragmentPagerAdapter
不使用SupportMapFragment
,但只添加了MyFragment
。 您應該將其getItem
函數更改為以下內容:
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case 0:
fragment = new MyFragment();
break;
case 1:
fragment = new MySupportMapFragment();
break;
}
Bundle args = new Bundle();
args.putInt(MyFragment.ARG_SECTION_NUMBER, position);
fragment.setArguments(args);
return fragment;
}
為此,您必須實現自己的類型MySupportMapFragment
,您可以在其中加載布局,類似於您對MyFragment
所做的MyFragment
。
然后,您可以直接從FragmentPagerAdapter
調用SupportMapFragment。 喜歡:
SupportMapFragment supportMapFragment =
(SupportMapFragment) mMyFragmentPagerAdapter.getItem(1);
map = supportMapFragment.getMap();
一旦添加到適配器,您應該獲得您期望的SupportMapFragment。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.