To place a Map inside a ViewPager, use the FragmentStatePagerAdapter as the ViewPager Adapter and return the Fragment in the following way.
-[Return MapFragment](returns #mapfragment) -[Return Fragment with ListView and return homemade cell with ListView](Return fragment with #listview and return homemade cell with listview) -[Return Fragment with FrameLayout](Return fragment with #framelayout)
The sample code for each is available on GitHub. The outline is shown below.
This is the easiest way to display only the Map in the ViewPager.
The sample code returns SupportMapFragment.
class MapFragmentPagerAdapter(fragmentManager: FragmentManager) : FragmentStatePagerAdapter(fragmentManager) {
override fun getItem(position: Int): Fragment {
return SupportMapFragment.newInstance()
}
// omitted
}
It is a method to display ListView on each page of ViewPager and display Map in the self-made cell in ListView. For the homebrew cell, View is defined in FrameLayout, and SupportMapFragment is added to it in the constructor.
class MapListViewAdapter(private val context: Context, private val fragmentManager: FragmentManager) : BaseAdapter() {
private val inflater = LayoutInflater.from(context)
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
// omitted
val mapView = MyMapView(this.context, this.fragmentManager)
val layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 500)
mapView.layoutParams = layoutParams
return mapView
}
// omitted
}
class MyMapView(context: Context, fragmentManager: FragmentManager) : FrameLayout(context) {
init {
val inflater = LayoutInflater.from(context)
val layoutView = inflater.inflate(R.layout.view_map, null)
val view = layoutView.mapFrameLayout
val mapFragment = SupportMapFragment.newInstance()
fragmentManager.beginTransaction().add(view.id, mapFragment).commit()
mapFragment.getMapAsync {
// nothing
}
this.addView(layoutView)
}
}
It is a method to define FrameLayout in the layout of each page of ViewPager and add SupportMapFragment to it. At this time, use ** childFragmentManager ** instead of fragmentManager.
class MapPagerFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.fragment_map_pager, container, false)
val mapView = view.mapView
val mapFragment = SupportMapFragment.newInstance()
this.childFragmentManager.beginTransaction().add(mapView.id, mapFragment).commit()
mapFragment.getMapAsync {
// nothing
}
return view
}
// omitted
}
Recommended Posts