`
dowhathowtodo
  • 浏览: 775345 次
文章分类
社区版块
存档分类
最新评论

导航菜单横向左右滑动并和下方的控件实现联动(有效果图)

 
阅读更多

这个是美团网个人订单的效果,找了很多地方都没找到,自己研究了两天终于弄出来了^_^,有什么问题希望大家指出来,谢谢。



实现原理是上方使用HorizontalScrollView这个可以水平横向拖动的控件,在其中加入了5个RadioButton;下方使用的是ViewPager,里面加入了7个Layout文件,其中第一个和最后一个为空,是为了实现拖到第一个屏幕的时候还能往外拖动的效果。


先看下效果,切换都是带动画效果的,并且点击上面最右边的标签时会自动滚动出后面的标签。


现在看一下代码:

Java代码 复制代码收藏代码
  1. packagecom.zj.horizontalsrollview;
  2. importjava.util.ArrayList;
  3. importandroid.app.Activity;
  4. importandroid.os.Bundle;
  5. importandroid.os.Parcelable;
  6. importandroid.support.v4.view.PagerAdapter;
  7. importandroid.support.v4.view.ViewPager;
  8. importandroid.support.v4.view.ViewPager.OnPageChangeListener;
  9. importandroid.util.Log;
  10. importandroid.view.View;
  11. importandroid.view.View.OnClickListener;
  12. importandroid.view.ViewGroup.MarginLayoutParams;
  13. importandroid.view.animation.Animation;
  14. importandroid.view.animation.AnimationSet;
  15. importandroid.view.animation.AnimationUtils;
  16. importandroid.view.animation.TranslateAnimation;
  17. importandroid.widget.HorizontalScrollView;
  18. importandroid.widget.ImageView;
  19. importandroid.widget.RadioButton;
  20. importandroid.widget.RadioGroup;
  21. importandroid.widget.RadioGroup.OnCheckedChangeListener;
  22. importandroid.widget.RelativeLayout;
  23. importandroid.widget.RelativeLayout.LayoutParams;
  24. /**
  25. *HorizontalScrollView和ViewPager联动效果
  26. *上面为HorizontalScrollView,下面为ViewPager
  27. *@authorzj
  28. *2012-5-23下午1:07:06
  29. */
  30. publicclassMainActivityextendsActivityimplementsOnCheckedChangeListener{
  31. privateRadioGroupmRadioGroup;
  32. privateRadioButtonmRadioButton1;
  33. privateRadioButtonmRadioButton2;
  34. privateRadioButtonmRadioButton3;
  35. privateRadioButtonmRadioButton4;
  36. privateRadioButtonmRadioButton5;
  37. privateImageViewmImageView;
  38. privatefloatmCurrentCheckedRadioLeft;//当前被选中的RadioButton距离左侧的距离
  39. privateHorizontalScrollViewmHorizontalScrollView;//上面的水平滚动控件
  40. privateViewPagermViewPager;//下方的可横向拖动的控件
  41. privateArrayList<View>mViews;//用来存放下方滚动的layout(layout_1,layout_2,layout_3)
  42. @Override
  43. publicvoidonCreate(BundlesavedInstanceState){
  44. super.onCreate(savedInstanceState);
  45. setContentView(R.layout.main);
  46. iniController();
  47. iniListener();
  48. iniVariable();
  49. mRadioButton1.setChecked(true);
  50. mViewPager.setCurrentItem(1);
  51. mCurrentCheckedRadioLeft=getCurrentCheckedRadioLeft();
  52. }
  53. privatevoidiniVariable(){
  54. //TODOAuto-generatedmethodstub
  55. mViews=newArrayList<View>();
  56. mViews.add(getLayoutInflater().inflate(R.layout.layout_0,null));
  57. mViews.add(getLayoutInflater().inflate(R.layout.layout_1,null));
  58. mViews.add(getLayoutInflater().inflate(R.layout.layout_2,null));
  59. mViews.add(getLayoutInflater().inflate(R.layout.layout_3,null));
  60. mViews.add(getLayoutInflater().inflate(R.layout.layout_4,null));
  61. mViews.add(getLayoutInflater().inflate(R.layout.layout_5,null));
  62. mViews.add(getLayoutInflater().inflate(R.layout.layout_0,null));
  63. mViewPager.setAdapter(newMyPagerAdapter());//设置ViewPager的适配器
  64. }
  65. /**
  66. *RadioGroup点击CheckedChanged监听
  67. */
  68. @Override
  69. publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
  70. AnimationSet_AnimationSet=newAnimationSet(true);
  71. TranslateAnimation_TranslateAnimation;
  72. Log.i("zj","checkedid="+checkedId);
  73. if(checkedId==R.id.btn1){
  74. _TranslateAnimation=newTranslateAnimation(mCurrentCheckedRadioLeft,getResources().getDimension(R.dimen.rdo1),0f,0f);
  75. _AnimationSet.addAnimation(_TranslateAnimation);
  76. _AnimationSet.setFillBefore(false);
  77. _AnimationSet.setFillAfter(true);
  78. _AnimationSet.setDuration(100);
  79. /*LayoutParams_LayoutParams1=newLayoutParams(100,4);
  80. _LayoutParams1.setMargins(0,0,0,0);
  81. _LayoutParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);*/
  82. //mImageView.bringToFront();
  83. mImageView.startAnimation(_AnimationSet);//开始上面蓝色横条图片的动画切换
  84. //mImageView.setLayoutParams(_LayoutParams1);
  85. mViewPager.setCurrentItem(1);//让下方ViewPager跟随上面的HorizontalScrollView切换
  86. }elseif(checkedId==R.id.btn2){
  87. _TranslateAnimation=newTranslateAnimation(mCurrentCheckedRadioLeft,getResources().getDimension(R.dimen.rdo2),0f,0f);
  88. _AnimationSet.addAnimation(_TranslateAnimation);
  89. _AnimationSet.setFillBefore(false);
  90. _AnimationSet.setFillAfter(true);
  91. _AnimationSet.setDuration(100);
  92. //mImageView.bringToFront();
  93. mImageView.startAnimation(_AnimationSet);
  94. mViewPager.setCurrentItem(2);
  95. }elseif(checkedId==R.id.btn3){
  96. _TranslateAnimation=newTranslateAnimation(mCurrentCheckedRadioLeft,getResources().getDimension(R.dimen.rdo3),0f,0f);
  97. _AnimationSet.addAnimation(_TranslateAnimation);
  98. _AnimationSet.setFillBefore(false);
  99. _AnimationSet.setFillAfter(true);
  100. _AnimationSet.setDuration(100);
  101. //mImageView.bringToFront();
  102. mImageView.startAnimation(_AnimationSet);
  103. mViewPager.setCurrentItem(3);
  104. }elseif(checkedId==R.id.btn4){
  105. _TranslateAnimation=newTranslateAnimation(mCurrentCheckedRadioLeft,getResources().getDimension(R.dimen.rdo4),0f,0f);
  106. _AnimationSet.addAnimation(_TranslateAnimation);
  107. _AnimationSet.setFillBefore(false);
  108. _AnimationSet.setFillAfter(true);
  109. _AnimationSet.setDuration(100);
  110. //mImageView.bringToFront();
  111. mImageView.startAnimation(_AnimationSet);
  112. mViewPager.setCurrentItem(4);
  113. }elseif(checkedId==R.id.btn5){
  114. _TranslateAnimation=newTranslateAnimation(mCurrentCheckedRadioLeft,getResources().getDimension(R.dimen.rdo5),0f,0f);
  115. _AnimationSet.addAnimation(_TranslateAnimation);
  116. _AnimationSet.setFillBefore(false);
  117. _AnimationSet.setFillAfter(true);
  118. _AnimationSet.setDuration(100);
  119. //mImageView.bringToFront();
  120. mImageView.startAnimation(_AnimationSet);
  121. mViewPager.setCurrentItem(5);
  122. }
  123. mCurrentCheckedRadioLeft=getCurrentCheckedRadioLeft();
  124. Log.i("zj","getCurrentCheckedRadioLeft="+getCurrentCheckedRadioLeft());
  125. Log.i("zj","getDimension="+getResources().getDimension(R.dimen.rdo2));
  126. mHorizontalScrollView.smoothScrollTo((int)mCurrentCheckedRadioLeft-(int)getResources().getDimension(R.dimen.rdo2),0);
  127. }
  128. /**
  129. *获得当前被选中的RadioButton距离左侧的距离
  130. */
  131. privatefloatgetCurrentCheckedRadioLeft(){
  132. //TODOAuto-generatedmethodstub
  133. if(mRadioButton1.isChecked()){
  134. //Log.i("zj","currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo1));
  135. returngetResources().getDimension(R.dimen.rdo1);
  136. }elseif(mRadioButton2.isChecked()){
  137. //Log.i("zj","currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo2));
  138. returngetResources().getDimension(R.dimen.rdo2);
  139. }elseif(mRadioButton3.isChecked()){
  140. //Log.i("zj","currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo3));
  141. returngetResources().getDimension(R.dimen.rdo3);
  142. }elseif(mRadioButton4.isChecked()){
  143. //Log.i("zj","currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo4));
  144. returngetResources().getDimension(R.dimen.rdo4);
  145. }elseif(mRadioButton5.isChecked()){
  146. //Log.i("zj","currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo5));
  147. returngetResources().getDimension(R.dimen.rdo5);
  148. }
  149. return0f;
  150. }
  151. privatevoidiniListener(){
  152. //TODOAuto-generatedmethodstub
  153. mRadioGroup.setOnCheckedChangeListener(this);
  154. mViewPager.setOnPageChangeListener(newMyPagerOnPageChangeListener());
  155. }
  156. privatevoidiniController(){
  157. //TODOAuto-generatedmethodstub
  158. mRadioGroup=(RadioGroup)findViewById(R.id.radioGroup);
  159. mRadioButton1=(RadioButton)findViewById(R.id.btn1);
  160. mRadioButton2=(RadioButton)findViewById(R.id.btn2);
  161. mRadioButton3=(RadioButton)findViewById(R.id.btn3);
  162. mRadioButton4=(RadioButton)findViewById(R.id.btn4);
  163. mRadioButton5=(RadioButton)findViewById(R.id.btn5);
  164. mImageView=(ImageView)findViewById(R.id.img1);
  165. mHorizontalScrollView=(HorizontalScrollView)findViewById(R.id.horizontalScrollView);
  166. mViewPager=(ViewPager)findViewById(R.id.pager);
  167. }
  168. /**
  169. *ViewPager的适配器
  170. *@authorzj
  171. *2012-5-24下午2:26:57
  172. */
  173. privateclassMyPagerAdapterextendsPagerAdapter{
  174. @Override
  175. publicvoiddestroyItem(Viewv,intposition,Objectobj){
  176. //TODOAuto-generatedmethodstub
  177. ((ViewPager)v).removeView(mViews.get(position));
  178. }
  179. @Override
  180. publicvoidfinishUpdate(Viewarg0){
  181. //TODOAuto-generatedmethodstub
  182. }
  183. @Override
  184. publicintgetCount(){
  185. //TODOAuto-generatedmethodstub
  186. returnmViews.size();
  187. }
  188. @Override
  189. publicObjectinstantiateItem(Viewv,intposition){
  190. ((ViewPager)v).addView(mViews.get(position));
  191. returnmViews.get(position);
  192. }
  193. @Override
  194. publicbooleanisViewFromObject(Viewarg0,Objectarg1){
  195. //TODOAuto-generatedmethodstub
  196. returnarg0==arg1;
  197. }
  198. @Override
  199. publicvoidrestoreState(Parcelablearg0,ClassLoaderarg1){
  200. //TODOAuto-generatedmethodstub
  201. }
  202. @Override
  203. publicParcelablesaveState(){
  204. //TODOAuto-generatedmethodstub
  205. returnnull;
  206. }
  207. @Override
  208. publicvoidstartUpdate(Viewarg0){
  209. //TODOAuto-generatedmethodstub
  210. }
  211. }
  212. /**
  213. *ViewPager的PageChangeListener(页面改变的监听器)
  214. *@authorzj
  215. *2012-5-24下午3:14:27
  216. */
  217. privateclassMyPagerOnPageChangeListenerimplementsOnPageChangeListener{
  218. @Override
  219. publicvoidonPageScrollStateChanged(intarg0){
  220. //TODOAuto-generatedmethodstub
  221. }
  222. @Override
  223. publicvoidonPageScrolled(intarg0,floatarg1,intarg2){
  224. //TODOAuto-generatedmethodstub
  225. }
  226. /**
  227. *滑动ViewPager的时候,让上方的HorizontalScrollView自动切换
  228. */
  229. @Override
  230. publicvoidonPageSelected(intposition){
  231. //TODOAuto-generatedmethodstub
  232. //Log.i("zj","position="+position);
  233. if(position==0){
  234. mViewPager.setCurrentItem(1);
  235. }elseif(position==1){
  236. mRadioButton1.performClick();
  237. }elseif(position==2){
  238. mRadioButton2.performClick();
  239. }elseif(position==3){
  240. mRadioButton3.performClick();
  241. }elseif(position==4){
  242. mRadioButton4.performClick();
  243. }elseif(position==5){
  244. mRadioButton5.performClick();
  245. }elseif(position==6){
  246. mViewPager.setCurrentItem(5);
  247. }
  248. }
  249. }
  250. }

XML文件:

Xml代码 复制代码收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <HorizontalScrollView
  8. android:layout_width="match_parent"
  9. android:layout_height="50dp"
  10. android:fadingEdge="@null"
  11. android:scrollbars="none"
  12. android:background="#555555"
  13. android:id="@+id/horizontalScrollView"
  14. >
  15. <RelativeLayout
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent"
  18. android:background="#33b5e5"
  19. >
  20. <RadioGroup
  21. android:id="@+id/radioGroup"
  22. android:layout_width="fill_parent"
  23. android:layout_height="49dp"
  24. android:orientation="horizontal"
  25. android:layout_alignParentTop="true"
  26. >
  27. <RadioButton
  28. style="@style/radioButton"
  29. android:text="one"
  30. android:id="@+id/btn1"
  31. />
  32. <RadioButton
  33. style="@style/radioButton"
  34. android:text="two"
  35. android:id="@+id/btn2"
  36. />
  37. <RadioButton
  38. style="@style/radioButton"
  39. android:text="three"
  40. android:id="@+id/btn3"
  41. />
  42. <RadioButton
  43. style="@style/radioButton"
  44. android:text="four"
  45. android:id="@+id/btn4"
  46. />
  47. <RadioButton
  48. style="@style/radioButton"
  49. android:text="five"
  50. android:id="@+id/btn5"
  51. />
  52. </RadioGroup>
  53. <ImageView
  54. android:id="@+id/img1"
  55. android:layout_width="100dp"
  56. android:layout_height="4dp"
  57. android:background="#33b5e5"
  58. android:layout_alignParentBottom="true"
  59. />
  60. </RelativeLayout>
  61. </HorizontalScrollView>
  62. <android.support.v4.view.ViewPager
  63. android:id="@+id/pager"
  64. android:layout_width="fill_parent"
  65. android:layout_height="fill_parent"
  66. />
  67. </LinearLayout>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics