Skip to content Skip to sidebar Skip to footer

How To Place A Google Map/code Inside A Viewpager

Like the title says, I want to make a ViewPager which contains two sections: 1- a Listview (name of the places to go with PHP / MySQL / JSON) 2- and a GoogleMap with Markers depe

Solution 1:

I've got three different fragments in a viewPager below. The example below is a dialogFragment but that doesn't make a difference it could be an activity or a fragment.

publicclassFragmentPagerSupportextendsDialogFragmentimplementsOnPageChangeListener {

    staticintNUM_ITEMS=3;

    publicinterfaceFragmentPageRefresh {
        voidonRefresh();
    }

    MyAdapter mAdapter;

    ViewPager mPager;

    static FragmentPagerSupport newInstance(int startingPage) {
        FragmentPagerSupportf=newFragmentPagerSupport();
        // Supply num input as an argument.Bundleargs=newBundle();
        args.putInt("startingPage", startingPage);
        f.setArguments(args);
        return f;
    }

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        intstartingPage=0;
        Bundleb= getArguments();
        if (b != null) {
            startingPage = b.getInt("startingPage");
        }


        Viewview= inflater.inflate(R.layout.fragment_pager, container);


        mAdapter = newMyAdapter(this.getChildFragmentManager());

        mPager = (ViewPager) view.findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
        mPager.setCurrentItem(startingPage);
        mPager.setOnPageChangeListener(this);
        getDialog().setTitle("Best Rides Settings");
        return view;
    }

    publicstaticclassMyAdapterextendsFragmentStatePagerAdapter {

        // private Map<Integer, Fragment> mPageReferenceMap = new// HashMap<Integer, Fragment>();publicMyAdapter(FragmentManager fm) {
            super(fm);
        }

        @OverridepublicintgetCount() {
            return NUM_ITEMS;
        }

        @Overridepublic Fragment getItem(int position) {
            Fragmentret=null;
            switch (position) {
            case0:
                ret = SettingMap.newInstance(position);
                break;
            case1:
                ret = SettingFollow.newInstance(position);
                break;
            case2:
                ret = SettingRecord.newInstance(position);
                break;
            }
            // mPageReferenceMap.remove(position);// mPageReferenceMap.put(position, ret);return ret;
        }

        @Overridepublic CharSequence getPageTitle(int position) {
            switch (position) {
            case0:
                return"Map Type";
            case1:
                return"Follow";
            case2:
                return"Tracker";
            default:
                return"unused";
            }
        }

Here's how you do the ".newInstance" in the fragment.

publicclassSettingFollowextendsFragmentimplementsOnCheckedChangeListener,
        android.widget.CompoundButton.OnCheckedChangeListener,
        OnSeekBarChangeListener {


    publicstatic Fragment newInstance(int position) {
        SettingFollowf=newSettingFollow();
        // Supply num input as an argument.Bundleargs=newBundle();
        args.putInt("num", position);
        f.setArguments(args);
        return f;
    }
 ...

}

simple layout

<?xml version="1.0" encoding="utf-8"?><!--
     Copyright (C) 2010 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
--><!-- Top-level content view for the simple fragment sample. --><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/default_screen_bg"android:orientation="vertical" ><android.support.v4.view.ViewPagerandroid:id="@+id/pager"android:layout_width="match_parent"android:layout_height="0px"android:layout_weight="1" ><android.support.v4.view.PagerTitleStripandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="top" /></android.support.v4.view.ViewPager></LinearLayout>

fire this beast up like this.

FragmentManagerfm= getSupportFragmentManager();
            FragmentPagerSupporteditSettingsDialog=newFragmentPagerSupport();
            // MapSettings editSettingsDialog = new MapSettings();
            editSettingsDialog.show(fm, "fragment_edit_name");

Post a Comment for "How To Place A Google Map/code Inside A Viewpager"