在我的应用程序中,我使用 FragmentActivity 设置了几个选项卡,并在选项卡旁边设置了一个按钮,用于激活幻灯片菜单。目前我有两个片段显示在标签 1 和 2 中。标签 1 的片段包含一个 v2 Google 地图,第二个只是一个空白屏幕。
但是,当我激活我的幻灯片菜单时,我得到了非常不同的行为,我现在已经在几个项目中使用了这个幻灯片菜单库,没有任何问题。当我在显示片段 2 时激活幻灯片菜单时,它工作正常,但是当显示片段 1(地图视图)时,激活幻灯片菜单会导致地图变黑,并在关闭幻灯片菜单时再次显示地图。
这是我设置标签的代码
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/linearLayout1"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>
</LinearLayout>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1" >
<FrameLayout
android:id="@+id/tab_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/tab_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/tab_3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/tab_4"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</RelativeLayout>
</TabHost>
JAVA
public class TabsFragment extends Fragment implements OnTabChangeListener {
private static final String TAG = "FragmentTabs";
public static final String TAB_MAP = "Map";
public static final String TAB_VENUES = "Venus";
public static final String TAB_GENRE = "Gap";
public static final String TAB_TICKETS = "Titus";
private View mRoot;
private TabHost mTabHost;
int mCurrentTab;
Button slide_btn;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.tabs_fragment, null);
mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
setupTabs();
return mRoot;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
mTabHost.setOnTabChangedListener(this);
mTabHost.setCurrentTab(0);
// manually start loading stuff in the first tab
updateTab(TAB_MAP, R.id.tab_1);
}
private void setupTabs() {
mTabHost.setup(); // important!
mTabHost.addTab(newTab(TAB_MAP, R.id.tab_1, R.drawable.tab_home));
mTabHost.addTab(newTab(TAB_VENUS, R.id.tab_2, R.drawable.tab_home));
mTabHost.addTab(newTab(TAB_GAP, R.id.tab_3, R.drawable.tab_home));
mTabHost.addTab(newTab(TAB_TITUS, R.id.tab_4, R.drawable.tab_home));
}
private TabSpec newTab(String tag, int tabContentId, int drawableId) {
Log.d(TAG, "buildTab(): tag=" + tag);
View indicator = LayoutInflater.from(getActivity()).inflate(
R.layout.tabindicator,
(ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
ImageView icon = (ImageView) indicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
TabSpec tabSpec = mTabHost.newTabSpec(tag);
tabSpec.setIndicator(indicator);
tabSpec.setContent(tabContentId);
return tabSpec;
}
@Override
public void onTabChanged(String tabId) {
Log.d(TAG, "onTabChanged(): tabId=" + tabId);
if (TAB_MAP.equals(tabId)) {
updateTab(tabId, R.id.tab_1);
mCurrentTab = 0;
return;
}
if (TAB_VENUS.equals(tabId)) {
updateTab(tabId, R.id.tab_2);
mCurrentTab = 1;
return;
}
if (TAB_GAP.equals(tabId)) {
updateTab(tabId, R.id.tab_3);
mCurrentTab = 2;
return;
}
if (TAB_TITUS.equals(tabId)) {
updateTab(tabId, R.id.tab_4);
mCurrentTab = 3;
return;
}
}
public void updateTab(String tabId, int placeholder) {
if (tabId.equals("Map")) {
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null) {
getFragmentManager().beginTransaction().add(placeholder, new MyFrag(), "Map").commit();
}
} else if (tabId.equals("Venus")) {
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null) {
getFragmentManager().beginTransaction().add(placeholder, new Green(), "Venus").commit();
}
} else if (tabId.equals("Gap")) {
} else if (tabId.equals("Titus")) {
}
}
public void slideMenu(){
if(isAdded()){
int width = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 200, getResources()
.getDisplayMetrics());
SlideoutActivity.prepare(getActivity(), R.id.inner_content, width);
Intent i = new Intent();
i.setClass(getActivity(), SlideMenuActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
getActivity().overridePendingTransition(0, 0);
}
}
}
然后我在一个 FragmentActivity 中渲染它,它自己的布局包含一个片段。
这是我用于地图的代码
XML
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/relativeLayout1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<ImageButton
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/search_selected" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/button3"
android:ems="10"
android:inputType="text" >
</EditText>
</RelativeLayout>
JAVA
public class MyFrag extends Fragment {
GoogleMap map;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.main, container, false);
new LoadMap().execute();
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}
public class LoadMap extends AsyncTask<String, Integer, Location> {
LocationManager lm;
LatLng p = new LatLng(51.50703296721856, -0.11260986328125);
@Override
protected Location doInBackground(String... params) {
map = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
LocationManager locationManager = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
p = new LatLng(location.getLatitude(), location.getLongitude());
return location;
}
protected void onPostExecute(Location location) {
try {
if (location != null) {
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLng(p));
map.moveCamera(CameraUpdateFactory.zoomTo(15));
map.setOnCameraChangeListener(null);
} else {
Toast.makeText(getActivity(), "You got nada",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}