我有这堂课:
public class Map extends ActionBarActivity implements TabListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_map);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab ttestTab = actionBar.newTab().setText("The test");
ActionBar.Tab chatTab = actionBar.newTab().setText("Chat");
testTab.setTabListener(this);
chatTab.setTabListener(this);
actionBar.addTab(testTab, false);
actionBar.addTab(chatTab, true);
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng mapCenter = new LatLng(41.889, -87.622);
LatLng mapCenter2 = new LatLng(41.889, -85.622);
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.orienteering))
.position(mapCenter)
.flat(true)
);
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.orienteering))
.position(mapCenter2)
.flat(true)
);
// CameraPosition cameraPosition = CameraPosition.builder()
// .target(mapCenter)
// .zoom(13)
// .bearing(90)
// .build();
// map.moveCamera(CameraUpdateFactory.newLatLngZoom(
// new LatLng(-18.142, 178.431), 2));
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Animate the change in camera view over 2 seconds
// map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),
// 2000, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater actionMenue = getMenuInflater();
actionMenue.inflate(R.menu.the_map, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.goBack) {
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_the_map,
container, false);
return rootView;
}
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
int position = tab.getPosition();
switch(position){
case 0:
// Intent displayTheMap = new Intent(this, Map.class);
// startActivity(displayTheMap);
// this.finish();
Toast.makeText(getApplicationContext(),
"case 0", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getApplicationContext(),
"case 1", Toast.LENGTH_SHORT).show();
Intent displayTheMap = new Intent(this, Map.class);
startActivity(displayTheMap);
this.finish();
break;
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
正如您所看到的那样,我已将chatTab
标记为已选中,我在onTabSelected
中有2个案例,这两个案例都显示了一个带有选择消息的祝酒词。
现在我通过以下方式调用我的MainActivity中的上述Map
课程:
Intent displayTheMap = new Intent(this, Map.class);
startActivity(displayTheMap);
并且模拟器一遍又一遍地显示toast消息case 1
。
为什么会这样?为什么它一遍又一遍地循环通过案例1? Isn&#t; t case 0
是否已标记为已选中?我知道我在这里错过了一小部分,但我无法发现它。
答案 0 :(得分:1)
actionBar.addTab(chatTab, true
); startActivity(displayTheMap);
)你是对的,这是一个无限循环。
如果您希望最初选择位置0处的选项卡,则交换第二个参数:
actionBar.addTab(testTab, true);
actionBar.addTab(chatTab, false);