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.

public class FragmentPagerSupport extends DialogFragment implements
        OnPageChangeListener {

    static int NUM_ITEMS = 3;

    public interface FragmentPageRefresh {
        void onRefresh();
    }

    MyAdapter mAdapter;

    ViewPager mPager;

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

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

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


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


        mAdapter = new MyAdapter(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;
    }

    public static class MyAdapter extends FragmentStatePagerAdapter {

        // private Map<Integer, Fragment> mPageReferenceMap = new
        // HashMap<Integer, Fragment>();

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment ret = null;
            switch (position) {
            case 0:
                ret = SettingMap.newInstance(position);
                break;
            case 1:
                ret = SettingFollow.newInstance(position);
                break;
            case 2:
                ret = SettingRecord.newInstance(position);
                break;
            }
            // mPageReferenceMap.remove(position);
            // mPageReferenceMap.put(position, ret);
            return ret;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return "Map Type";
            case 1:
                return "Follow";
            case 2:
                return "Tracker";
            default:
                return "unused";
            }
        }

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

    public class SettingFollow extends Fragment implements
        OnCheckedChangeListener,
        android.widget.CompoundButton.OnCheckedChangeListener,
        OnSeekBarChangeListener {


    public static Fragment newInstance(int position) {
        SettingFollow f = new SettingFollow();
        // Supply num input as an argument.
        Bundle args = new Bundle();
        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. -->

<LinearLayout xmlns: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.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" >

        <android.support.v4.view.PagerTitleStrip
            android: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.

FragmentManager fm = getSupportFragmentManager();
            FragmentPagerSupport editSettingsDialog = new FragmentPagerSupport();
            // MapSettings editSettingsDialog = new MapSettings();
            editSettingsDialog.show(fm, "fragment_edit_name");

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