Android自定义控件之联动视图 .
来源:互联网 发布:centos 7安装 编辑:程序博客网 时间:2024/05/18 01:12
效果么就是ViewPage + 底部导航,效果还不错...Adapter类请在《Android自定义控件之广告视图》中查找....
- /**
- * 联动视图控件适配器
- * 若要更改样式 请重载响应的方法
- * @author: linxcool.hu
- */
- public abstract class GangedPageAdapter extends Adapter{
- /**
- * 标题宽度
- * @return
- */
- public int getMenuWidth(Context context){
- return getFixPx(context,100);
- }
- /**
- * 标题高度
- * @return
- */
- public int getMenuHeight(Context context){
- return getFixPx(context,50);
- }
- public View[] getMenus() {
- int count=getCount();
- View[] views=new View[count];
- for (int i = 0; i < count; i++) {
- views[i]=getMenu(i);
- }
- return views;
- }
- public abstract View getMenu(int position);
- }
/** * 联动视图控件适配器 * 若要更改样式 请重载响应的方法 * @author: linxcool.hu */public abstract class GangedPageAdapter extends Adapter{/** * 标题宽度 * @return */public int getMenuWidth(Context context){return getFixPx(context,100);}/** * 标题高度 * @return */public int getMenuHeight(Context context){return getFixPx(context,50);}public View[] getMenus() {int count=getCount();View[] views=new View[count];for (int i = 0; i < count; i++) {views[i]=getMenu(i);}return views;}public abstract View getMenu(int position);}
- /**
- * 联动视图组件
- * @author: linxcool.hu
- */
- public class GangedPage extends LinearLayout{
- public interface OnPageSelectedListener{
- public void onPageSelected(int position);
- };
- private OnPageSelectedListener listener;
- private GangedPageAdapter adapter;
- //标题栏
- private HorizontalScrollView topMenuScrollView;
- private RadioButton[] radioBtns;
- private View[] menuViews;
- //页面栏
- private View[] pages;
- //选中的背景
- private View checkedBg;
- //页面
- private ViewPager viewPager;
- //附加属性
- private int menuWidth;
- private int menuHeight;
- private float currentCheckedBtnToLeftSpace;
- private int lastCheckedId;
- public void setOnPageSelectedListener(OnPageSelectedListener listener) {
- this.listener = listener;
- }
- public View getPage(int position){
- return pages[position];
- }
- public boolean setSelected(int position){
- if(position<0||position>radioBtns.length)
- return false;
- if(lastCheckedId!=position)
- radioBtns[position].performClick();
- else if(listener!=null)
- listener.onPageSelected(position);
- return true;
- }
- /**
- * 设置菜单栏的背景
- * @param defaultMenuBg
- */
- public void setTitleBackground(int color){
- topMenuScrollView.setBackgroundColor(color);
- }
- /**
- * 设置菜单栏的背景
- * @param drawable
- */
- public void setTitleBackground(Drawable drawable){
- topMenuScrollView.setBackgroundDrawable(drawable);
- }
- /**
- * 设置菜单按钮间距
- * @param size
- */
- public void setMenuItemBlankSpace(int size){
- for (int i = 0; i < radioBtns.length; i++) {
- LayoutParams btnParams=(LayoutParams) radioBtns[i].getLayoutParams();
- if(i!=radioBtns.length-1)
- btnParams.setMargins(0, 0,size, 0);
- menuViews[i].setLayoutParams(btnParams);
- radioBtns[i].setLayoutParams(btnParams);
- }
- }
- public GangedPage(Context context) {
- super(context);
- setOrientation(VERTICAL);
- }
- public void setAdapter(GangedPageAdapter adapter){
- this.adapter=adapter;
- initResource();
- initPages();
- initMenus();
- lastCheckedId=0;
- radioBtns[lastCheckedId].setChecked(true);
- }
- /**
- * 构造器
- * @param context
- * @param adapter
- */
- public GangedPage(Context context,GangedPageAdapter adapter) {
- this(context);
- setAdapter(adapter);
- }
- protected void initResource(){
- menuWidth=adapter.getMenuWidth(getContext());
- menuHeight=adapter.getMenuHeight(getContext());
- menuViews=adapter.getMenus();
- checkedBg=adapter.getCheckedBg();
- pages=adapter.getPages();
- }
- protected void initMenus(){
- //最外层滚动栏
- topMenuScrollView=new HorizontalScrollView(getContext());
- topMenuScrollView.setHorizontalScrollBarEnabled(false);
- topMenuScrollView.setBackgroundColor(Color.WHITE);
- topMenuScrollView.setFadingEdgeLength(0);
- LayoutParams svParams=new LayoutParams(
- LayoutParams.FILL_PARENT,menuHeight);
- this.addView(topMenuScrollView,svParams);
- RelativeLayout outLayout=new RelativeLayout(getContext());
- topMenuScrollView.addView(outLayout,
- new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
- //Frame可叠加层
- FrameLayout menuFrame=new FrameLayout(getContext());
- RelativeLayout.LayoutParams frameParams=new RelativeLayout.LayoutParams(
- LayoutParams.FILL_PARENT,menuHeight);
- outLayout.addView(menuFrame,frameParams);
- //叠加层:背景特效容器
- LinearLayout menuBgLayout=new LinearLayout(getContext());
- menuBgLayout.setOrientation(RadioGroup.HORIZONTAL);
- FrameLayout.LayoutParams bParams=new FrameLayout.LayoutParams(
- LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
- menuFrame.addView(menuBgLayout,bParams);
- //叠加层:显示内容容器
- LinearLayout menuItemLayout=new LinearLayout(getContext());
- menuItemLayout.setOrientation(RadioGroup.HORIZONTAL);
- FrameLayout.LayoutParams cParams=new FrameLayout.LayoutParams(
- LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
- menuFrame.addView(menuItemLayout,cParams);
- //叠加层:单选按钮容器
- RadioGroup menuRadioGroup=new RadioGroup(getContext());
- menuRadioGroup.setOrientation(RadioGroup.HORIZONTAL);
- menuFrame.addView(menuRadioGroup,cParams);
- radioBtns=new RadioButton[menuViews.length];
- for (int i = 0; i < menuViews.length; i++) {
- //背景内容视图
- LinearLayout.LayoutParams btnParams=new LinearLayout.LayoutParams(
- menuWidth, menuHeight,1.0f);
- /*if(i!=menuViews.length-1)
- btnParams.setMargins(0, 0,1, 0);*/
- menuItemLayout.addView(menuViews[i],btnParams);
- //单选按钮视图
- radioBtns[i]=new RadioButton(getContext());
- radioBtns[i].setId(i);
- radioBtns[i].setButtonDrawable(android.R.color.transparent);
- menuRadioGroup.addView(radioBtns[i],btnParams);
- }
- menuRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- //下方线条动画
- AnimationSet aniSet = new AnimationSet(true);
- TranslateAnimation tAni=new TranslateAnimation(
- currentCheckedBtnToLeftSpace,checkedId*menuWidth, 0f, 0f);
- aniSet.addAnimation(tAni);
- //aniSet.setFillBefore(false);
- aniSet.setFillAfter(true);
- aniSet.setDuration(200);
- checkedBg.startAnimation(aniSet);
- //设置当前显示的Page
- viewPager.setCurrentItem(checkedId);
- //重置currentCheckedBtnToLeftSpace 并滚动标题位置
- currentCheckedBtnToLeftSpace = getCurrentCheckedBtnToLeftSpace();
- float x=currentCheckedBtnToLeftSpace-menuWidth;
- topMenuScrollView.smoothScrollTo((int)x,0);
- //修改颜色
- lastCheckedId=checkedId;
- }
- });
- //选中背景
- LayoutParams ivParams=new LayoutParams(
- menuWidth,menuHeight-adapter.getFixPx(getContext(), 4));
- menuBgLayout.setGravity(Gravity.CENTER_VERTICAL);
- menuBgLayout.addView(checkedBg,ivParams);
- currentCheckedBtnToLeftSpace=getCurrentCheckedBtnToLeftSpace();
- }
- protected void initPages(){
- viewPager=new ViewPager(getContext()){
- @Override
- public boolean onInterceptTouchEvent(MotionEvent arg0) {
- return super.onInterceptTouchEvent(arg0);
- //return false;
- }
- };
- LayoutParams vpParams=new LayoutParams(
- LayoutParams.FILL_PARENT,0,1.0f);
- this.addView(viewPager,vpParams);
- viewPager.setAdapter(new PagerAdapter() {
- @Override
- public boolean isViewFromObject(View arg0, Object arg1) {
- return arg0==arg1;
- }
- @Override
- public int getCount() {
- return pages.length;
- }
- @Override
- public void destroyItem(View container, int position, Object object) {
- ((ViewPager)container).removeView(pages[position]);
- }
- @Override
- public Object instantiateItem(View v, int position) {
- ((ViewPager)v).addView(pages[position]);
- return pages[position];
- }
- });
- viewPager.setOnPageChangeListener(new OnPageChangeListener() {
- @Override
- public void onPageSelected(int position) {
- radioBtns[position].performClick();
- if(listener!=null)listener.onPageSelected(position);
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- }
- });
- }
- /**
- * 顶部被选中按钮的到左边0坐标的距离
- * @return
- */
- protected float getCurrentCheckedBtnToLeftSpace(){
- for (int i = 0; i < radioBtns.length; i++) {
- if(radioBtns[i].isChecked()){
- return menuWidth*i;
- }
- }
- return 0f;
- }
- }
/** * 联动视图组件 * @author: linxcool.hu */public class GangedPage extends LinearLayout{public interface OnPageSelectedListener{public void onPageSelected(int position);};private OnPageSelectedListener listener;private GangedPageAdapter adapter;//标题栏private HorizontalScrollView topMenuScrollView;private RadioButton[] radioBtns;private View[] menuViews;//页面栏private View[] pages;//选中的背景private View checkedBg;//页面private ViewPager viewPager;//附加属性private int menuWidth;private int menuHeight;private float currentCheckedBtnToLeftSpace;private int lastCheckedId;public void setOnPageSelectedListener(OnPageSelectedListener listener) {this.listener = listener;}public View getPage(int position){return pages[position];}public boolean setSelected(int position){if(position<0||position>radioBtns.length)return false;if(lastCheckedId!=position)radioBtns[position].performClick();else if(listener!=null)listener.onPageSelected(position);return true;}/** * 设置菜单栏的背景 * @param defaultMenuBg */public void setTitleBackground(int color){topMenuScrollView.setBackgroundColor(color);}/** * 设置菜单栏的背景 * @param drawable */public void setTitleBackground(Drawable drawable){topMenuScrollView.setBackgroundDrawable(drawable);}/** * 设置菜单按钮间距 * @param size */public void setMenuItemBlankSpace(int size){for (int i = 0; i < radioBtns.length; i++) {LayoutParams btnParams=(LayoutParams) radioBtns[i].getLayoutParams();if(i!=radioBtns.length-1)btnParams.setMargins(0, 0,size, 0);menuViews[i].setLayoutParams(btnParams);radioBtns[i].setLayoutParams(btnParams);}}public GangedPage(Context context) {super(context);setOrientation(VERTICAL);}public void setAdapter(GangedPageAdapter adapter){this.adapter=adapter;initResource();initPages();initMenus();lastCheckedId=0;radioBtns[lastCheckedId].setChecked(true);}/** * 构造器 * @param context * @param adapter */public GangedPage(Context context,GangedPageAdapter adapter) {this(context);setAdapter(adapter);}protected void initResource(){menuWidth=adapter.getMenuWidth(getContext());menuHeight=adapter.getMenuHeight(getContext());menuViews=adapter.getMenus();checkedBg=adapter.getCheckedBg();pages=adapter.getPages();}protected void initMenus(){//最外层滚动栏topMenuScrollView=new HorizontalScrollView(getContext());topMenuScrollView.setHorizontalScrollBarEnabled(false);topMenuScrollView.setBackgroundColor(Color.WHITE);topMenuScrollView.setFadingEdgeLength(0);LayoutParams svParams=new LayoutParams(LayoutParams.FILL_PARENT,menuHeight);this.addView(topMenuScrollView,svParams);RelativeLayout outLayout=new RelativeLayout(getContext());topMenuScrollView.addView(outLayout,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));//Frame可叠加层FrameLayout menuFrame=new FrameLayout(getContext());RelativeLayout.LayoutParams frameParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,menuHeight);outLayout.addView(menuFrame,frameParams);//叠加层:背景特效容器LinearLayout menuBgLayout=new LinearLayout(getContext());menuBgLayout.setOrientation(RadioGroup.HORIZONTAL);FrameLayout.LayoutParams bParams=new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);menuFrame.addView(menuBgLayout,bParams);//叠加层:显示内容容器LinearLayout menuItemLayout=new LinearLayout(getContext());menuItemLayout.setOrientation(RadioGroup.HORIZONTAL);FrameLayout.LayoutParams cParams=new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);menuFrame.addView(menuItemLayout,cParams);//叠加层:单选按钮容器RadioGroup menuRadioGroup=new RadioGroup(getContext());menuRadioGroup.setOrientation(RadioGroup.HORIZONTAL);menuFrame.addView(menuRadioGroup,cParams);radioBtns=new RadioButton[menuViews.length];for (int i = 0; i < menuViews.length; i++) {//背景内容视图LinearLayout.LayoutParams btnParams=new LinearLayout.LayoutParams(menuWidth, menuHeight,1.0f);/*if(i!=menuViews.length-1)btnParams.setMargins(0, 0,1, 0);*/menuItemLayout.addView(menuViews[i],btnParams);//单选按钮视图radioBtns[i]=new RadioButton(getContext());radioBtns[i].setId(i);radioBtns[i].setButtonDrawable(android.R.color.transparent);menuRadioGroup.addView(radioBtns[i],btnParams);}menuRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {//下方线条动画AnimationSet aniSet = new AnimationSet(true);TranslateAnimation tAni=new TranslateAnimation(currentCheckedBtnToLeftSpace,checkedId*menuWidth, 0f, 0f);aniSet.addAnimation(tAni);//aniSet.setFillBefore(false);aniSet.setFillAfter(true);aniSet.setDuration(200);checkedBg.startAnimation(aniSet);//设置当前显示的PageviewPager.setCurrentItem(checkedId);//重置currentCheckedBtnToLeftSpace 并滚动标题位置currentCheckedBtnToLeftSpace = getCurrentCheckedBtnToLeftSpace();float x=currentCheckedBtnToLeftSpace-menuWidth;topMenuScrollView.smoothScrollTo((int)x,0);//修改颜色lastCheckedId=checkedId;}});//选中背景LayoutParams ivParams=new LayoutParams(menuWidth,menuHeight-adapter.getFixPx(getContext(), 4));menuBgLayout.setGravity(Gravity.CENTER_VERTICAL);menuBgLayout.addView(checkedBg,ivParams);currentCheckedBtnToLeftSpace=getCurrentCheckedBtnToLeftSpace();}protected void initPages(){viewPager=new ViewPager(getContext()){@Overridepublic boolean onInterceptTouchEvent(MotionEvent arg0) {return super.onInterceptTouchEvent(arg0);//return false;}};LayoutParams vpParams=new LayoutParams(LayoutParams.FILL_PARENT,0,1.0f);this.addView(viewPager,vpParams);viewPager.setAdapter(new PagerAdapter() {@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0==arg1;}@Overridepublic int getCount() {return pages.length;}@Overridepublic void destroyItem(View container, int position, Object object) {((ViewPager)container).removeView(pages[position]);}@Overridepublic Object instantiateItem(View v, int position) {((ViewPager)v).addView(pages[position]);return pages[position];}});viewPager.setOnPageChangeListener(new OnPageChangeListener() {@Overridepublic void onPageSelected(int position) {radioBtns[position].performClick();if(listener!=null)listener.onPageSelected(position);}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}@Overridepublic void onPageScrollStateChanged(int arg0) {}});}/** * 顶部被选中按钮的到左边0坐标的距离 * @return */protected float getCurrentCheckedBtnToLeftSpace(){for (int i = 0; i < radioBtns.length; i++) {if(radioBtns[i].isChecked()){return menuWidth*i;}}return 0f;}}资源:http://download.csdn.net/category
版权声明:本文为博主原创文章,未经博主允许不得转载。
0 0
- Android自定义控件之联动视图
- Android自定义控件之联动视图 .
- Android自定义控件之广告视图
- Android自定义控件之天气视图
- 自定义控件之三级联动
- android 自定义视图控件开发
- Android自定义控件---“取消”视图
- android 自定义视图控件属性
- 自定义控件开发之----自定义视图状态
- Android--自定义视图控件(一)(Android Studio)
- Openlayers之视图联动
- 【视图控件篇】自定义Android控件之IOS滑动开关模拟详解
- android自定义控件系列教程----视图
- Android绘图API开发自定义控件视图
- Android 自定义加载效果视图(loading)控件
- Android之自定义控件
- Android之自定义控件
- android之自定义控件
- Docker使用心得
- 一、项目简介
- 当今世界十大经典算法
- android 5.0 悬浮窗使用 之“有权查看应用使用情况”
- Commons Daemon 之 procrun
- Android自定义控件之联动视图 .
- HAProxy用法详解 全网最详细中文文档
- Android 实现由下至上弹出并位于屏幕底部的提示框
- MindMapper中该如何添加便笺
- jquery-validate验证表单
- sql server TIMESTAMP数据类型的使用
- DP 递推j计数
- Java学习篇之---Spring Scope prototype与singleton区别
- EasyUI加载内嵌json数据方法