登录 立即注册
安币:

安卓巴士 - 安卓开发 - Android开发 - 安卓 - 移动互联网门户

查看: 295|回复: 6

android配合viewpager实现可滑动的标签栏示例分享,android viewpager

[复制链接]

437

主题

789

帖子

1959

安币

手工艺人

发表于 2018-2-10 10:31:52 | 显示全部楼层 |阅读模式

            

        复制代码 代码如下:package com.example.playtabtest.view;

        import com.example.playtabtest.r;

        import android.app.activity;import android.content.context;import android.support.v4.view.viewpager;import android.support.v4.view.viewpager.onpagechangelistener;import android.util.attributeset;import android.util.displaymetrics;import android.view.layoutinflater;import android.view.view;import android.view.viewgroup;import android.view.animation.linearinterpolator;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;

        public class synchorizontalscrollview extends horizontalscrollview {

        private view view;private imageview leftimage;private imageview rightimage;private int windowwitdh = 0;private activity mcontext;private radiogroup rg_nav_content;private imageview iv_nav_indicator;private layoutinflater minflater;private int indicatorwidth;// 每个标签所占的宽度private int currentindicatorleft = 0;// 当前所在标签页面的位移private viewpager mviewpager;//与本view相关联的viewpager

        public synchorizontalscrollview(context context) {super(context);// todo auto-generated constructor stub}

        public synchorizontalscrollview(context context, attributeset attrs) {super(context, attrs);// todo auto-generated constructor stub}

        /** *  * @param mviewpager与本view关联的viewpager * @param leftimage左箭头 * @param rightimage右箭头 * @param tabtitle 标签数组,对应各个标签的名称 * @param count一页显示的标签数 * @param context */public void setsomeparam(viewpager mviewpager, imageview leftimage,imageview rightimage, string[] tabtitle, int count, activity context) {this.mcontext = context;this.mviewpager = mviewpager;minflater = layoutinflater.from(context);this.view = minflater.inflate(r.layout.sync_hsv_item, null);this.addview(view);this.leftimage = leftimage;this.rightimage = rightimage;displaymetrics dm = new displaymetrics();context.getwindowmanager().getdefaultdisplay().getmetrics(dm);windowwitdh = dm.widthpixels;indicatorwidth = windowwitdh / count;init(tabtitle);this.invalidate();}

        private void init(string[] tabtitle) {rg_nav_content = (radiogroup) view.findviewbyid(r.id.rg_nav_content);iv_nav_indicator = (imageview) view.findviewbyid(r.id.iv_nav_indicator);initindicatorwidth();initnavigationhsv(tabtitle);setlistener();}

        // 初始化滑动下标的宽private void initindicatorwidth() {viewgroup.layoutparams cursor_params = iv_nav_indicator.getlayoutparams();cursor_params.width = indicatorwidth;iv_nav_indicator.setlayoutparams(cursor_params);}

        // 添加顶部标签private void initnavigationhsv(string[] tabtitle) {rg_nav_content.removeallviews();for (int i = 0; i < tabtitle.length; i++) {radiobutton rb = (radiobutton) minflater.inflate(r.layout.nav_radiogroup_item, null);rb.setid(i);rb.settext(tabtitle);rb.setlayoutparams(new layoutparams(indicatorwidth,layoutparams.match_parent));rg_nav_content.addview(rb);}

        }

        private void setlistener() {rg_nav_content.setoncheckedchangelistener(new oncheckedchangelistener() {@overridepublic void oncheckedchanged(radiogroup group, int checkedid) {if (rg_nav_content.getchildat(checkedid) != null) {translateanimation animation = new translateanimation(currentindicatorleft,((radiobutton) rg_nav_content.getchildat(checkedid)).getleft(),0f, 0f);animation.setinterpolator(new linearinterpolator());animation.setduration(100);animation.setfillafter(true);

        // 执行位移动画iv_nav_indicator.startanimation(animation);mviewpager.setcurrentitem(checkedid); // viewpager// 跟随一起 切换// 记录当前 下标的距最左侧的 距离currentindicatorleft = ((radiobutton) rg_nav_content.getchildat(checkedid)).getleft();smoothscrollto((checkedid > 1 ? ((radiobutton) rg_nav_content.getchildat(checkedid)).getleft(): 0)- ((radiobutton) rg_nav_content.getchildat(2)).getleft(),0);}}});}

        /** * 模拟点击事件,供外部调用 * @param position */public void performlabelclick(int position) {if (rg_nav_content != null && rg_nav_content.getchildcount() > position) {((radiobutton) rg_nav_content.getchildat(position)).performclick();}}

        // 显示和隐藏左右两边的箭头protected void onscrollchanged(int l, int t, int oldl, int oldt) {super.onscrollchanged(l, t, oldl, oldt);if (!mcontext.isfinishing() && view != null && rightimage != null&& leftimage != null) {if (view.getwidth() <= windowwitdh) {leftimage.setvisibility(view.gone);rightimage.setvisibility(view.gone);} else {if (l == 0) {leftimage.setvisibility(view.gone);rightimage.setvisibility(view.visible);} else if (view.getwidth() - l == windowwitdh) {leftimage.setvisibility(view.visible);rightimage.setvisibility(view.gone);} else {leftimage.setvisibility(view.visible);rightimage.setvisibility(view.visible);}}}}}

        复制代码 代码如下:<?xml version="1.0" encoding="utf-8"?><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <relativelayout android:id="@+id/rl_nav" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="#5ab0eb" >

         <radiogroup android:id="@+id/rg_nav_content" android:layout_width="fill_parent" android:layout_height="38dip" android:layout_alignparenttop="true" android:background="#f2f2f2" android:orientation="horizontal" > </radiogroup>

         <imageview android:id="@+id/iv_nav_indicator" android:layout_width="1dip" android:layout_height="5dip" android:layout_alignparentbottom="true" android:background="#5ab0eb" android:contentdescription="@string/nav_desc" android:scaletype="matrix" /> </relativelayout>

        </linearlayout>



        

21

主题

9159

帖子

2064

安币

Android大神

Rank: 6Rank: 6

发表于 2018-2-11 16:54:43 | 显示全部楼层
感谢分享,安卓巴士有你更精彩:lol

7

主题

9258

帖子

1986

安币

Android大神

Rank: 6Rank: 6

发表于 2018-2-12 17:36:24 | 显示全部楼层
不错不错,楼主辛苦了。。。

27

主题

9358

帖子

1893

安币

Android大神

Rank: 6Rank: 6

发表于 2018-2-13 04:22:15 | 显示全部楼层
支持,感谢,祝巴士越来越好~

440

主题

740

帖子

1993

安币

手工艺人

发表于 2018-2-13 12:15:38 | 显示全部楼层
楼主是好人,回个帖会有安币吗?

322

主题

607

帖子

721

安币

手工艺人

发表于 2018-2-13 21:34:59 | 显示全部楼层
感谢大神~

2

主题

9303

帖子

2104

安币

Android大神

Rank: 6Rank: 6

QQ达人

发表于 2018-2-14 00:47:23 | 显示全部楼层
感谢分享,楼主V5~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

站长推荐

通过邮件订阅最新安卓weekly信息
上一条 /4 下一条

下载安卓巴士客户端

全国最大的安卓开发者社区
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 下载客户端|申请友链|手机版|站点统计|安卓巴士 ( 粤ICP备15117877号 )

快速回复 返回顶部 返回列表