这个是美团网个人订单的效果,找了很多地方都没找到,自己研究了两天终于弄出来了^_^,有什么问题希望大家指出来,谢谢。

实现原理是上方使用HorizontalScrollView这个可以水平横向拖动的控件,在其中加入了5个RadioButton;下方使用的是ViewPager,里面加入了7个Layout文件,其中第一个和最后一个为空,是为了实现拖到第一个屏幕的时候还能往外拖动的效果。

先看下效果,切换都是带动画效果的,并且点击上面最右边的标签时会自动滚动出后面的标签。

现在看一下代码:

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();
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();
/*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();//让下方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(); //mImageView.bringToFront();
mImageView.startAnimation(_AnimationSet); mViewPager.setCurrentItem();
}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(); //mImageView.bringToFront();
mImageView.startAnimation(_AnimationSet); mViewPager.setCurrentItem();
}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(); //mImageView.bringToFront();
mImageView.startAnimation(_AnimationSet);
mViewPager.setCurrentItem();
}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(); //mImageView.bringToFront();
mImageView.startAnimation(_AnimationSet); mViewPager.setCurrentItem();
} 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), );
} /**
* 获得当前被选中的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 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(页面改变的监听器)
* @author zj
* 2012-5-24 下午3:14:27
*/
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 == ) {
mViewPager.setCurrentItem();
}else if (position == ) {
mRadioButton1.performClick();
}else if (position == ) {
mRadioButton2.performClick();
}else if (position == ) {
mRadioButton3.performClick();
}else if (position == ) {
mRadioButton4.performClick();
}else if (position == ) {
mRadioButton5.performClick();
}else if (position == ) {
mViewPager.setCurrentItem();
}
} } }

XML文件:

<?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="match_parent"
android:layout_height="50dp"
android:fadingEdge="@null"
android:scrollbars="none"
android:background="#555555"
android:id="@+id/horizontalScrollView"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
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"
/>
</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="fill_parent"
/>
</LinearLayout>

第一次写这么多,希望转载的朋友能够尊重作者的劳动成果,加上转载地,谢谢。

http://zhengjiong.iteye.com/blog/1539951

Android导航菜单横向左右滑动并和下方的控件实现联动的更多相关文章

  1. Android 高级UI设计笔记11:Gallery(画廊控件)之Gallery基本使用

    1. 这里要向大家介绍Android控件Gallery(画廊控件) Gallery控件主要用于横向显示图像列表,不过按常规做法.Gallery组件只能有限地显示指定的图像.也就是说,如果为Galler ...

  2. Android开发系列(十八):自己定义控件样式在drawable目录下的XML实现

    在Android开发的过程中,我们常常须要对控件的样式做一下改变,能够通过用添加背景图片的方式进行改变,可是背景图片放多了肯定会使得APK文件变的非常大. 我们能够用自己定义属性shape来实现. s ...

  3. 【Android】13.0 UI开发(四)——列表控件RecyclerView的横向布局排列实现

    1.0 新建项目,由于ListView的局限性,RecyclerView是一种很好取代ListView的控件,可以灵活实现多种布局. 2.0 新建项目RecyclerviewTest,目录如下: 3. ...

  4. Android 滑动RecyclerView时隐藏部分控件

    在使用RecyclerView控件时,上下拖动控件时的时候,需要实时的隐藏与显示部分控件,已到达很好的用户体验.   原理很简单,当RecyclerView拖动至最上层时显示控件,当RecyclerV ...

  5. Android 高级UI设计笔记14:Gallery(画廊控件)之 3D图片浏览

    1. 利用Gallery组件实现 3D图片浏览器的功能,如下: 2. 下面是详细的实现过程如下: (1)这里我是测试性代码,我的图片是自己添加到res/drawable/目录下的,如下: 但是开发中不 ...

  6. 《Android群英传》读书笔记 (2) 第三章 控件架构与自定义控件详解 + 第四章 ListView使用技巧 + 第五章 Scroll分析

    第三章 Android控件架构与自定义控件详解 1.Android控件架构下图是UI界面架构图,每个Activity都有一个Window对象,通常是由PhoneWindow类来实现的.PhoneWin ...

  7. Android UI设计中一些不错的示例及第三方控件

    1.二级ListView自定义布局ExpandableListView http://pan.baidu.com/s/1mhlJh12 密码:nhc2 2.ListView实现各种动画效果ListVi ...

  8. Android实现Material Design风格的设置页面(滑动开关控件)

    前言 本文链接 http://blog.csdn.net/never_cxb/article/details/50763271 转载请注明出处 參考了这篇文章 Material Design 风格的设 ...

  9. 【Android】15.0 UI开发(六)——列表控件RecyclerView的网格布局排列实现

    1.0 列表控件RecyclerView的网格布局排列实现,关键词GridLayoutManager. LinearLayoutManager 实现顺序布局 GridLayoutManager 实现网 ...

随机推荐

  1. 【No system images installed for this target】的解决方式

    打开eclipse,新建安卓SDK模拟器时,选择完Target之后,再选择CPU/ABI时,默认为No system images installed for this target. 且无法编辑: ...

  2. BOT、BT、PPP形式介绍(3)

    PPP     20世纪90年代后,一种崭新的融资模式-PPP模式(Public-Private-Partnership,即“公共部门-私人企业-合作”的模式)在西方特别是欧洲流行起来,在公共基础设施 ...

  3. Fence Repair (POJ 3253)

    农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板长度为L1.L2.L3...LN,未切割前的木板长度恰好为切割后木板长度的总和.每次切断木板时,需要的开销为这块木板的长度.例如长度为 ...

  4. One手动玩转

    <preface p2 by Ruiy,我就在开头简单奇葩两句!> 老周被查,涉及到政治问题,我先就不聊了,但Ruiy叹那,都查到七*务了,土党唱哪一出! 能基本玩转OpenNebula都 ...

  5. JSON解析---初识

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式 全然独立于语言的文本格式 易于人阅读和编写 易于解析和生成 (网络传输速度快) JSON语法规则 数据在 ...

  6. Android应用开发提高系列(4)——Android动态加载(上)——加载未安装APK中的类

    前言 近期做换肤功能,由于换肤程度较高,受限于平台本身,实现起来较复杂,暂时搁置了该功能,但也积累了一些经验,将分两篇文章来写这部分的内容,欢迎交流! 关键字:Android动态加载 声明 欢迎转载, ...

  7. IOS 原生解析JSON 问题

    服务器----WebService 返回的是JSON数据 IOS解析报错: Error Domain=NSCocoaErrorDomain Code=3840 "Unable to conv ...

  8. gstreamer让playbin能够播放rtp over udp流数据

    最近一段时间在研究传屏低延迟传输相关的一些东西.本来想使用gstreamer来验证下rtp over udp传送h264 nal数据相关 的,结果发现竟然不能用playbin来播放rtp的数据!诚然, ...

  9. Socket学习笔记

    ..........(此处略去万万字)学习中曲折的过程不介绍了,直接说结果 我的学习方法,问自己三个问题,学习过程将围绕这三个问题进行 what:socket是什么 why:为什么要使用socket ...

  10. (转)[老老实实学WCF] 第二篇 配置WCF

    第二篇 配置WCF 在上一篇中,我们在一个控制台应用程序中编写了一个简单的WCF服务并承载了它.先回顾一下服务端的代码: using System; using System.Collections. ...