位置: 首页>> android HorizontalScrollView和ViewPager联动效果

android HorizontalScrollView和ViewPager联动效果

困扰我一个星期的问题解决了,为了实现这个效果,我走了多少的弯路。菜鸟就是菜鸟,差一点就歪在了Intent这个地方,幸好有个高手救了一命。
下面我们来看看这个效果:当然在网上也有很多这种例子
31f276b8-2ec8-3905-90c2-81380edfa100
看下代码:
package com.yinlans;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationSet;
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;
/**
* HorizontalScrollView和ViewPager联动效果
* 上面为HorizontalScrollView,下面为ViewPager
*/
public class MainActivity extends Activity implements OnCheckedChangeListener{

private RadioGroup mRadioGroup;
private RadioButton mRadioButton1;
private RadioButton mRadioButton2;
private RadioButton mRadioButton3;
private RadioButton mRadioButton4;
private RadioButton mRadioButton5;
private RadioButton mRadioButton6;
private ImageView mImageView;
private float mCurrentCheckedRadioLeft;//当前被选中的RadioButton距离左侧的距离
private HorizontalScrollView mHorizontalScrollView;//上面的水平滚动控件
private ViewPager mViewPager; //下方的可横向拖动的控件
private ArrayList mViews;//用来存放下方滚动的layout(layout_1,layout_2,layout_3)

LocalActivityManager manager = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

manager = new LocalActivityManager(this, true);
manager.dispatchCreate(savedInstanceState);

iniController();
iniListener();
iniVariable();

mRadioButton1.setChecked(true);
mViewPager.setCurrentItem(1);
mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();

}

private View getView(String id, Intent intent) {
return manager.startActivity(id, intent).getDecorView();
}

private void iniVariable() {
Intent intent = new Intent(this,Tab01Activity.class);
Intent intent1 = new Intent(this,Tab02Activity.class);
Intent intent2= new Intent(this,Tab03Activity.class);
Intent intent3= new Intent(this,Tab04Activity.class);
Intent intent4= new Intent(this,Tab05Activity.class);
Intent intent5= new Intent(this,Tab05Activity.class);
Intent intent6= new Intent(this,Tab05Activity.class);

mViews = new ArrayList();

mViews.add(getView(“A”, intent));
mViews.add(getView(“B”, intent1));
mViews.add(getView(“C”, intent2));
mViews.add(getView(“D”, intent3));
mViews.add(getView(“E”, intent4));
mViews.add(getView(“F”, intent5));
mViews.add(getView(“G”, intent6));

mViewPager.setAdapter(new MyPagerAdapter());//设置ViewPager的适配器
}

/**
* RadioGroup点击CheckedChanged监听
*/
@SuppressLint(“NewApi”)
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

AnimationSet _AnimationSet = new AnimationSet(true);
TranslateAnimation _TranslateAnimation;

Log.i(“zj”, “checkedid=”+checkedId);
if (checkedId == R.id.btn1) {
_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo1), 0f, 0f);
_AnimationSet.addAnimation(_TranslateAnimation);
_AnimationSet.setFillBefore(false);
_AnimationSet.setFillAfter(true);
_AnimationSet.setDuration(100);

mImageView.startAnimation(_AnimationSet);//开始上面蓝色横条图片的动画切换
mViewPager.setCurrentItem(1);//让下方ViewPager跟随上面的HorizontalScrollView切换
}else if (checkedId == R.id.btn2) {
_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo2), 0f, 0f);

_AnimationSet.addAnimation(_TranslateAnimation);
_AnimationSet.setFillBefore(false);
_AnimationSet.setFillAfter(true);
_AnimationSet.setDuration(100);

//mImageView.bringToFront();
mImageView.startAnimation(_AnimationSet);

mViewPager.setCurrentItem(2);
}else if (checkedId == R.id.btn3) {
_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo3), 0f, 0f);

_AnimationSet.addAnimation(_TranslateAnimation);
_AnimationSet.setFillBefore(false);
_AnimationSet.setFillAfter(true);
_AnimationSet.setDuration(100);

//mImageView.bringToFront();
mImageView.startAnimation(_AnimationSet);

mViewPager.setCurrentItem(3);
}else if (checkedId == R.id.btn4) {
_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo4), 0f, 0f);

_AnimationSet.addAnimation(_TranslateAnimation);
_AnimationSet.setFillBefore(false);
_AnimationSet.setFillAfter(true);
_AnimationSet.setDuration(100);

//mImageView.bringToFront();
mImageView.startAnimation(_AnimationSet);
mViewPager.setCurrentItem(4);
}else if (checkedId == R.id.btn5) {
_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo5), 0f, 0f);

_AnimationSet.addAnimation(_TranslateAnimation);
_AnimationSet.setFillBefore(false);
_AnimationSet.setFillAfter(true);
_AnimationSet.setDuration(100);

//mImageView.bringToFront();
mImageView.startAnimation(_AnimationSet);

mViewPager.setCurrentItem(5);
}else if (checkedId == R.id.btn6) {
_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo6), 0f, 0f);

_AnimationSet.addAnimation(_TranslateAnimation);
_AnimationSet.setFillBefore(false);
_AnimationSet.setFillAfter(true);
_AnimationSet.setDuration(100);

//mImageView.bringToFront();
mImageView.startAnimation(_AnimationSet);

mViewPager.setCurrentItem(6);
}

mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();//更新当前蓝色横条距离左边的距离

Log.i(“zj”, “getCurrentCheckedRadioLeft=”+getCurrentCheckedRadioLeft());
Log.i(“zj”, “getDimension=”+getResources().getDimension(R.dimen.rdo2));

mHorizontalScrollView.smoothScrollTo((int)mCurrentCheckedRadioLeft-(int)getResources().getDimension(R.dimen.rdo2), 0);
}

/**
* 获得当前被选中的RadioButton距离左侧的距离
*/
private float getCurrentCheckedRadioLeft() {
// TODO Auto-generated method stub
if (mRadioButton1.isChecked()) {
return getResources().getDimension(R.dimen.rdo1);
}else if (mRadioButton2.isChecked()) {
return getResources().getDimension(R.dimen.rdo2);
}else if (mRadioButton3.isChecked()) {
return getResources().getDimension(R.dimen.rdo3);
}else if (mRadioButton4.isChecked()) {
return getResources().getDimension(R.dimen.rdo4);
}else if (mRadioButton5.isChecked()) {
return getResources().getDimension(R.dimen.rdo5);
}else if (mRadioButton6.isChecked()) {
return getResources().getDimension(R.dimen.rdo6);
}
return 0f;
}

private void iniListener() {
// TODO Auto-generated method stub
mRadioGroup.setOnCheckedChangeListener(this);
mViewPager.setOnPageChangeListener(new MyPagerOnPageChangeListener());
}

private void iniController() {
// TODO Auto-generated method stub
mRadioGroup = (RadioGroup)findViewById(R.id.radioGroup);
mRadioButton1 = (RadioButton)findViewById(R.id.btn1);
mRadioButton2 = (RadioButton)findViewById(R.id.btn2);
mRadioButton3 = (RadioButton)findViewById(R.id.btn3);
mRadioButton4 = (RadioButton)findViewById(R.id.btn4);
mRadioButton5 = (RadioButton)findViewById(R.id.btn5);
mRadioButton6 = (RadioButton)findViewById(R.id.btn6);

mImageView = (ImageView)findViewById(R.id.img1);

mHorizontalScrollView = (HorizontalScrollView)findViewById(R.id.horizontalScrollView);

mViewPager = (ViewPager)findViewById(R.id.pager);
}

/**
* ViewPager的适配器
*/
private class MyPagerAdapter extends PagerAdapter{

@Override
public void destroyItem(View v, int position, Object obj) {
// TODO Auto-generated method stub
((ViewPager)v).removeView(mViews.get(position));
}

@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mViews.size();
}

@Override
public Object instantiateItem(View v, int position) {
((ViewPager)v).addView(mViews.get(position));
return mViews.get(position);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}

@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}

@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}

}
/**
* ViewPager的PageChangeListener(页面改变的监听器)
*/
private class MyPagerOnPageChangeListener implements OnPageChangeListener{

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
/**
* 滑动ViewPager的时候,让上方的HorizontalScrollView自动切换
*/
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
//Log.i(“zj”, “position=”+position);

if (position == 0) {
mViewPager.setCurrentItem(1);
}else if (position == 1) {
mRadioButton1.performClick();
}else if (position == 2) {
mRadioButton2.performClick();
}else if (position == 3) {
mRadioButton3.performClick();
}else if (position == 4) {
mRadioButton4.performClick();
}else if (position == 5) {
mRadioButton5.performClick();
}else if (position == 6) {
mRadioButton6.performClick();
}else if (position == 7) {
mViewPager.setCurrentItem(6);
}
}
}

}
XML布局文件:
< ?xml version=”1.0″ encoding=”utf-8″?> <?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<HorizontalScrollView
android:layout_width=”fill_parent”
android:layout_height=”50dp”
android:fadingEdge=”@null”
android:scrollbars=”none”
android:background=”#555555″
android:id=”@+id/horizontalScrollView”
>
<RelativeLayout
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:background=”#33b5e5″
>
<RadioGroup
android:id=”@+id/radioGroup”
android:layout_width=”fill_parent”
android:layout_height=”49dp”
android:orientation=”horizontal”
android:layout_alignParentTop=”true”
>
<RadioButton
style=”@style/radioButton”
android:text=”one”
android:id=”@+id/btn1″
/>
<RadioButton
style=”@style/radioButton”
android:text=”two”
android:id=”@+id/btn2″
/>
<RadioButton
style=”@style/radioButton”
android:text=”three”
android:id=”@+id/btn3″
/>
<RadioButton
style=”@style/radioButton”
android:text=”four”
android:id=”@+id/btn4″
/>
<RadioButton
style=”@style/radioButton”
android:text=”five”
android:id=”@+id/btn5″
/>
<RadioButton
style=”@style/radioButton”
android:text=”six”
android:id=”@+id/btn6″
/>
</RadioGroup>
<ImageView
android:id=”@+id/img1″
android:layout_width=”100dp”
android:layout_height=”4dp”
android:background=”#33b5e5″
android:layout_alignParentBottom=”true”
/>
</RelativeLayout>
</HorizontalScrollView>

<android.support.v4.view.ViewPager
android:id=”@+id/pager”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_weight=”1″ >
</android.support.v4.view.ViewPager>

</LinearLayout>
下载链接:http://download.csdn.net/detail/zflwzs/5482595

One Response To This Post So Far(Rss)

  1. 幼儿园文化网 | #1
    2013 年 6 月 6 日 at 下午 2:10

    好吧 我承认完全看不懂

Post a comment