亲宝软件园·资讯

展开

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

人气:0
想了解android配合viewpager实现可滑动的标签栏示例讲解的相关内容吗,在本文为您仔细讲解android实现可滑动的标签栏的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:标签栏,viewpager,下面大家一起来学习吧。

复制代码 代码如下:

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" />

加载全部内容

相关教程
猜你喜欢
用户评论