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

实现原理是上方使用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. bash与sh的区别

    在shell脚本的开头往往有一句话来定义使用哪种sh解释器来解释脚本.目前研发送测的shell脚本中主要有以下两种方式:(1) #!/bin/sh(2) #!/bin/bash在这里求教同福客栈的各位 ...

  2. 【转】Android Service完全解析,关于服务你所需知道的一切(下) ---- 不错

    原文网址:http://blog.csdn.net/guolin_blog/article/details/9797169 转载请注册出处:http://blog.csdn.net/guolin_bl ...

  3. 哲学家用餐问题的几个解法(c语言实现)

    参考资料: 1.维基百科:哲学家用餐问题 2.Windows的多线程编程

  4. JS监听组合按键

    有些时候,我们需要在网页上,增加一些快捷按键,方便用户使用一些常用的操作,比如:保存,撤销,复制.粘贴等等. 下面简单梳理一下思路: 我们所熟悉的按键有这么集中类型: 单独的按键操作,如:delete ...

  5. linux系统批量无人值守安装

    一:批量无人值守安安装原理 利用DHCP TFTP FTP和PXE技术实现批量安装系统,首先在主server上安装好DHCP TFTP和FTP服务,client通过网卡的PXE技术获取到IP地址和TF ...

  6. javascript正則表達式 &quot;\b&quot;问题

    preface 昨晚在看<javascript权威指南>后.看见作者自己封装一个兼容全部浏览器的山寨HTML5新API classLIst类.自己想了想认为自己也要去玩一下.可是能力还是有 ...

  7. myeclipse自动补全设置

    第一步: windows -->preference -->java -->editor -->content Assist --> auto activation -- ...

  8. oracle之case when

    oracle case when 的用法 http://www.cnblogs.com/xiaowu/archive/2011/08/17/2143445.html(转) http://www.cnb ...

  9. iOS程序的加载过程

    1.执行main函数2.执行UIApplicationMain函数1> 创建一个UIApplication对象(UIApplication是整个程序的象征)一个应用只有一个application ...

  10. vsftp虚拟用户登录配置详解

    一.安装:1.安装Vsftpd服务:# yum install vsftpd 2.安装DB4部件包:这里要特别安装一个db4的包,用来支持文件数据库.# yum install db4-utils 二 ...