簡體   English   中英

如何在viewpager中放置谷歌地圖/代碼

[英]How to place a google map/code inside a viewpager

就像標題中所說的,我想制作一個包含兩個部分的ViewPager

1- a Listview (PHP / MySQL / JSON所要使用的地方的名稱)

2-和帶有MarkersGoogleMap ,具體取決於您在第1點中選擇的內容

因此,我有一個用於ListView的代碼,另一個是具有位置按鈕的GoogleMap的代碼。 我想將它們合並到ViewPager中,但是我不知道如何實現這一點。 請參閱下面的代碼:

谷歌類:

package com.bla;


import com.google.android.gms.maps.GoogleMap;

import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

//extends FragmentActivity

public class google extends FragmentActivity implements LocationListener  {


    Button btnShowLocation;

    // GPSTracker class
GPSTracker gps;
// GoogleMap googleMap;

LatLng myPosition;

private SupportMapFragment map;
private GoogleMap mMapView;


// add all necessary things

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google);

// if  Google Play Services are available then

            // Getting reference to the SupportMapFragment of activity_main.xml
          // SIRVE ... SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);


     /*   fm = SupportMapFragment.newInstance();
        FragmentTransaction fragmentTransaction =
                 getChildFragmentManager().beginTransaction();
         fragmentTransaction.add(R.id.map, fm);
         fragmentTransaction.commit();   */ 

          //  SupportMapFragment fm = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
        FragmentManager fm = getSupportFragmentManager();
        map = (SupportMapFragment) fm.findFragmentById(R.id.map);
        if (map == null) {
            map = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map, map).commit();
        }

      //   GoogleMap = mMapFragment.getMap();

            // Getting GoogleMap object from the fragment
      //  mMapView = fm.getMap();
        mMapView = map.getMap();
            // Enabling MyLocation Layer of Google Map
        mMapView.setMyLocationEnabled(true);

            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();

            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);

            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);

            if(location!=null){
            // Getting latitude of the current location
            double latitude = location.getLatitude();

            // Getting longitude of the current location
            double longitude = location.getLongitude();

            // Creating a LatLng object for the current location
            LatLng latLng = new LatLng(latitude, longitude);

             myPosition = new LatLng(latitude, longitude);

             mMapView.addMarker(new MarkerOptions().position(myPosition).title("Start"));

            }

}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    }

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

}

listview類:

package com.example.mysqltest;


import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;
import com.example.mysqltest.R;



import com.viewpagerindicator.TabPageIndicator;

public class titlepager extends Activity {



// GoogleMap googleMap;

LatLng myPosition;

private SupportMapFragment map;
private GoogleMap mMapView;


        //these are the titles that will appear on the "tabs"
    final String[] page_titles = new String[]{"Home", "Me"};
    //this will go the description TextView
        final String[] desc = new String[]{
            "Aqui ira el listview ",
            "Aqui iraaa el mapa :D ",
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.title);

        //Instantiating the adapter
        GiloAdapter mAdapter = new GiloAdapter(this);

        //instantiate the Views
        ViewPager mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        TabPageIndicator mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);


    private class GiloAdapter extends PagerAdapter{

        Context context;

        public GiloAdapter(Context c){
            this.context = c;
        }

        //This is the number of pages -- 5
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return page_titles.length;
        }

        @Override
        public boolean isViewFromObject(View v, Object o) {
            // TODO Auto-generated method stub
            return v.equals(o);
        }

        //This is the title of the page that will apppear on the "tab"
        public CharSequence getPageTitle(int position) {
            return page_titles[position];
        }

        //This is where all the magic happen
        public Object instantiateItem(View pager, int position) {
            final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.page, null, false);
            View v2 = inflater.inflate(R.layout.page2, null, false);

            TextView title = (TextView)v.findViewById(R.id.tvTitle);
            TextView description = (TextView) v.findViewById(R.id.tvdesc);

            title.setText(page_titles[position]);
            description.setText(desc[position]);

            //This is very important
            ( (ViewPager) pager ).addView( v, 0 );


            ( (ViewPager) pager ).addView( v2, 1 );
            return v;
        }


    @Override
    public void destroyItem(View pager, int position, Object view) {
        ((ViewPager) pager).removeView((View) view);
    }

    @Override
    public void finishUpdate(View view) {
    }

    @Override
    public void restoreState(Parcelable p, ClassLoader c) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View view) {
    }

}


}

是否有合並這些類的選項? 切換每頁以及要收取的版式? 我是Android新手,您能幫我知道怎么做嗎?

我在下面的viewPager中有三個不同的片段。 下面的示例是一個dialogFragment,但沒有區別,它可以是活動或片段。

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";
            }
        }

這是您在片段中執行“ .newInstance”的方法。

    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;
    }
 ...

}

簡單的布局

<?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>

像這樣開火。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM