android配合viewpager實(shí)現(xiàn)可滑動(dòng)的標(biāo)簽欄示例分享
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;// 每個(gè)標(biāo)簽所占的寬度
private int currentIndicatorLeft = 0;// 當(dāng)前所在標(biāo)簽頁面的位移
private ViewPager mViewPager;//與本view相關(guān)聯(lián)的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關(guān)聯(lián)的viewpager
* @param leftImage左箭頭
* @param rightImage右箭頭
* @param tabTitle 標(biāo)簽數(shù)組,對(duì)應(yīng)各個(gè)標(biāo)簽的名稱
* @param count一頁顯示的標(biāo)簽數(shù)
* @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();
}
// 初始化滑動(dòng)下標(biāo)的寬
private void initIndicatorWidth() {
ViewGroup.LayoutParams cursor_Params = iv_nav_indicator
.getLayoutParams();
cursor_Params.width = indicatorWidth;
iv_nav_indicator.setLayoutParams(cursor_Params);
}
// 添加頂部標(biāo)簽
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);
// 執(zhí)行位移動(dòng)畫
iv_nav_indicator.startAnimation(animation);
mViewPager.setCurrentItem(checkedId); // ViewPager
// 跟隨一起 切換
// 記錄當(dāng)前 下標(biāo)的距最左側(cè)的 距離
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);
}
}
});
}
/**
* 模擬點(diǎn)擊事件,供外部調(diào)用
* @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" />
相關(guān)文章
Android仿支付寶中余額寶的數(shù)字動(dòng)畫效果
最近因?yàn)楣ぷ餍枰叻掠囝~寶數(shù)字動(dòng)畫效果,達(dá)到炫酷的數(shù)字動(dòng)畫效果,所以寫出了分享給大家,有需要的朋友可以直接拿來用,下面一起來看看。2016-08-08
android 獲取手機(jī)GSM/CDMA信號(hào)信息,并獲得基站信息的方法
下面小編就為大家?guī)硪黄猘ndroid 獲取手機(jī)GSM/CDMA信號(hào)信息,并獲得基站信息的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
Android多線程處理機(jī)制中的Handler使用介紹
本文將為大家介紹下Android的Handler的使用方法,Handler可以發(fā)送Messsage和Runnable對(duì)象到與其相關(guān)聯(lián)的線程的消息隊(duì)列,感興趣的朋友可以了解下哈2013-06-06
Android 開源項(xiàng)目側(cè)邊欄菜單(SlidingMenu)使用詳解
SlidingMenu的是一種比較新的設(shè)置界面或配置界面效果,在主界面左滑或者右滑出現(xiàn)設(shè)置界面,能方便的進(jìn)行各種操作.目前有大量的應(yīng)用都在使用這一效果。如Evernote、Google+、Foursquare等,國內(nèi)的豌豆夾,人人,360手機(jī)助手等都使用SlidingMenu的界面方案。2016-05-05
Android?App跳轉(zhuǎn)微信小程序踩坑實(shí)戰(zhàn)
現(xiàn)在市面上很多的應(yīng)用都可以實(shí)現(xiàn)相互跳轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于Android?App跳轉(zhuǎn)微信小程序踩坑的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
android 開發(fā)教程之日歷項(xiàng)目實(shí)踐(二)
決定開始學(xué)習(xí) Android 平臺(tái)下的軟件開發(fā),以日歷作為實(shí)踐項(xiàng)目,進(jìn)行一周后,基本完成,有需要的朋友可以參考下2013-01-01
android用鬧鐘定時(shí)做http請(qǐng)求推送的解決方案
這篇文章主要為大家詳細(xì)介紹了android用鬧鐘定時(shí)做http請(qǐng)求推送的解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
ListView滑動(dòng)隱藏顯示ToolBar的實(shí)例
下面小編就為大家分享一篇ListView滑動(dòng)隱藏顯示ToolBar的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Kotlin作用域函數(shù)應(yīng)用詳細(xì)介紹
作用域函數(shù):是Kotlin標(biāo)準(zhǔn)庫中的內(nèi)聯(lián)函數(shù),作用在對(duì)象上時(shí),執(zhí)行給定的block代碼塊。可以在block代碼塊中通過it,this代表當(dāng)前對(duì)象,進(jìn)行代碼邏輯處理2022-08-08
android Gallery組件實(shí)現(xiàn)的iPhone圖片滑動(dòng)效果實(shí)例
這篇文章主要介紹了android Gallery組件實(shí)現(xiàn)的iPhone圖片滑動(dòng)效果實(shí)例,即相冊(cè)內(nèi)的圖片實(shí)現(xiàn)可左右滑動(dòng)的效果,需要的朋友可以參考下2014-07-07

