3

有关工作原理的详细信息,请参阅我的previous question使用ActionBarSherlock和SupportMapFragment的子类内部选项卡式导航

当前设置

我创建了一个名为SherlockMapFragment延伸SupportMapFragmentActionBarSherlock(ABS)库中的类。这工作正常,以显示地图等,我直到现在还没有意识到(因为我是一个白痴)以下:

  • 我创建了一个叫做mapFrag的SherlockMapFragment实例。
  • 然后使用并显示这个我调用mapFrag.newInstance()。

这实际上是在返回一个SupportMapFragment的实例,但是这个调用对于显示MapView是必须的。

这是一个巨大的问题,我试图实现,因为我试图将我的碎片添加到选项卡式导航栏,但显然我不能将一个SupportMapFragment添加到TabListener,因为我得到各种类转换异常等其预计延期SherlockFragmentFragment

任何人都可以看到一个方法吗?或者如果我需要让自己更清楚,请提问。

一个可能的解决

一种解决方案是创建自己的标签栏和处理的Fragments交易自己。但是,这些工具已经存在,如果只是一个简单的修复,我并不想这样做。

预先感谢

回答

3

你可以扩展SherlockFragment和处理自己的MapView。这看起来是这样的......

public class SherlockMapFragment extends SherlockFragment 
{ 
private MapView mapView; 

@Override 
public void onActivityCreated(Bundle savedInstanceState) 
{ 
    super.onActivityCreated(savedInstanceState); 

    mapView = (MapView) getView().findViewById(R.id.my_map_id); 
    mapView.onCreate(savedInstanceState); 
} 

@Override 
public void onResume() 
{ 
    super.onResume(); 
    mapView.onResume(); 
} 

@Override 
public void onPause() 
{ 
    super.onPause(); 
    mapView.onPause(); 
} 

@Override 
public void onLowMemory() 
{ 
    super.onLowMemory(); 
    mapView.onLowMemory(); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) 
{ 
    super.onSaveInstanceState(outState); 
    mapView.onSaveInstanceState(outState); 
} 

@Override 
public void onDestroy() 
{ 
    super.onDestroy(); 
    mapView.onDestroy(); 
} 
} 

没有说你绝对必须使用SupportMapFragment。新API的最酷部分之一是能够将MapView用作普通视图。

+0

我试图将其包含在我的xml配置只插入的MapView,但是我不知道是哪个班将其转换为我一直收到'ClassCastException'。请参阅此问题http://stackoverflow.com/questions/13803385/android-google-maps-api-v2-0-including-mapview-in-xml – StuStirling

0

试试这个

公共类BysFragmentActivity扩展SherlockFragmentActivity {

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    Tab tab = actionBar.newTab() 
      .setText("Mapa 1") 
      .setTabListener(new TabListener<BySMapFragment>(this, "mapa", BySMapFragment.class)); 
    actionBar.addTab(tab); 
    tab = actionBar.newTab() 
      .setText("Mapa 2") 
      .setTabListener(new TabListener<BySMapFragment>(this, "mapa", BySMapFragment.class)); 
    actionBar.addTab(tab); 

} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
} 

@Override 
public void onAttachFragment(android.support.v4.app.Fragment fragment) { 
    super.onAttachFragment(fragment); 
} 

public static class TabListener<T extends Fragment> implements ActionBar.TabListener { 

    private Fragment mFragment; 
    private final Activity mActivity; 
    private final String mTag; 
    private final Class<T> mClass; 

    public TabListener(Activity activity, String tag, Class<T> clz) { 
     mActivity = activity; 
     mTag = tag; 
     mClass = clz; 
    } 

    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) {  
     if (mFragment == null) { 
      mFragment = Fragment.instantiate(mActivity, mClass.getName()); 
      ft.replace(android.R.id.content, mFragment, mTag); 
     } else { 
      ft.attach(mFragment); 
     } 
    } 

    @Override 
    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     if (mFragment != null) { 
      ft.detach(mFragment); 
     } 
    } 

    @Override 
    public void onTabReselected(Tab tab, FragmentTransaction ft) { 

    } 

} 

}

相关问题