[GO] How to use Map in Android ViewPager

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.

Returns MapFragment

This is the easiest way to display only the Map in the ViewPager.

The sample code returns SupportMapFragment.

MapFragmentPagerAdapter

class MapFragmentPagerAdapter(fragmentManager: FragmentManager) : FragmentStatePagerAdapter(fragmentManager) {

    override fun getItem(position: Int): Fragment {
        return SupportMapFragment.newInstance()
    }

    // omitted

}

Returns a Fragment with a ListView and a Homemade Cell in the ListView

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.

MapListViewAdapter

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

}

MyMapView

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

}

Returns a Fragment with a FrameLayout

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.

MapPagerFragment

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

}

reference

Recommended Posts

How to use Map in Android ViewPager
How to use classes in Theano
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
How to use calculated columns in CASTable
[Introduction to Python] How to use class in Python?
How to use Google Test in C
How to use Anaconda interpreter in PyCharm
How to use __slots__ in Python class
How to use regular expressions in Python
How to use is and == in Python
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to use Pandas 2
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to display Map using Google Map API (Android)
How to use the C library in Python
How to use Python Image Library in python3 series
How to use Python Kivy ④ ~ Execution on Android ~
Summary of how to use MNIST in Python
How to use tkinter with python in pyenv
[For beginners] How to use say command in python!
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
A memorandum on how to use keras.preprocessing.image in Keras
How to use Python argparse
How to use IPython Notebook
How to use Pandas Rolling