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

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



实现原理是上方使用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. privateArrayListmViews;//用来存放下方滚动的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();
  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. }
package com.zj.horizontalsrollview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
/**
 * HorizontalScrollView和ViewPager联动效果
 * 上面为HorizontalScrollView,下面为ViewPager
 * @author zj
 * 2012-5-23 下午1:07:06
 */
public class MainActivity extends Activity implements OnCheckedChangeListener{
	private RadioGroup mRadioGroup;
	private RadioButton mRadioButton1;
	private RadioButton mRadioButton2;
	private RadioButton mRadioButton3;
	private RadioButton mRadioButton4;
	private RadioButton mRadioButton5;
	private ImageView mImageView;
	private float mCurrentCheckedRadioLeft;//当前被选中的RadioButton距离左侧的距离
	private HorizontalScrollView mHorizontalScrollView;//上面的水平滚动控件
	private ViewPager mViewPager;	//下方的可横向拖动的控件
	private ArrayList mViews;//用来存放下方滚动的layout(layout_1,layout_2,layout_3)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        iniController();
        iniListener();
        iniVariable();
        
        mRadioButton1.setChecked(true);
        mViewPager.setCurrentItem(1);
        mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();
        
    }
    
    private void iniVariable() {
		// TODO Auto-generated method stub
    	mViews = new ArrayList();
    	mViews.add(getLayoutInflater().inflate(R.layout.layout_0, null));
    	mViews.add(getLayoutInflater().inflate(R.layout.layout_1, null));
    	mViews.add(getLayoutInflater().inflate(R.layout.layout_2, null));
    	mViews.add(getLayoutInflater().inflate(R.layout.layout_3, null));
    	mViews.add(getLayoutInflater().inflate(R.layout.layout_4, null));
    	mViews.add(getLayoutInflater().inflate(R.layout.layout_5, null));
    	mViews.add(getLayoutInflater().inflate(R.layout.layout_0, null));
    	
    	mViewPager.setAdapter(new MyPagerAdapter());//设置ViewPager的适配器
	}
    
    /**
	 * RadioGroup点击CheckedChanged监听
	 */
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		
		AnimationSet _AnimationSet = new AnimationSet(true);
		TranslateAnimation _TranslateAnimation;
		
		Log.i("zj", "checkedid="+checkedId);
		if (checkedId == R.id.btn1) {
			_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo1), 0f, 0f);
			_AnimationSet.addAnimation(_TranslateAnimation);
			_AnimationSet.setFillBefore(false);
			_AnimationSet.setFillAfter(true);
			_AnimationSet.setDuration(100);
			/*LayoutParams _LayoutParams1 = new LayoutParams(100, 4);
			_LayoutParams1.setMargins(0, 0, 0, 0);
			_LayoutParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);*/
			//mImageView.bringToFront();
			mImageView.startAnimation(_AnimationSet);//开始上面蓝色横条图片的动画切换
			//mImageView.setLayoutParams(_LayoutParams1);
			mViewPager.setCurrentItem(1);//让下方ViewPager跟随上面的HorizontalScrollView切换
		}else if (checkedId == R.id.btn2) {
			_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo2), 0f, 0f);

			_AnimationSet.addAnimation(_TranslateAnimation);
			_AnimationSet.setFillBefore(false);
			_AnimationSet.setFillAfter(true);
			_AnimationSet.setDuration(100);

			//mImageView.bringToFront();
			mImageView.startAnimation(_AnimationSet);
			
			mViewPager.setCurrentItem(2);
		}else if (checkedId == R.id.btn3) {
			_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo3), 0f, 0f);
			
			_AnimationSet.addAnimation(_TranslateAnimation);
			_AnimationSet.setFillBefore(false);
			_AnimationSet.setFillAfter(true);
			_AnimationSet.setDuration(100);
			
			//mImageView.bringToFront();
			mImageView.startAnimation(_AnimationSet);
			
			mViewPager.setCurrentItem(3);
		}else if (checkedId == R.id.btn4) {
			_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo4), 0f, 0f);
			
			_AnimationSet.addAnimation(_TranslateAnimation);
			_AnimationSet.setFillBefore(false);
			_AnimationSet.setFillAfter(true);
			_AnimationSet.setDuration(100);
			
			//mImageView.bringToFront();
			mImageView.startAnimation(_AnimationSet);
			mViewPager.setCurrentItem(4);
		}else if (checkedId == R.id.btn5) {
			_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo5), 0f, 0f);
			
			_AnimationSet.addAnimation(_TranslateAnimation);
			_AnimationSet.setFillBefore(false);
			_AnimationSet.setFillAfter(true);
			_AnimationSet.setDuration(100);
			
			//mImageView.bringToFront();
			mImageView.startAnimation(_AnimationSet);
			
			mViewPager.setCurrentItem(5);
		}
		
		mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();
		
		Log.i("zj", "getCurrentCheckedRadioLeft="+getCurrentCheckedRadioLeft());
		Log.i("zj", "getDimension="+getResources().getDimension(R.dimen.rdo2));
		
		mHorizontalScrollView.smoothScrollTo((int)mCurrentCheckedRadioLeft-(int)getResources().getDimension(R.dimen.rdo2), 0);
	}
    
	/**
     * 获得当前被选中的RadioButton距离左侧的距离
     */
	private float getCurrentCheckedRadioLeft() {
		// TODO Auto-generated method stub
		if (mRadioButton1.isChecked()) {
			//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo1));
			return getResources().getDimension(R.dimen.rdo1);
		}else if (mRadioButton2.isChecked()) {
			//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo2));
			return getResources().getDimension(R.dimen.rdo2);
		}else if (mRadioButton3.isChecked()) {
			//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo3));
			return getResources().getDimension(R.dimen.rdo3);
		}else if (mRadioButton4.isChecked()) {
			//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo4));
			return getResources().getDimension(R.dimen.rdo4);
		}else if (mRadioButton5.isChecked()) {
			//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo5));
			return getResources().getDimension(R.dimen.rdo5);
		}
		return 0f;
	}

	private void iniListener() {
		// TODO Auto-generated method stub
		
		mRadioGroup.setOnCheckedChangeListener(this);
		
		
		mViewPager.setOnPageChangeListener(new MyPagerOnPageChangeListener());
	}

	private void iniController() {
		// TODO Auto-generated method stub
		mRadioGroup = (RadioGroup)findViewById(R.id.radioGroup);
		mRadioButton1 = (RadioButton)findViewById(R.id.btn1);
		mRadioButton2 = (RadioButton)findViewById(R.id.btn2);
		mRadioButton3 = (RadioButton)findViewById(R.id.btn3);
		mRadioButton4 = (RadioButton)findViewById(R.id.btn4);
		mRadioButton5 = (RadioButton)findViewById(R.id.btn5);
		
		mImageView = (ImageView)findViewById(R.id.img1);
		
		mHorizontalScrollView = (HorizontalScrollView)findViewById(R.id.horizontalScrollView);
		
		mViewPager = (ViewPager)findViewById(R.id.pager);
	}

	/**
	 * ViewPager的适配器
	 * @author zj
	 * 2012-5-24 下午2:26:57
	 */
	private class MyPagerAdapter extends PagerAdapter{

		@Override
		public void destroyItem(View v, int position, Object obj) {
			// TODO Auto-generated method stub
			((ViewPager)v).removeView(mViews.get(position));
		}

		@Override
		public void finishUpdate(View arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mViews.size();
		}

		@Override
		public Object instantiateItem(View v, int position) {
			((ViewPager)v).addView(mViews.get(position));
			return mViews.get(position);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public Parcelable saveState() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	/**
	 * ViewPager的PageChangeListener(页面改变的监听器)
	 * @author zj
	 * 2012-5-24 下午3:14:27
	 */
	private class MyPagerOnPageChangeListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}
		/**
		 * 滑动ViewPager的时候,让上方的HorizontalScrollView自动切换
		 */
		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			//Log.i("zj", "position="+position);
			
			if (position == 0) {
				mViewPager.setCurrentItem(1);
			}else if (position == 1) {
				mRadioButton1.performClick();
			}else if (position == 2) {
				mRadioButton2.performClick();
			}else if (position == 3) {
				mRadioButton3.performClick();
			}else if (position == 4) {
				mRadioButton4.performClick();
			}else if (position == 5) {
				mRadioButton5.performClick();
			}else if (position == 6) {
				mViewPager.setCurrentItem(5);
			}
		}
		
	}
	
}

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>

你可能感兴趣的:(导航菜单横向左右滑动并和下方的控件实现联动(有效果图))