MainActivity.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package com.Intelligent.FamilyU.model.main;
  2. import android.content.Intent;
  3. import android.support.v4.app.Fragment;
  4. import android.support.v4.app.FragmentManager;
  5. import android.support.v4.view.GravityCompat;
  6. import android.support.v4.widget.DrawerLayout;
  7. import android.support.v7.widget.RecyclerView;
  8. import android.view.KeyEvent;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.RelativeLayout;
  12. import android.widget.TextView;
  13. import com.Intelligent.FamilyU.R;
  14. import com.Intelligent.FamilyU.base.BaseFragmentActivity;
  15. import com.Intelligent.FamilyU.base.BasePagerAdapter;
  16. import com.Intelligent.FamilyU.manager.ActivityStackManager;
  17. import com.Intelligent.FamilyU.model.home.fragment.HomeFurnishingFragment;
  18. import com.Intelligent.FamilyU.model.message.activity.MessageListActivity;
  19. import com.Intelligent.FamilyU.model.my.fragment.HomeMyFragment;
  20. import com.Intelligent.FamilyU.model.plugin.activity.PluginActivity;
  21. import com.Intelligent.FamilyU.model.scene.fragment.HomeSceneMainFragment;
  22. import com.Intelligent.FamilyU.model.shopping.fragment.HomeShopingFragment;
  23. import com.Intelligent.FamilyU.model.user.activity.UserListActivity;
  24. import com.Intelligent.FamilyU.utils.ToastUtils;
  25. import com.Intelligent.FamilyU.view.CustomViewPager;
  26. import java.lang.ref.SoftReference;
  27. import java.util.ArrayList;
  28. import butterknife.BindView;
  29. import butterknife.OnClick;
  30. /**
  31. * 首頁
  32. */
  33. public class MainActivity extends BaseFragmentActivity {
  34. @BindView(R.id.view_pager)
  35. CustomViewPager viewPager;
  36. @BindView(R.id.page_title)
  37. TextView titleTv;
  38. @BindView(R.id.page_right_tv)
  39. TextView pageRightTv;
  40. @BindView(R.id.page_right_rl)
  41. RelativeLayout pageRightRl;
  42. @BindView(R.id.page_cancle)
  43. TextView pageCancle;
  44. // @BindView(R.id.drawer_layout)
  45. // DrawerLayout drawerLayout;
  46. // @BindView(R.id.header_rl)
  47. // RelativeLayout headerRl;
  48. // @BindView(R.id.user_list_number)
  49. // TextView userNumberTv;
  50. // @BindView(R.id.plugn_list_number)
  51. // TextView plugnNumberTv;
  52. private BasePagerAdapter mPagerAdapter;
  53. private ArrayList<Fragment> mFragmentList = new ArrayList<>();
  54. private SoftReference<Fragment> mHomeFurnishingFragment = null;
  55. private SoftReference<Fragment> mHomeSceneFragment = null;
  56. private SoftReference<Fragment> mHomeShopingFragment = null;
  57. private SoftReference<Fragment> mHomeMyFragment = null;
  58. private FragmentManager mFragmentManager = null;
  59. private Fragment currentFragment = null;
  60. private long clickTime = 0; // 第一次点击的时间
  61. // private MainPluginListRecyclerViewAdapter adapter;
  62. // private List<HashMap<String, String>> mlist = new ArrayList<HashMap<String, String>>();
  63. // private LinearLayoutManager mLayoutManager;
  64. // private String[] nameStrings = null;
  65. //
  66. // private MainPluginListRecyclerViewAdapter.OnItemClickListener mOnItemClickListener = new MainPluginListRecyclerViewAdapter.OnItemClickListener() {
  67. // @Override
  68. // public void onItemClick(int postion) {
  69. // //dapter.notifyDataSetChanged();
  70. // }
  71. //
  72. // @Override
  73. // public void onItemLongClick(int postion) {
  74. //
  75. // }
  76. // };
  77. @Override
  78. protected int getContentViewId() {
  79. return R.layout.drawerlayout_main;
  80. }
  81. @Override
  82. protected void initBundleData() {
  83. }
  84. @Override
  85. protected void initView() {
  86. //initNavigationView();
  87. //initAdapter();
  88. }
  89. @Override
  90. protected void initData() {
  91. mHomeFurnishingFragment = new SoftReference(new HomeFurnishingFragment());
  92. mHomeSceneFragment = new SoftReference(new HomeSceneMainFragment());
  93. mHomeShopingFragment = new SoftReference(new HomeShopingFragment());
  94. mHomeMyFragment = new SoftReference(new HomeMyFragment());
  95. mFragmentList.add(mHomeFurnishingFragment.get());
  96. mFragmentList.add(mHomeSceneFragment.get());
  97. mFragmentList.add(mHomeShopingFragment.get());
  98. mFragmentList.add(mHomeMyFragment.get());
  99. mFragmentManager = getSupportFragmentManager();
  100. mPagerAdapter = new BasePagerAdapter(mFragmentManager, mFragmentList);
  101. //设置Adapter
  102. viewPager.setAdapter(mPagerAdapter);
  103. viewPager.setScanScroll(false);
  104. viewPager.setCurrentItem(0);
  105. currentFragment = mFragmentList.get(0);
  106. titleTv.setText(R.string.home_furnishing);
  107. pageRightTv.setBackgroundResource(R.mipmap.icon_message);
  108. pageRightRl.setVisibility(View.VISIBLE);
  109. // pageCancle.setBackgroundResource(R.mipmap.dlna_head_menu);
  110. // userNumberTv.setText(mContext.getResources().getString(R.string.home_individual, 5));
  111. // plugnNumberTv.setText(mContext.getResources().getString(R.string.home_individual, 3));
  112. }
  113. // private void initNavigationView() {
  114. // //关闭手势滑动
  115. // drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
  116. // drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
  117. // @Override
  118. // public void onDrawerSlide(View view, float v) {
  119. //
  120. // }
  121. //
  122. // @Override
  123. // public void onDrawerOpened(View view) {
  124. //
  125. // }
  126. //
  127. // @Override
  128. // public void onDrawerClosed(View view) {
  129. // }
  130. //
  131. // @Override
  132. // public void onDrawerStateChanged(int i) {
  133. //
  134. // }
  135. // });
  136. // }
  137. // @Override
  138. // public boolean onOptionsItemSelected(MenuItem item) {
  139. // if (item.getItemId() == android.R.id.home) {
  140. // //打开抽屉侧滑菜单
  141. // drawerLayout.openDrawer(GravityCompat.START);
  142. // }
  143. // return super.onOptionsItemSelected(item);
  144. // }
  145. @OnClick({R.id.main_page, R.id.main_scene, R.id.main_shopping, R.id.main_my, R.id.page_back, R.id.page_right_rl, R.id.user_list_ll, R.id.plugn_list_ll})
  146. public void onClick(View v) {
  147. Fragment mFragment = null;
  148. switch (v.getId()) {
  149. case R.id.main_page:
  150. mFragment = mHomeFurnishingFragment.get();
  151. if (!mFragment.isAdded()) {
  152. mFragmentList.set(0, mFragment);
  153. }
  154. currentFragment = switchFragment(mFragmentManager, currentFragment, mFragment);
  155. viewPager.setCurrentItem(0);
  156. titleTv.setText(R.string.home_furnishing);
  157. pageRightRl.setVisibility(View.VISIBLE);
  158. pageCancle.setVisibility(View.VISIBLE);
  159. break;
  160. case R.id.main_scene:
  161. mFragment = mHomeSceneFragment.get();
  162. if (!mFragment.isAdded()) {
  163. mFragmentList.set(1, mFragment);
  164. }
  165. currentFragment = switchFragment(mFragmentManager, currentFragment, mFragment);
  166. viewPager.setCurrentItem(1);
  167. titleTv.setText(R.string.home_scene);
  168. pageRightRl.setVisibility(View.GONE);
  169. break;
  170. case R.id.main_shopping:
  171. mFragment = mHomeShopingFragment.get();
  172. if (!mFragment.isAdded()) {
  173. mFragmentList.set(2, mFragment);
  174. }
  175. currentFragment = switchFragment(mFragmentManager, currentFragment, mFragment);
  176. viewPager.setCurrentItem(2);
  177. titleTv.setText(R.string.home_shopping);
  178. pageRightRl.setVisibility(View.GONE);
  179. break;
  180. case R.id.main_my:
  181. mFragment = mHomeMyFragment.get();
  182. if (!mFragment.isAdded()) {
  183. mFragmentList.set(3, mFragment);
  184. }
  185. currentFragment = switchFragment(mFragmentManager, currentFragment, mFragment);
  186. viewPager.setCurrentItem(3);
  187. titleTv.setText(R.string.home_my);
  188. pageRightRl.setVisibility(View.GONE);
  189. break;
  190. case R.id.page_right_rl:
  191. startActivity(new Intent(mContext, MessageListActivity.class));
  192. break;
  193. case R.id.page_back:
  194. ActivityStackManager.getManager().exitApp(mContext);
  195. // drawerLayout.openDrawer(headerRl);
  196. break;
  197. }
  198. }
  199. @Override
  200. public void onBackPressed() {
  201. exit();
  202. }
  203. @Override
  204. public boolean onKeyDown(int keyCode, KeyEvent event) {
  205. // 是否触发按键为back键
  206. if (keyCode == KeyEvent.KEYCODE_BACK) {
  207. onBackPressed();
  208. return true;
  209. } else {
  210. // 如果不是back键正常响应
  211. return super.onKeyDown(keyCode, event);
  212. }
  213. }
  214. private void exit() {
  215. if ((System.currentTimeMillis() - clickTime) > 2000) {
  216. ToastUtils.showToast(this, getResources().getString(R.string.home_exits));
  217. clickTime = System.currentTimeMillis();
  218. } else {
  219. ActivityStackManager.getManager().exitApp(mContext);
  220. }
  221. }
  222. @Override
  223. protected void onDestroy() {
  224. super.onDestroy();
  225. ActivityStackManager.getManager().exitApp(mContext);
  226. }
  227. }