我的应用程序的MainActivity在ViewPager,MapFragment和ListFragment中有两个片段。我正在尝试按照Arshu's guide将MapFragment添加到ViewPager中,但没有太多运气。如果你想看看我的意思,这里是我的应用程序的截图:
以下是MainActivity的简明形式:
@Bind(R.id.activity_main_tabs)
PagerSlidingTabStrip mainTabs;
@Bind(R.id.activity_main_pager)
ViewPager mainPager;
@Bind(R.id.activity_main_toolbar)
Toolbar activityToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar(activityToolbar);
ButterKnife.bind(this);
fragManager = getSupportFragmentManager();
MainTabPagerAdapter adapter = new MainTabPagerAdapter(fragManager, aircraftArrayList);
mainPager.setAdapter(adapter);
mainTabs.setViewPager(mainPager);
}
我的ViewPager的TabPagerAdapter:
public class MainTabPagerAdapter extends FragmentStatePagerAdapter {
private final String[] TAB_TITLES = {"Map", "List of planes"};
public final String AIR_KEY = "aircraftKey";
public static FragmentManager fragMgr;
public Bundle bundle;
ArrayList<Aircraft> aircraftArrayList;
public MainTabPagerAdapter(FragmentManager fm, ArrayList<Aircraft> aircraftArrayList) {
super(fm);
fragMgr = fm;
this.aircraftArrayList = aircraftArrayList;
}
@Override
public CharSequence getPageTitle(int position){
return TAB_TITLES[position];
}
@Override
public int getCount() {
return TAB_TITLES.length;
}
@Override
public Fragment getItem(int position){
switch(position){
case 0:
return MainMapFragment.newInstance(aircraftArrayList);
case 1:
return AircraftListFragment.newInstance(1, aircraftArrayList);
}
return null;
}
}
我的MainActivity的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main"
android:fitsSystemWindows="true"
tools:context="com.example.se415017.maynoothskyradar.activities.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/activity_main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.v4.view.ViewPager
android:id="@+id/activity_main_pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#eeeeee">
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/activity_main_tabs"
android:layout_width="match_parent"
android:layout_height="36dp"
android:layout_gravity="top"
android:background="#eeeeee"
android:paddingBottom="4dp"
android:paddingTop="4dp"
app:pstsIndicatorColor="#33ccee"
app:pstsIndicatorHeight="4dp"
app:pstsUnderlineColor="#33ccee"
app:pstsUnderlineHeight="2dp"
app:pstsShouldExpand="true"/>
</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
而且,如果有帮助,我的MapFragment:
public class MainMapFragment extends Fragment implements OnMapReadyCallback {
static View view;
SupportMapFragment mainMapFrag;
private GoogleMap googleMap;
private CameraPosition camPos;
public static MainMapFragment newInstance(ArrayList<Aircraft> aircraftArrayList) {
MainMapFragment fragment = new MainMapFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(AIR_KEY, aircraftArrayList);
fragment.setArguments(bundle);
return fragment;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if(container == null)
return null;
view = inflater.inflate(R.layout.fragment_main_map, container, false);
ButterKnife.bind(this, view);
mainMapFrag = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.main_map);
mainMapFrag.getMapAsync(this);
setUpMapIfNeeded();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
if(googleMap != null)
setUpMap(googleMap);
else { //It's null anyway
((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.main_map)).getMapAsync(this);
mainMapFrag.getMapAsync(this);
if (googleMap != null)
setUpMap(googleMap);
}
}
@Override
public void onPause() {
super.onPause();
if(googleMap != null)
camPos = googleMap.getCameraPosition();
googleMap = null;
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
//Wait until googleMap is re-initialised
if(camPos != null & googleMap != null) {
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(camPos));
camPos = null;
}
}
//Sets up the map if it hasn't been set up already
//Somehow it works just fine if I get rid of "static"
protected void setUpMapIfNeeded() {
if (googleMap == null){
Log.d(TAG, "Map was null");
((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.main_map)) //TODO: FIX THIS
.getMapAsync(this);
//Check if the map was obtained successfully
if (googleMap != null)
setUpMap(googleMap);
}
}
/** This is where markers, lines and listeners are added, and where the camera is moved.
* @param googleMap The GoogleMap object to be set up.
*/
private void setUpMap(GoogleMap googleMap) {
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
//googleMap.setMyLocationEnabled(true); //TODO: Maybe wait until I have the pointing function worked out
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)))
.setTitle("My server is here");
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(53.5, -6.35), 8.0f));
}
@Override
public void onMapReady(final GoogleMap gMap) {
googleMap = gMap;
setUpMap(googleMap);
}
其布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.se415017.maynoothskyradar.fragments.MainMapFragment">
<fragment
android:id="@+id/main_map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
有人可以告诉我我做错了吗?
答案 0 :(得分:0)
事实证明,通过将PagerSlidingTabStrip放在ViewPager元素中,我搞砸了MainActivity的布局文件。这是我的固定文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main"
android:fitsSystemWindows="true"
tools:context="com.example.se415017.maynoothskyradar.activities.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/activity_main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:visibility="visible" />
<android.support.v4.view.ViewPager
android:id="@+id/activity_main_pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#eeeeee"
android:clickable="false"/>
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/activity_main_tabs"
android:layout_below="@id/activity_main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#eeeeee"
android:paddingBottom="4dp"
android:paddingTop="4dp"
app:pstsIndicatorColor="#33ccee"
app:pstsIndicatorHeight="4dp"
app:pstsUnderlineColor="#33ccee"
app:pstsUnderlineHeight="2dp"
app:pstsShouldExpand="true"
android:visibility="visible"
android:measureAllChildren="false"
android:longClickable="true"
android:clickable="true" />
</android.support.design.widget.CoordinatorLayout>