Android自定义控件之联动视图 .

来源:互联网 发布:centos 7安装 编辑:程序博客网 时间:2024/05/18 01:12

效果么就是ViewPage + 底部导航,效果还不错...Adapter类请在《Android自定义控件之广告视图》中查找....

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 联动视图控件适配器  
  3.  * 若要更改样式 请重载响应的方法 
  4.  * @author: linxcool.hu 
  5.  */  
  6. public abstract class GangedPageAdapter extends Adapter{  
  7.     /** 
  8.      * 标题宽度 
  9.      * @return 
  10.      */  
  11.     public int getMenuWidth(Context context){  
  12.         return getFixPx(context,100);  
  13.     }  
  14.     /** 
  15.      * 标题高度 
  16.      * @return 
  17.      */  
  18.     public int getMenuHeight(Context context){  
  19.         return getFixPx(context,50);  
  20.     }  
  21.       
  22.     public View[] getMenus() {  
  23.         int count=getCount();  
  24.         View[] views=new View[count];  
  25.         for (int i = 0; i < count; i++) {  
  26.             views[i]=getMenu(i);  
  27.         }  
  28.         return views;  
  29.     }  
  30.       
  31.     public abstract View getMenu(int position);  
  32.       
  33. }  

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 联动视图组件 
  3.  * @author: linxcool.hu 
  4.  */  
  5. public class GangedPage extends LinearLayout{  
  6.     public interface OnPageSelectedListener{  
  7.         public void onPageSelected(int position);  
  8.     };  
  9.     private OnPageSelectedListener listener;  
  10.       
  11.     private GangedPageAdapter adapter;  
  12.       
  13.     //标题栏   
  14.     private HorizontalScrollView topMenuScrollView;  
  15.     private RadioButton[] radioBtns;  
  16.     private View[] menuViews;  
  17.     //页面栏   
  18.     private View[] pages;  
  19.     //选中的背景   
  20.     private View checkedBg;  
  21.     //页面   
  22.     private ViewPager viewPager;  
  23.     //附加属性   
  24.     private int menuWidth;  
  25.     private int menuHeight;  
  26.     private float currentCheckedBtnToLeftSpace;  
  27.     private int lastCheckedId;  
  28.       
  29.     public void setOnPageSelectedListener(OnPageSelectedListener listener) {  
  30.         this.listener = listener;  
  31.     }  
  32.       
  33.     public View getPage(int position){  
  34.         return pages[position];  
  35.     }  
  36.       
  37.     public boolean setSelected(int position){  
  38.         if(position<0||position>radioBtns.length)  
  39.             return false;  
  40.         if(lastCheckedId!=position)  
  41.             radioBtns[position].performClick();  
  42.         else if(listener!=null)  
  43.             listener.onPageSelected(position);  
  44.         return true;  
  45.     }  
  46.       
  47.     /** 
  48.      * 设置菜单栏的背景 
  49.      * @param defaultMenuBg 
  50.      */  
  51.     public void setTitleBackground(int color){  
  52.         topMenuScrollView.setBackgroundColor(color);  
  53.     }  
  54.     /** 
  55.      * 设置菜单栏的背景 
  56.      * @param drawable 
  57.      */  
  58.     public void setTitleBackground(Drawable drawable){  
  59.         topMenuScrollView.setBackgroundDrawable(drawable);  
  60.     }  
  61.       
  62.     /** 
  63.      * 设置菜单按钮间距 
  64.      * @param size 
  65.      */  
  66.     public void setMenuItemBlankSpace(int size){  
  67.         for (int i = 0; i < radioBtns.length; i++) {  
  68.             LayoutParams btnParams=(LayoutParams) radioBtns[i].getLayoutParams();  
  69.             if(i!=radioBtns.length-1)  
  70.                 btnParams.setMargins(00,size, 0);  
  71.               
  72.             menuViews[i].setLayoutParams(btnParams);  
  73.             radioBtns[i].setLayoutParams(btnParams);  
  74.         }  
  75.     }  
  76.       
  77.     public GangedPage(Context context) {  
  78.         super(context);  
  79.         setOrientation(VERTICAL);  
  80.     }  
  81.       
  82.     public void setAdapter(GangedPageAdapter adapter){  
  83.         this.adapter=adapter;  
  84.         initResource();  
  85.         initPages();  
  86.         initMenus();  
  87.         lastCheckedId=0;  
  88.         radioBtns[lastCheckedId].setChecked(true);  
  89.     }  
  90.       
  91.     /** 
  92.      * 构造器 
  93.      * @param context 
  94.      * @param adapter 
  95.      */  
  96.     public GangedPage(Context context,GangedPageAdapter adapter) {  
  97.         this(context);  
  98.         setAdapter(adapter);  
  99.     }  
  100.   
  101.     protected void initResource(){  
  102.         menuWidth=adapter.getMenuWidth(getContext());  
  103.         menuHeight=adapter.getMenuHeight(getContext());  
  104.         menuViews=adapter.getMenus();  
  105.         checkedBg=adapter.getCheckedBg();  
  106.         pages=adapter.getPages();  
  107.     }  
  108.   
  109.     protected void initMenus(){  
  110.         //最外层滚动栏   
  111.         topMenuScrollView=new HorizontalScrollView(getContext());  
  112.         topMenuScrollView.setHorizontalScrollBarEnabled(false);  
  113.         topMenuScrollView.setBackgroundColor(Color.WHITE);  
  114.         topMenuScrollView.setFadingEdgeLength(0);  
  115.         LayoutParams svParams=new LayoutParams(  
  116.                 LayoutParams.FILL_PARENT,menuHeight);  
  117.         this.addView(topMenuScrollView,svParams);  
  118.   
  119.         RelativeLayout outLayout=new RelativeLayout(getContext());  
  120.         topMenuScrollView.addView(outLayout,  
  121.                 new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));  
  122.         //Frame可叠加层   
  123.         FrameLayout menuFrame=new FrameLayout(getContext());  
  124.         RelativeLayout.LayoutParams frameParams=new RelativeLayout.LayoutParams(  
  125.                 LayoutParams.FILL_PARENT,menuHeight);  
  126.         outLayout.addView(menuFrame,frameParams);  
  127.         //叠加层:背景特效容器   
  128.         LinearLayout menuBgLayout=new LinearLayout(getContext());  
  129.         menuBgLayout.setOrientation(RadioGroup.HORIZONTAL);  
  130.         FrameLayout.LayoutParams bParams=new FrameLayout.LayoutParams(  
  131.                 LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);  
  132.         menuFrame.addView(menuBgLayout,bParams);  
  133.         //叠加层:显示内容容器   
  134.         LinearLayout menuItemLayout=new LinearLayout(getContext());  
  135.         menuItemLayout.setOrientation(RadioGroup.HORIZONTAL);  
  136.         FrameLayout.LayoutParams cParams=new FrameLayout.LayoutParams(  
  137.                 LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);  
  138.         menuFrame.addView(menuItemLayout,cParams);  
  139.         //叠加层:单选按钮容器   
  140.         RadioGroup menuRadioGroup=new RadioGroup(getContext());  
  141.         menuRadioGroup.setOrientation(RadioGroup.HORIZONTAL);  
  142.         menuFrame.addView(menuRadioGroup,cParams);  
  143.   
  144.         radioBtns=new RadioButton[menuViews.length];  
  145.         for (int i = 0; i < menuViews.length; i++) {  
  146.             //背景内容视图   
  147.             LinearLayout.LayoutParams btnParams=new LinearLayout.LayoutParams(  
  148.                     menuWidth, menuHeight,1.0f);  
  149.             /*if(i!=menuViews.length-1) 
  150.                 btnParams.setMargins(0, 0,1, 0);*/  
  151.             menuItemLayout.addView(menuViews[i],btnParams);  
  152.             //单选按钮视图   
  153.             radioBtns[i]=new RadioButton(getContext());  
  154.             radioBtns[i].setId(i);  
  155.             radioBtns[i].setButtonDrawable(android.R.color.transparent);  
  156.             menuRadioGroup.addView(radioBtns[i],btnParams);  
  157.         }  
  158.         menuRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  159.             @Override  
  160.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  161.                 //下方线条动画   
  162.                 AnimationSet aniSet = new AnimationSet(true);  
  163.                 TranslateAnimation tAni=new TranslateAnimation(  
  164.                         currentCheckedBtnToLeftSpace,checkedId*menuWidth, 0f, 0f);  
  165.                 aniSet.addAnimation(tAni);  
  166.                 //aniSet.setFillBefore(false);  
  167.                 aniSet.setFillAfter(true);  
  168.                 aniSet.setDuration(200);  
  169.                 checkedBg.startAnimation(aniSet);  
  170.                 //设置当前显示的Page   
  171.                 viewPager.setCurrentItem(checkedId);  
  172.                 //重置currentCheckedBtnToLeftSpace 并滚动标题位置  
  173.                 currentCheckedBtnToLeftSpace = getCurrentCheckedBtnToLeftSpace();  
  174.                 float x=currentCheckedBtnToLeftSpace-menuWidth;  
  175.                 topMenuScrollView.smoothScrollTo((int)x,0);  
  176.                 //修改颜色   
  177.                 lastCheckedId=checkedId;  
  178.             }  
  179.         });  
  180.   
  181.         //选中背景   
  182.         LayoutParams ivParams=new LayoutParams(  
  183.                 menuWidth,menuHeight-adapter.getFixPx(getContext(), 4));  
  184.         menuBgLayout.setGravity(Gravity.CENTER_VERTICAL);  
  185.         menuBgLayout.addView(checkedBg,ivParams);  
  186.         currentCheckedBtnToLeftSpace=getCurrentCheckedBtnToLeftSpace();  
  187.     }  
  188.   
  189.     protected void initPages(){  
  190.         viewPager=new ViewPager(getContext()){  
  191.             @Override  
  192.             public boolean onInterceptTouchEvent(MotionEvent arg0) {  
  193.                 return super.onInterceptTouchEvent(arg0);  
  194.                 //return false;   
  195.             }  
  196.         };  
  197.         LayoutParams vpParams=new LayoutParams(  
  198.                 LayoutParams.FILL_PARENT,0,1.0f);  
  199.         this.addView(viewPager,vpParams);  
  200.         viewPager.setAdapter(new PagerAdapter() {  
  201.             @Override  
  202.             public boolean isViewFromObject(View arg0, Object arg1) {  
  203.                 return arg0==arg1;  
  204.             }  
  205.             @Override  
  206.             public int getCount() {  
  207.                 return pages.length;  
  208.             }  
  209.             @Override  
  210.             public void destroyItem(View container, int position, Object object) {  
  211.                 ((ViewPager)container).removeView(pages[position]);  
  212.             }  
  213.             @Override  
  214.             public Object instantiateItem(View v, int position) {  
  215.                 ((ViewPager)v).addView(pages[position]);  
  216.                 return pages[position];  
  217.             }  
  218.         });  
  219.         viewPager.setOnPageChangeListener(new OnPageChangeListener() {  
  220.             @Override  
  221.             public void onPageSelected(int position) {  
  222.                 radioBtns[position].performClick();  
  223.                 if(listener!=null)listener.onPageSelected(position);  
  224.             }  
  225.             @Override  
  226.             public void onPageScrolled(int arg0, float arg1, int arg2) {  
  227.   
  228.             }  
  229.             @Override  
  230.             public void onPageScrollStateChanged(int arg0) {  
  231.   
  232.             }  
  233.         });  
  234.     }  
  235.   
  236.     /** 
  237.      * 顶部被选中按钮的到左边0坐标的距离 
  238.      * @return 
  239.      */  
  240.     protected float getCurrentCheckedBtnToLeftSpace(){  
  241.         for (int i = 0; i < radioBtns.length; i++) {  
  242.             if(radioBtns[i].isChecked()){  
  243.                 return menuWidth*i;  
  244.             }  
  245.         }  
  246.         return 0f;  
  247.     }  
  248. }  
资源:http://download.csdn.net/category

版权声明:本文为博主原创文章,未经博主允许不得转载。

0 0