package com.zj.horizontalsrollview;
import java.util.ArrayList;
import android.app.Activity;
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.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
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;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
/**
* HorizontalScrollView和ViewPager联动效果
* 上面为HorizontalScrollView,下面为ViewPager
* @author zj
* 2012-5-23 下午1:07:06
*/
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 ImageView mImageView;
private float mCurrentCheckedRadioLeft;//当前被选中的RadioButton距离左侧的距离
private HorizontalScrollView mHorizontalScrollView;//上面的水平滚动控件
private ViewPager mViewPager; //下方的可横向拖动的控件
private ArrayList<View> mViews;//用来存放下方滚动的layout(layout_1,layout_2,layout_3)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iniController();
iniListener();
iniVariable();
mRadioButton1.setChecked(true);
mViewPager.setCurrentItem(1);
mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();
}
private void iniVariable() {
// TODO Auto-generated method stub
mViews = new ArrayList<View>();
mViews.add(getLayoutInflater().inflate(R.layout.layout_0, null));
mViews.add(getLayoutInflater().inflate(R.layout.layout_1, null));
mViews.add(getLayoutInflater().inflate(R.layout.layout_2, null));
mViews.add(getLayoutInflater().inflate(R.layout.layout_3, null));
mViews.add(getLayoutInflater().inflate(R.layout.layout_4, null));
mViews.add(getLayoutInflater().inflate(R.layout.layout_5, null));
mViews.add(getLayoutInflater().inflate(R.layout.layout_0, null));
mViewPager.setAdapter(new MyPagerAdapter());//设置ViewPager的适配器
}
/**
* RadioGroup点击CheckedChanged监听
*/
@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);
/*LayoutParams _LayoutParams1 = new LayoutParams(100, 4);
_LayoutParams1.setMargins(0, 0, 0, 0);
_LayoutParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);*/
//mImageView.bringToFront();
mImageView.startAnimation(_AnimationSet);//开始上面蓝色横条图片的动画切换
//mImageView.setLayoutParams(_LayoutParams1);
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);
}
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()) {
//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo1));
return getResources().getDimension(R.dimen.rdo1);
}else if (mRadioButton2.isChecked()) {
//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo2));
return getResources().getDimension(R.dimen.rdo2);
}else if (mRadioButton3.isChecked()) {
//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo3));
return getResources().getDimension(R.dimen.rdo3);
}else if (mRadioButton4.isChecked()) {
//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo4));
return getResources().getDimension(R.dimen.rdo4);
}else if (mRadioButton5.isChecked()) {
//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo5));
return getResources().getDimension(R.dimen.rdo5);
}
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);
mImageView = (ImageView)findViewById(R.id.img1);
mHorizontalScrollView = (HorizontalScrollView)findViewById(R.id.horizontalScrollView);
mViewPager = (ViewPager)findViewById(R.id.pager);
}
/**
* ViewPager的适配器
* @author zj
* 2012-5-24 下午2:26:57
*/
private class MyPagerAdapter extends PagerAdapter{
@Override
public void destroyItem(View v, int positio
没有合适的资源?快使用搜索试试~ 我知道了~
Android导航菜单横向左右滑动并和下方的控件实现联动
共42个文件
xml:16个
class:13个
png:3个
5星 · 超过95%的资源 需积分: 3 38 下载量 50 浏览量
2014-11-27
11:32:38
上传
评论 1
收藏 305KB RAR 举报
温馨提示
实现原理是上方使用HorizontalScrollView这个可以水平横向拖动的控件,在其中加入了5个RadioButton;下方使用的是ViewPager,里面加入了7个Layout文件,其中第一个和最后一个为空,是为了实现拖到第一个屏幕的时候还能往外拖动的效果。 先
资源推荐
资源详情
资源评论
收起资源包目录
ZJ_HorizontalScrollView.rar (42个子文件)
ZJ_HorizontalScrollView
bin
classes.dex 152KB
com
zj
horizontalsrollview
MainActivity.class 6KB
R$layout.class 610B
R$id.class 653B
R$drawable.class 500B
R$dimen.class 527B
R$attr.class 364B
MainActivity$MyPagerAdapter.class 2KB
R.class 809B
MainActivity$MyPagerOnPageChangeListener.class 2KB
R$string.class 451B
R$color.class 462B
R$anim.class 459B
R$style.class 422B
ZJ_HorizontalScrollView.apk 86KB
resources.ap_ 17KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable
radiobtn_selector.xml 269B
radiobtn_bg.xml 122B
values
colors.xml 161B
strings.xml 186B
styles.xml 482B
dimens.xml 247B
drawable-mdpi
icon.png 3KB
anim
slide_to_right.xml 218B
slide_to_left.xml 219B
layout
layout_1.xml 511B
layout_4.xml 511B
layout_3.xml 511B
layout_5.xml 511B
main.xml 2KB
layout_0.xml 291B
layout_2.xml 511B
assets
default.properties 362B
gen
com
zj
horizontalsrollview
R.java 2KB
proguard.cfg 1KB
src
com
zj
horizontalsrollview
MainActivity.java 10KB
.project 859B
.classpath 345B
lib
android-support-v4.jar 137KB
AndroidManifest.xml 692B
共 42 条
- 1
资源评论
- shaocpa2015-08-20非常好,稍微改下就可以用,记得把V4重新导入下,要不无法运行!
- Override笑看人生2017-05-06分是多了点,不过确实是我想要的!物超所值
- doupi20082017-07-05不错,可以参考,就是分太多了
- dayan52120012018-06-26资源不错,可以参考
乐逍遥二
- 粉丝: 24
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功