DetailActivity.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package com.usai.apex;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import com.usai.util.commonUtil;
  7. import android.R.integer;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.KeyEvent;
  11. import android.view.Menu;
  12. import android.view.MenuInflater;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.FrameLayout;
  16. import android.widget.TabHost;
  17. import android.support.v4.app.Fragment;
  18. import android.support.v4.app.FragmentActivity;
  19. import android.support.v4.app.FragmentTransaction;
  20. import android.support.v4.app.NavUtils;
  21. public class DetailActivity extends FragmentActivity implements
  22. TabHost.TabContentFactory
  23. {
  24. // int radomid = 1 + (int) (Math.random() * 15);
  25. TabHost mTabHost;
  26. LinkedHashMap<String, Integer> tabmap = new LinkedHashMap<String, Integer>();
  27. HashMap<String,String> contentmap = new HashMap<String ,String>();
  28. // HashMap<String, DetailFragment> fragments = new HashMap<String,
  29. // DetailFragment>();
  30. String function_name;
  31. String _id;
  32. String cargo_criterion;
  33. int criterion_type;
  34. public void save_content(String which,String content)
  35. {
  36. contentmap.put(which, content);
  37. }
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState)
  40. {
  41. Log.e("DetailActivity", "onCreate");
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.activity_detail);
  44. List<Fragment> fragments = getSupportFragmentManager().getFragments();
  45. if (fragments != null)
  46. {
  47. Log.e("DetailActivity", "fragments count=" + fragments.size());
  48. // for (int i = 0; i < fragments.size(); i++)
  49. // Log.e("DetailFragment", "fragment name="
  50. // + fragments.get(i).getTag());
  51. }
  52. if (savedInstanceState != null)
  53. {
  54. tabmap = (LinkedHashMap<String, Integer>) savedInstanceState
  55. .getSerializable("tabmap");
  56. contentmap = (HashMap<String, String>) savedInstanceState.getSerializable("contentmap");
  57. }
  58. else
  59. {
  60. int count = getIntent().getIntExtra("actions_count", 0);
  61. for (int i = 0; i < count; i++)
  62. {
  63. int vid = commonUtil.generateViewId();
  64. tabmap.put(getIntent().getStringExtra("action" + i), vid);
  65. }
  66. }
  67. function_name = getIntent().getStringExtra("function_name");
  68. if (function_name.equals("Cargo Tracking"))
  69. {
  70. criterion_type = getIntent().getIntExtra("criterion_type", 0);
  71. cargo_criterion = getIntent().getStringExtra("cargo_criterion");
  72. }
  73. _id = getIntent().getStringExtra("_id");
  74. setupTabs();
  75. }
  76. // @Override
  77. // protected void onSaveInstanceState(Bundle outState)
  78. // {
  79. // Iterator<String> iter=fragments.keySet().iterator();
  80. //
  81. // while (iter.hasNext())
  82. // {
  83. // String tag = (String) iter.next();
  84. // getSupportFragmentManager().putFragment(outState,tag,fragments.get(tag));
  85. //
  86. // }
  87. // outState.putStringArray("tags", fragments.keySet().toArray(new
  88. // String[0]));
  89. // Log.d("onSaveInstanceState","save fragments" +
  90. // fragments.keySet().size());
  91. // super.onSaveInstanceState(outState);
  92. // }
  93. //
  94. // @Override
  95. // protected void onRestoreInstanceState(Bundle savedInstanceState)
  96. // {
  97. // String [] tags =savedInstanceState.getStringArray("tags");
  98. // Log.d("onRestoreInstanceState","load fragments" + tags.length);
  99. // for(int i=0;i<tags.length;i++)
  100. // {
  101. // Log.d("onRestoreInstanceState","load fragment" + tags[i]);
  102. // fragments.put(tags[i],(DetailFragment)
  103. // getSupportFragmentManager().getFragment(savedInstanceState,tags[i]));
  104. // }
  105. //
  106. // super.onRestoreInstanceState(savedInstanceState);
  107. // }
  108. /** {@inheritDoc} */
  109. public View createTabContent(String tag)
  110. {
  111. FrameLayout fl = new FrameLayout(this);
  112. fl.setId(tabmap.get(tag));
  113. Fragment f;
  114. Bundle bundle = new Bundle();
  115. if (function_name.equals("Cargo Tracking"))
  116. {
  117. if (criterion_type == 0)
  118. bundle.putString("hbol", cargo_criterion);
  119. else
  120. bundle.putString("container_no", cargo_criterion);
  121. }
  122. bundle.putString("action_type", tag);
  123. bundle.putString("id", _id);
  124. bundle.putString("module_name", function_name);
  125. // Log.e("findFragmentByTag", "tag=" + tag + ", isnull= "
  126. // + (getSupportFragmentManager().findFragmentByTag(tag) == null));
  127. f = getSupportFragmentManager().findFragmentById(tabmap.get(tag));
  128. if (f == null)
  129. {
  130. Log.e("createTabContent", "create fragment" + tag);
  131. f = new DetailFragment();
  132. f.setArguments(bundle);
  133. }
  134. ((DetailFragment)f).set_content( contentmap.get(tag));
  135. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  136. ft.replace(fl.getId(), f);
  137. ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  138. ft.addToBackStack(tag);
  139. ft.commit();
  140. return fl;
  141. }
  142. @Override
  143. protected void onSaveInstanceState(Bundle outState)
  144. {
  145. outState.putSerializable("tabmap", tabmap);
  146. outState.putSerializable("contentmap", contentmap);
  147. super.onSaveInstanceState(outState);
  148. }
  149. // TabHost mTabHost;
  150. // private FrameLayout mTabContent;
  151. @Override
  152. public boolean onKeyDown(int keyCode, KeyEvent event)
  153. {
  154. if (keyCode == KeyEvent.KEYCODE_BACK)
  155. {
  156. // Intent myIntent = new Intent();
  157. // myIntent = new Intent(EditActivity.this, tabActivity.class);
  158. // startActivity(myIntent);
  159. finish();
  160. return true;
  161. }
  162. return super.onKeyDown(keyCode, event);
  163. }
  164. //
  165. // @Override
  166. // protected void onCreate(Bundle savedInstanceState)
  167. // {
  168. // super.onCreate(savedInstanceState);
  169. // setContentView(R.layout.activity_detail);
  170. // // Show the Up button in the action bar.
  171. // setupActionBar();
  172. // setupTabs();
  173. // }
  174. //
  175. // /**
  176. // * Set up the {@link android.app.ActionBar}.
  177. // */
  178. // private void setupActionBar()
  179. // {
  180. //
  181. // getActionBar().setDisplayHomeAsUpEnabled(true);
  182. //
  183. // }
  184. //
  185. // // private View createTabIndicatorView(ViewGroup parent, CharSequence
  186. // label, Drawable icon) {
  187. // // final LayoutInflater inflater = LayoutInflater.from(this);
  188. // // final View tabIndicator = inflater.inflate(R.layout.tab_indicator,
  189. // parent, false);
  190. // //
  191. // //// final TextView tv = (TextView)
  192. // tabIndicator.findViewById(R.id.tab_title);
  193. // //// tv.setText(label);
  194. // ////
  195. // //// final ImageView iconView = (ImageView)
  196. // tabIndicator.findViewById(R.id.tab_icon);
  197. // //// iconView.setImageDrawable(icon);
  198. // //
  199. // // return tabIndicator;
  200. // // }
  201. //
  202. // @Override
  203. // public View createTabContent(String tag)
  204. // {
  205. // final TextView tv = new TextView(this);
  206. // tv.setText("Content for tab with tag " + tag);
  207. // return tv;
  208. // }
  209. private void setupTabs()
  210. {
  211. mTabHost = (TabHost) findViewById(R.id.tabhost);
  212. // mTabContent = (FrameLayout) findViewById(android.R.id.tabcontent);
  213. // FrameLayout tab1 = new FrameLayout(this);
  214. mTabHost.setup();
  215. Iterator<String> iter = tabmap.keySet().iterator();
  216. while (iter.hasNext())
  217. {
  218. String name = (String) iter.next();
  219. mTabHost.addTab(mTabHost.newTabSpec(name).setIndicator(name)
  220. .setContent(this));
  221. // String val = (String) tabmap.get(field);
  222. // parms.putString(field, val);
  223. // // Cursor c = (Cursor) m_listadapter.getItem(key);
  224. // Log.d("@@@@@@@@@", field + " : " + val);
  225. }
  226. // for (int i=1; i <= tabmap.size(); i++) {
  227. // String name = tabmap.keySet().iterator().;
  228. // mTabHost.addTab(mTabHost.newTabSpec(name)
  229. // .setIndicator(name)
  230. // .setContent(this));
  231. // }
  232. // // if(true)
  233. // // return;
  234. //
  235. // // View tab1 = createTabIndicatorView(mTabHost.getTabWidget(),
  236. // // "Login",null);
  237. // // tabhost.addTab(
  238. // // tabhost.newTabSpec("tab" + i).setIndicator(tab)
  239. // // .setContent(Mytabfirst.this)); tabhost.setCurrentTab(i);
  240. // // i++; tab = null;
  241. //
  242. // // // ��ɵײ��Զ�����ʽ�İ�ť
  243. // // String[] title = new String[] { "Login", "Not login" };
  244. // // int[] tabIds = new int[] { R.id.tab1, R.id.tab2 };
  245. // mTabHost.addTab(mTabHost.newTabSpec("Login").setIndicator("Login")
  246. // .setContent(this));
  247. // //
  248. // mTabHost.addTab(mTabHost.newTabSpec("Direct Tracking").setIndicator("Direct Tracking").setContent(R.id.tab2));
  249. // mTabHost.addTab(mTabHost.newTabSpec("Service Location")
  250. // .setIndicator("Service Location").setContent(this));
  251. // FragmentManager manager = this.getFragmentManager();
  252. // Instantiate a new fragment.
  253. // Fragment loginFragment = new LoginFragment();
  254. // // Fragment dtFragement = new DirectTrackingFragment();
  255. // Fragment slFragment = new ServiceLocationFragment();
  256. // // Add the fragment to the activity, pushing this transaction
  257. // // on to the back stack.
  258. //
  259. // FragmentTransaction ft =
  260. // getSupportFragmentManager().beginTransaction();
  261. // ft.replace(mTabHost.getTabContentView().getChildAt(0).getId(),
  262. // loginFragment);
  263. // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  264. // ft.addToBackStack("Login");
  265. //
  266. // // ft.replace(R.id.tab2, dtFragement);
  267. // // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  268. // // ft.addToBackStack("Tracking");
  269. //
  270. // ft.replace(R.id.tab3, (Fragment) slFragment);
  271. // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  272. // ft.addToBackStack("Location");
  273. //
  274. // ft.commit();
  275. // if (manager.findFragmentByTag(tabId) == null)
  276. // {
  277. // FragmentTransaction trans = manager.beginTransaction();
  278. // trans.replace(contentViewID, frag, tabId);
  279. // trans.commit();
  280. // }
  281. // for (int i = 0; i < title.length; i++) {
  282. // Button button = new Button(this);
  283. // button.setText(title[i]);
  284. // button.setBackgroundDrawable(this.getResources().getDrawable(
  285. // R.drawable.tab_lable)); //�Զ��尴ť��ʽ
  286. // mTabHost.addTab(mTabHost.newTabSpec(title[i]).setIndicator(button)
  287. // .setContent(tabIds[i]));
  288. // }
  289. // mTabHost.setOnTabChangedListener(this);
  290. }
  291. // @Override
  292. // public boolean onCreateOptionsMenu(Menu menu)
  293. // {
  294. // // Inflate the menu; this adds items to the action bar if it is present.
  295. // getMenuInflater().inflate(R.menu.detail, menu);
  296. // return true;
  297. // }
  298. //
  299. // @Override
  300. // public boolean onOptionsItemSelected(MenuItem item)
  301. // {
  302. // switch (item.getItemId())
  303. // {
  304. // case android.R.id.home:
  305. // // This ID represents the Home or Up button. In the case of this
  306. // // activity, the Up button is shown. Use NavUtils to allow users
  307. // // to navigate up one level in the application structure. For
  308. // // more details, see the Navigation pattern on Android Design:
  309. // //
  310. // //
  311. // // http: //
  312. // // developer.android.com/design/patterns/navigation.html#up-vs-back
  313. // //
  314. // NavUtils.navigateUpFromSameTask(this);
  315. // return true;
  316. // }
  317. // return super.onOptionsItemSelected(item);
  318. // }
  319. }