PageRenderTime 405ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/playcar/activity/CarNearActivity.java

https://gitlab.com/Er_Hei/PlayCar
Java | 260 lines | 176 code | 56 blank | 28 comment | 5 complexity | e7013809a029c7d982d21fd222f99f42 MD5 | raw file
  1. package com.playcar.activity;
  2. import android.app.ActivityManager;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.os.CountDownTimer;
  7. import android.os.Handler;
  8. import android.support.v4.app.Fragment;
  9. import android.support.v4.app.FragmentActivity;
  10. import android.support.v4.app.FragmentManager;
  11. import android.support.v4.app.FragmentTransaction;
  12. import android.support.v4.view.PagerAdapter;
  13. import android.support.v4.view.ViewPager;
  14. import android.text.Editable;
  15. import android.text.TextUtils;
  16. import android.text.TextWatcher;
  17. import android.view.KeyEvent;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.view.animation.AccelerateDecelerateInterpolator;
  21. import android.view.animation.TranslateAnimation;
  22. import android.widget.Button;
  23. import android.widget.EditText;
  24. import android.widget.ImageView;
  25. import android.widget.LinearLayout;
  26. import android.widget.RelativeLayout;
  27. import android.widget.Toast;
  28. import com.alibaba.fastjson.JSON;
  29. import com.alibaba.fastjson.JSONObject;
  30. import com.playcar.R;
  31. import com.playcar.fragment.CarNearFriendsFragment;
  32. import com.playcar.fragment.CarNearGroupFragment;
  33. import com.playcar.fragment.CarNearTrandsFragment;
  34. import com.playcar.view.MainViewPager;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. import java.util.Set;
  38. /**
  39. * Created by Aure on 15/9/23.
  40. */
  41. public class CarNearActivity extends CarBaseActivity {
  42. public MainViewPager viewPager;
  43. public List<Fragment> fragments = new ArrayList<Fragment>();
  44. private ImageView mTabImg;
  45. private int zero = 0;// 动画图片偏移量
  46. private int one;//单个水平动画位移
  47. private int two;
  48. // private int three;
  49. private FragmentManager fragmentManager;
  50. // private int displayWidth, displayHeight;
  51. // 底部线条移动初始位置
  52. private int mUnderLineFromX = 0;
  53. private int mUnderLineWidth;
  54. private int mScreenWidth;
  55. private RelativeLayout mTab1,mTab2,mTab3;
  56. // private NormalTopBar titleBar;
  57. private String activityName = "CarNearActivity";
  58. private LinearLayout fartherView;
  59. private int moreWidth = 0;
  60. @Override
  61. public void onCreate(Bundle savedInstanceState) {
  62. super.onCreate(savedInstanceState);
  63. setContentView(R.layout.car_near_activity);
  64. // three = one * 3;
  65. initView();
  66. }
  67. private void initView () {
  68. // titleBar = (NormalTopBar) findViewById(R.id.order_list_top_bar);
  69. // titleBar.setTitle("订单管理");
  70. // titleBar.setBackTextVisibility(false);
  71. // titleBar.setBtSettingVisibility(false);
  72. // titleBar.setOnBackListener(this);
  73. moreWidth = Dp2Px(this,180);
  74. mScreenWidth = getResources().getDisplayMetrics().widthPixels;
  75. int tuW = mScreenWidth - moreWidth;
  76. fartherView = (LinearLayout) findViewById(R.id.tab_farther);
  77. int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
  78. int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
  79. fartherView.measure(w, h);
  80. int height =fartherView.getMeasuredHeight();
  81. int width =fartherView.getMeasuredWidth();
  82. mUnderLineWidth = tuW / 3;
  83. one = mUnderLineWidth; //设置水平动画平移大小
  84. two = one * 2;
  85. mTab1 = (RelativeLayout) findViewById(R.id.all_order);
  86. mTab2 = (RelativeLayout) findViewById(R.id.payed_order);
  87. mTab3 = (RelativeLayout) findViewById(R.id.checked_order);
  88. mTabImg = (ImageView) findViewById(R.id.img_tab_now);
  89. mTab1.setOnClickListener(new MyOnClickListener(0));
  90. mTab2.setOnClickListener(new MyOnClickListener(1));
  91. mTab3.setOnClickListener(new MyOnClickListener(2));
  92. ViewGroup.LayoutParams laParams=(ViewGroup.LayoutParams)mTabImg.getLayoutParams();
  93. laParams.height=4;
  94. laParams.width=mUnderLineWidth;
  95. mTabImg.setLayoutParams(laParams);
  96. mTabImg.setVisibility(View.VISIBLE);
  97. // int[] location = new int[2];
  98. // fartherView.getLocationOnScreen(location);
  99. // int x = location[0];
  100. // int y = location[1];
  101. // mUnderLineFromX = x;
  102. fragments.add(new CarNearTrandsFragment());
  103. fragments.add(new CarNearFriendsFragment());
  104. fragments.add(new CarNearGroupFragment());
  105. // fragments.add(new FinishOrderListFragment());
  106. fragmentManager = this.getSupportFragmentManager();
  107. viewPager = (MainViewPager) findViewById(R.id.viewPager);
  108. //viewPager.setSlipping(false);//设置ViewPager是否可以滑动
  109. viewPager.setOffscreenPageLimit(3);
  110. viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
  111. viewPager.setAdapter(new MyPagerAdapter());
  112. viewPager.setCurrentItem(1);
  113. }
  114. public static int Dp2Px(Context context, float dp) {
  115. final float scale = context.getResources().getDisplayMetrics().density;
  116. return (int) (dp * scale + 0.5f);
  117. }
  118. private void doUnderLineAnimation(int index) {
  119. TranslateAnimation animation = new TranslateAnimation(mUnderLineFromX,
  120. index * mUnderLineWidth, 0, 0);
  121. animation.setInterpolator(new AccelerateDecelerateInterpolator());
  122. animation.setFillAfter(true);
  123. animation.setDuration(150);
  124. mTabImg.startAnimation(animation);
  125. mUnderLineFromX = index * mUnderLineWidth;
  126. }
  127. public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
  128. @Override
  129. public void onPageSelected(int arg0) {
  130. switch (arg0) {
  131. case 0:
  132. doUnderLineAnimation(0);
  133. break;
  134. case 1:
  135. doUnderLineAnimation(1);
  136. break;
  137. case 2:
  138. doUnderLineAnimation(2);
  139. break;
  140. }
  141. }
  142. @Override
  143. public void onPageScrolled(int arg0, float arg1, int arg2) {
  144. }
  145. @Override
  146. public void onPageScrollStateChanged(int arg0) {
  147. }
  148. }
  149. private class MyOnClickListener implements View.OnClickListener {
  150. private int index = 0;
  151. public MyOnClickListener(int i) {
  152. index = i;
  153. }
  154. @Override
  155. public void onClick(View v) {
  156. viewPager.setCurrentItem(index);
  157. }
  158. };
  159. private class MyPagerAdapter extends PagerAdapter {
  160. @Override
  161. public boolean isViewFromObject(View arg0, Object arg1) {
  162. return arg0 == arg1;
  163. }
  164. @Override
  165. public int getCount() {
  166. return fragments.size();
  167. }
  168. @Override
  169. public void destroyItem(View container, int position, Object object) {
  170. ((ViewPager) container).removeView(fragments.get(position)
  171. .getView());
  172. }
  173. @Override
  174. public Object instantiateItem(ViewGroup container, int position) {
  175. Fragment fragment = fragments.get(position);
  176. if (!fragment.isAdded()) { // 如果fragment还没有added
  177. FragmentTransaction ft = fragmentManager.beginTransaction();
  178. ft.add(fragment, fragment.getClass().getSimpleName());
  179. ft.commit();
  180. /**
  181. * 在用FragmentTransaction.commit()方法提交FragmentTransaction对象后
  182. * 会在进程的主线程中,用异步的方式来执行。
  183. * 如果想要立即执行这个等待中的操作,就要调用这个方法(只能在主线程中调用)。
  184. * 要注意的是,所有的回调和相关的行为都会在这个调用中被执行完成,因此要仔细确认这个方法的调用位置。
  185. */
  186. fragmentManager.executePendingTransactions();
  187. }
  188. if (fragment.getView().getParent() == null) {
  189. container.addView(fragment.getView()); // 为viewpager增加布局
  190. }
  191. return fragment.getView();
  192. }
  193. };
  194. @Override
  195. protected void onResume() {
  196. super.onResume();
  197. //友盟页面统计
  198. }
  199. @Override
  200. protected void onPause() {
  201. super.onPause();
  202. //友盟页面统计
  203. }
  204. }