Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android配合viewpager實現可滑動的標簽欄示例分享

android配合viewpager實現可滑動的標簽欄示例分享

編輯:關於Android編程

復制代碼 代碼如下:
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" />

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved