我正在研究googlemap carousel(寻呼机)项目,需要在复杂的布局中显示google地图, 但每次我移动寻呼机时,都没有加载mapfragment。如果我翻回来,地图会突然出现。 这是我的代码:
MainActivity
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.*;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
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 java.util.ArrayList;
public class MainActivity extends FragmentActivity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
private ArrayList<MarkerOptions> mMarkers;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
}
class MyMapFragment extends SupportMapFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
GoogleMap map = getMap();
if (savedInstanceState == null) {
CameraUpdate center =
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
}
addMarker(map, 40.748963847316034, -73.96807193756104);
addMarker(map, 40.76866299974387, -73.98268461227417);
addMarker(map, 40.765136435316755, -73.97989511489868);
addMarker(map, 40.70686417491799, -74.01572942733765);
setRetainInstance(true);
}
private void addMarker(GoogleMap map, double lat, double lon) {
map.addMarker(new MarkerOptions().position(new LatLng(lat, lon)));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.ttt, null);
getActivity().getSupportFragmentManager()
.beginTransaction()
.add(R.id.map_layout, newInstance())
.commit();
return view;
}
}
class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return new MyMapFragment(); //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public int getCount() {
return 10; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getPageTitle(int position) {
return ("page " + String.valueOf(position + 1));
}
}
class MapAwarePager extends ViewPager {
public MapAwarePager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x,
int y) {
if (v instanceof SurfaceView || v instanceof TextureView || v instanceof PagerTabStrip) {
return (true);
}
return (super.canScroll(v, checkV, dx, x, y));
}
}
和main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MainActivity"
/>
<view android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.spot.maps.MapAwarePager" android:id="@+id/view_pager"/>
</LinearLayout>
和ttt.xml我在哪里放置片段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New EditText"
android:id="@+id/editText" android:layout_gravity="left|center_vertical" android:layout_weight="1"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_gravity="left|center_vertical" android:layout_weight="1"
android:id="@+id/map_layout">
</FrameLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Button"
android:id="@+id/button" android:layout_weight="1"/>
</LinearLayout>
我的Mainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.spot.maps"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10"/>
<permission
android:name="com.spot.maps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.spot.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--<uses-permission android:name="android.permission.INTERNET"/>-->
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:hardwareAccelerated="true">
<activity android:name="MainActivity"
android:screenOrientation="portrait"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyChbP6k9pnX4kLoTnOfXHFJK5Y_nM1wSqQ" />
</application>
</manifest>
我提出THIS VIDEO以便更好地理解。
希望你能帮助我。
答案 0 :(得分:0)
Viewpager运行时很难确定,为unexpected results留下了很多空间。
请注意寻呼机的适配器。很多例子都使用内部类来实现。
注2 ViewPagers
中使用的一般类型的适配器请注意,任一适配器都可以将ArrayList的实例绑定到视图,并且可以使用完全不同的方式来加载ViewPager框架。 INSTEAD迭代你对FragmentTransaction的调用,你可以在调用adapter.notifyDataSetChanged()之前简单地将片段添加到适配器中的ArrayList对象。 ViewPager应该自动管理内存并管理从更大的ArrayList对象中仅保留内存中最相关的mapView。
的示例简答:
通过ArrayList对象加载片段适配器,然后像许多样本一样调用“onNotifyDataSetChanged()”......