[英]How to add a Fragment to a tab in Android?
這是我的MapFragment:
public class MapFragment extends SherlockFragment {
public static final String TAG = "mapFragment";
public MapFragment() {}
@Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
// The Activity created the MapView for us, so we can do some init stuff.
Exchanger.mMapView.setClickable(true);
Exchanger.mMapView.setBuiltInZoomControls(true); // If you want.
/*
* If you're getting Exceptions saying that the MapView already has
* a parent, uncomment the next lines of code, but I think that it
* won't be necessary. In other cases it was, but in this case I
* don't this should happen.
*/
/*
* final ViewGroup parent = (ViewGroup) Exchanger.mMapView.getParent();
* if (parent != null) parent.removeView(Exchanger.mMapView);
*/
return Exchanger.mMapView;
}
}
This is my LocationTabActivity:
public class LocationTabActivity extends RoboSherlockMapActivity implements ActionBar.TabListener{
private MapFragment mMapFragment;
private MyListFragment mMyListFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Exchanger.mMapView = (MapView)findViewById(R.id.mapView1);
getSupportActionBar()
.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab newTab0 = getSupportActionBar().newTab();
newTab0.setText("Location");
ActionBar.Tab newTab1 = getSupportActionBar().newTab();
newTab1.setText("Map");
newTab0.setTabListener(this);
newTab1.setTabListener( this);
getSupportActionBar().addTab(newTab0);
getSupportActionBar().addTab(newTab1);
}
我發現將片段添加到Android選項卡非常困難。 有什么幫助嗎?
此鏈接可能會對您有所幫助。
您還可以在低於11的API級別中實現片段。 為此,您需要執行以下步驟:-將android-support-v4.jar添加到您的項目中。 在SDK-PATH / extras / android / compatibility / v4 /中找到此jar。 將其復制到項目根目錄下的libs文件夾中,並將其添加到項目構建路徑中。 使用FragmentActivity而不是Activity。 要獲取FragmentManager,請使用getSupportFragmentManager而不是getFragmentManager。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.