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

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

概述复制代码代码如下:packagecom.example.playtabtest.view;importcom.example.playtabtest.R;importandroid.app.Activity;importandroid.content.Context;importandroid.support.v4.view.ViewPager;importandroid.su

复制代码 代码如下:
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[i]);
   rb.setLayoutParams(new LayoutParams(indicatorWIDth,
     LayoutParams.MATCH_PARENT));
   rg_nav_content.addVIEw(rb);
  }

 }

 private voID setListener() {
  rg_nav_content
    .setonCheckedchangelistener(new OnCheckedchangelistener() {
     @OverrIDe
     public 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>

复制代码 代码如下:
<?xml version="1.0" enCoding="utf-8"?>
<Radiobutton xmlns:androID="http://schemas.androID.com/apk/res/androID"
    androID:layout_wIDth="0dip"
    androID:layout_height="fill_parent"
    androID:background="#F2F2F2"
    androID:button="@null"
    androID:checked="true"
    androID:gravity="center"
    androID:text=""
    androID:textcolor="@drawable/rb_blue_bg"
    androID:textSize="14.0dip" />

总结

以上是内存溢出为你收集整理的android配合viewpager实现可滑动的标签栏示例分享全部内容,希望文章能够帮你解决android配合viewpager实现可滑动的标签栏示例分享所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141641.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存