加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Android导航菜单横向左右滑动并和下方的控件实现联动

(2015-01-28 13:56:33)
标签:

android

分类: android
转自:http://blog.csdn.net/fengqiangfeng/article/details/7606040

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


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

 


http://dl.iteye.com/upload/attachment/0068/5942/31f276b8-2ec8-3905-90c2-81380edfa100.gif

 

 

现在看一下代码:

 

  1. package com.zj.horizontalsrollview;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.os.Parcelable;  
  8. import android.support.v4.view.PagerAdapter;  
  9. import android.support.v4.view.ViewPager;  
  10. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  11. import android.util.Log;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.view.ViewGroup.MarginLayoutParams;  
  15. import android.view.animation.Animation;  
  16. import android.view.animation.AnimationSet;  
  17. import android.view.animation.AnimationUtils;  
  18. import android.view.animation.TranslateAnimation;  
  19. import android.widget.HorizontalScrollView;  
  20. import android.widget.ImageView;  
  21. import android.widget.RadioButton;  
  22. import android.widget.RadioGroup;  
  23. import android.widget.RadioGroup.OnCheckedChangeListener;  
  24. import android.widget.RelativeLayout;  
  25. import android.widget.RelativeLayout.LayoutParams;  
  26.   
  27. public class MainActivity extends Activity implements OnCheckedChangeListener{  
  28.     private RadioGroup mRadioGroup;  
  29.     private RadioButton mRadioButton1;  
  30.     private RadioButton mRadioButton2;  
  31.     private RadioButton mRadioButton3;  
  32.     private RadioButton mRadioButton4;  
  33.     private RadioButton mRadioButton5;  
  34.     private ImageView mImageView;  
  35.     private float mCurrentCheckedRadioLeft;//当前被选中的RadioButton距离左侧的距离  
  36.     private HorizontalScrollView mHorizontalScrollView;//上面的水平滚动控件  
  37.     private ViewPager mViewPager;   //下方的可横向拖动的控件  
  38.     private ArrayList mViews;//用来存放下方滚动的layout(layout_1,layout_2,layout_3)  
  39.     @Override  
  40.     public void onCreate(Bundle savedInstanceState)  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.main);  
  43.           
  44.         iniController();  
  45.         iniListener();  
  46.         iniVariable();  
  47.           
  48.         mRadioButton1.setChecked(true);  
  49.         mViewPager.setCurrentItem(1);  
  50.         mCurrentCheckedRadioLeft getCurrentCheckedRadioLeft();  
  51.           
  52.      
  53.       
  54.     private void iniVariable()  
  55.         // TODO Auto-generated method stub  
  56.         mViews new ArrayList();  
  57.         mViews.add(getLayoutInflater().inflate(R.layout.layout_0, null));  
  58.         mViews.add(getLayoutInflater().inflate(R.layout.layout_1, null));  
  59.         mViews.add(getLayoutInflater().inflate(R.layout.layout_2, null));  
  60.         mViews.add(getLayoutInflater().inflate(R.layout.layout_3, null));  
  61.         mViews.add(getLayoutInflater().inflate(R.layout.layout_4, null));  
  62.         mViews.add(getLayoutInflater().inflate(R.layout.layout_5, null));  
  63.         mViews.add(getLayoutInflater().inflate(R.layout.layout_0, null));  
  64.           
  65.         mViewPager.setAdapter(new MyPagerAdapter());//设置ViewPager的适配器  
  66.      
  67.       
  68.       
  69.     @Override  
  70.     public void onCheckedChanged(RadioGroup group, int checkedId)  
  71.           
  72.         AnimationSet _AnimationSet new AnimationSet(true);  
  73.         TranslateAnimation _TranslateAnimation;  
  74.           
  75.         Log.i("zj""checkedid="+checkedId);  
  76.         if (checkedId == R.id.btn1)  
  77.             _TranslateAnimation new TranslateAnimation(mCurrentCheckedRadioLeftgetResources().getDimension(R.dimen.rdo1), 0f, 0f);  
  78.             _AnimationSet.addAnimation(_TranslateAnimation);  
  79.             _AnimationSet.setFillBefore(false);  
  80.             _AnimationSet.setFillAfter(true);  
  81.             _AnimationSet.setDuration(100);  
  82.               
  83.             //mImageView.bringToFront();  
  84.             mImageView.startAnimation(_AnimationSet);//开始上面蓝色横条图片的动画切换  
  85.             //mImageView.setLayoutParams(_LayoutParams1);  
  86.             mViewPager.setCurrentItem(1);//让下方ViewPager跟随上面的HorizontalScrollView切换  
  87.         }else if (checkedId == R.id.btn2)  
  88.             _TranslateAnimation new TranslateAnimation(mCurrentCheckedRadioLeftgetResources().getDimension(R.dimen.rdo2), 0f, 0f);  
  89.   
  90.             _AnimationSet.addAnimation(_TranslateAnimation);  
  91.             _AnimationSet.setFillBefore(false);  
  92.             _AnimationSet.setFillAfter(true);  
  93.             _AnimationSet.setDuration(100);  
  94.   
  95.             //mImageView.bringToFront();  
  96.             mImageView.startAnimation(_AnimationSet);  
  97.               
  98.             mViewPager.setCurrentItem(2);  
  99.         }else if (checkedId == R.id.btn3)  
  100.             _TranslateAnimation new TranslateAnimation(mCurrentCheckedRadioLeftgetResources().getDimension(R.dimen.rdo3), 0f, 0f);  
  101.               
  102.             _AnimationSet.addAnimation(_TranslateAnimation);  
  103.             _AnimationSet.setFillBefore(false);  
  104.             _AnimationSet.setFillAfter(true);  
  105.             _AnimationSet.setDuration(100);  
  106.               
  107.             //mImageView.bringToFront();  
  108.             mImageView.startAnimation(_AnimationSet);  
  109.               
  110.             mViewPager.setCurrentItem(3);  
  111.         }else if (checkedId == R.id.btn4)  
  112.             _TranslateAnimation new TranslateAnimation(mCurrentCheckedRadioLeftgetResources().getDimension(R.dimen.rdo4), 0f, 0f);  
  113.               
  114.             _AnimationSet.addAnimation(_TranslateAnimation);  
  115.             _AnimationSet.setFillBefore(false);  
  116.             _AnimationSet.setFillAfter(true);  
  117.             _AnimationSet.setDuration(100);  
  118.               
  119.             //mImageView.bringToFront();  
  120.             mImageView.startAnimation(_AnimationSet);  
  121.             mViewPager.setCurrentItem(4);  
  122.         }else if (checkedId == R.id.btn5)  
  123.             _TranslateAnimation new TranslateAnimation(mCurrentCheckedRadioLeftgetResources().getDimension(R.dimen.rdo5), 0f, 0f);  
  124.               
  125.             _AnimationSet.addAnimation(_TranslateAnimation);  
  126.             _AnimationSet.setFillBefore(false);  
  127.             _AnimationSet.setFillAfter(true);  
  128.             _AnimationSet.setDuration(100);  
  129.               
  130.             //mImageView.bringToFront();  
  131.             mImageView.startAnimation(_AnimationSet);  
  132.               
  133.             mViewPager.setCurrentItem(5);  
  134.          
  135.           
  136.         mCurrentCheckedRadioLeft getCurrentCheckedRadioLeft();  
  137.           
  138.         Log.i("zj""getCurrentCheckedRadioLeft="+getCurrentCheckedRadioLeft());  
  139.         Log.i("zj""getDimension="+getResources().getDimension(R.dimen.rdo2));  
  140.           
  141.         mHorizontalScrollView.smoothScrollTo((int)mCurrentCheckedRadioLeft-(int)getResources().getDimension(R.dimen.rdo2), 0);  
  142.      
  143.       
  144.       
  145.     private float getCurrentCheckedRadioLeft()  
  146.         // TODO Auto-generated method stub  
  147.         if (mRadioButton1.isChecked())  
  148.             //Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo1));  
  149.             return getResources().getDimension(R.dimen.rdo1);  
  150.         }else if (mRadioButton2.isChecked())  
  151.             //Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo2));  
  152.             return getResources().getDimension(R.dimen.rdo2);  
  153.         }else if (mRadioButton3.isChecked())  
  154.             //Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo3));  
  155.             return getResources().getDimension(R.dimen.rdo3);  
  156.         }else if (mRadioButton4.isChecked())  
  157.             //Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo4));  
  158.             return getResources().getDimension(R.dimen.rdo4);  
  159.         }else if (mRadioButton5.isChecked())  
  160.             //Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo5));  
  161.             return getResources().getDimension(R.dimen.rdo5);  
  162.          
  163.         return 0f;  
  164.      
  165.   
  166.     private void iniListener()  
  167.         // TODO Auto-generated method stub  
  168.           
  169.         mRadioGroup.setOnCheckedChangeListener(this);  
  170.           
  171.           
  172.         mViewPager.setOnPageChangeListener(new MyPagerOnPageChangeListener());  
  173.      
  174.   
  175.     private void iniController()  
  176.         // TODO Auto-generated method stub  
  177.         mRadioGroup (RadioGroup)findViewById(R.id.radioGroup);  
  178.         mRadioButton1 (RadioButton)findViewById(R.id.btn1);  
  179.         mRadioButton2 (RadioButton)findViewById(R.id.btn2);  
  180.         mRadioButton3 (RadioButton)findViewById(R.id.btn3);  
  181.         mRadioButton4 (RadioButton)findViewById(R.id.btn4);  
  182.         mRadioButton5 (RadioButton)findViewById(R.id.btn5);  
  183.           
  184.         mImageView (ImageView)findViewById(R.id.img1);  
  185.           
  186.         mHorizontalScrollView (HorizontalScrollView)findViewById(R.id.horizontalScrollView);  
  187.           
  188.         mViewPager (ViewPager)findViewById(R.id.pager);  
  189.      
  190.   
  191.       
  192.     private class MyPagerAdapter extends PagerAdapter{  
  193.   
  194.         @Override  
  195.         public void destroyItem(View v, int position, Object obj)  
  196.             // TODO Auto-generated method stub  
  197.             ((ViewPager)v).removeView(mViews.get(position));  
  198.          
  199.   
  200.         @Override  
  201.         public void finishUpdate(View arg0)  
  202.             // TODO Auto-generated method stub  
  203.               
  204.          
  205.   
  206.         @Override  
  207.         public int getCount()  
  208.             // TODO Auto-generated method stub  
  209.             return mViews.size();  
  210.          
  211.   
  212.         @Override  
  213.         public Object instantiateItem(View v, int position)  
  214.             ((ViewPager)v).addView(mViews.get(position));  
  215.             return mViews.get(position);  
  216.          
  217.   
  218.         @Override  
  219.         public boolean isViewFromObject(View arg0, Object arg1)  
  220.             // TODO Auto-generated method stub  
  221.             return arg0 == arg1;  
  222.          
  223.   
  224.         @Override  
  225.         public void restoreState(Parcelable arg0, ClassLoader arg1)  
  226.             // TODO Auto-generated method stub  
  227.               
  228.          
  229.   
  230.         @Override  
  231.         public Parcelable saveState()  
  232.             // TODO Auto-generated method stub  
  233.             return null 
  234.          
  235.   
  236.         @Override  
  237.         public void startUpdate(View arg0)  
  238.             // TODO Auto-generated method stub  
  239.               
  240.          
  241.           
  242.      
  243.       
  244.     private class MyPagerOnPageChangeListener implements OnPageChangeListener{  
  245.   
  246.         @Override  
  247.         public void onPageScrollStateChanged(int arg0)  
  248.             // TODO Auto-generated method stub  
  249.               
  250.          
  251.   
  252.         @Override  
  253.         public void onPageScrolled(int arg0, float arg1, int arg2)  
  254.             // TODO Auto-generated method stub  
  255.               
  256.          
  257.           
  258.         @Override  
  259.         public void onPageSelected(int position)  
  260.             // TODO Auto-generated method stub  
  261.             //Log.i("zj", "position="+position);  
  262.               
  263.             if (position == 0 
  264.                 mViewPager.setCurrentItem(1);  
  265.             }else if (position == 1 
  266.                 mRadioButton1.performClick();  
  267.             }else if (position == 2 
  268.                 mRadioButton2.performClick();  
  269.             }else if (position == 3 
  270.                 mRadioButton3.performClick();  
  271.             }else if (position == 4 
  272.                 mRadioButton4.performClick();  
  273.             }else if (position == 5 
  274.                 mRadioButton5.performClick();  
  275.             }else if (position == 6 
  276.                 mViewPager.setCurrentItem(5);  
  277.              
  278.          
  279.           
  280.      
  281.       
  282.  

 

 

 

 

 

 

 XML文件:

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有