DetailActivity.java 8.8 KB

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