Android導航選單橫向左右滑動並和下方的控制元件實現聯動
這個是美團網個人訂單的效果,找了很多地方都沒找到,自己研究了兩天終於弄出來了^_^,有什麼問題希望大家指出來,謝謝。
原始碼下載 這裡有http://www.eoeandroid.com/thread-175041-1-1.html
實現原理是上方使用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(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 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 == 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) {
- mViewPager.setCurrentItem(5);
- }
- }
- }
- }
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
相關文章
- 直播app開發,滑動式的頂部導航欄(左右橫向滑動)APP
- js橫向滑動摺疊導航選單程式碼例項JS
- jQuery滑動導航選單jQuery
- css實現立體效果橫向導航選單CSS
- 實現左側導航和橫向導航
- android的左右側滑選單實現Android
- Vue實現左右選單聯動實現(更新)Vue
- iOS 可以縱向橫向滑動的表格實現iOS
- jQuery背景滑動跟隨的導航選單jQuery
- 透明層滑動跟隨導航選單
- CSS3橫向導航選單效果CSSS3
- JavaScript橫向二級導航選單效果JavaScript
- Android左右滑動效果的程式碼實現Android
- 粘性控制元件,滑動停留StickLayout(導航欄滑動停留)控制元件
- 安卓中如何實現滑動導航安卓
- 使用 jQuery 和 CSS3 製作滑動導航選單jQueryCSSS3
- javascript底部具有跟隨效果的橫向導航選單JavaScript
- css3實現動態導航選單CSSS3
- css3滑鼠懸浮背景滑動導航選單CSSS3
- CSS製作橫向導航選單例項程式碼CSS單例
- 如何讓 fixed 定位的導航條可橫向滾動?
- CSS 動態導航選單CSS
- 滑鼠懸浮中英文切換橫向導航選單
- Android 禁止ViewPager左右滑動AndroidViewpager
- HTML橫向導航欄HTML
- 成品直播原始碼,頂部導航欄部分支援左右滑動原始碼
- UI介面微信底部(ViewPager實現Tab,左右滑動+底部點選)UIViewpager
- 僅2步實現 拜拜 漢堡導航欄效果~ 全新底部導航互動(滑動隱藏)
- 聯動選單的實現方案探索
- 微信小程式實現卡片左右滑動效果微信小程式
- ViewPager實現左右無限迴圈滑動Viewpager
- (十)如果實現滑動展示選單效果
- 滑鼠懸浮導航選單底部出現動畫橫線動畫
- android 全域性頁面滑動返回聯動效果的實現Android
- css3實現的水平立體動態導航選單效果CSSS3
- Vue.js+cube-ui(Scroll元件)實現類似頭條效果的橫向滾動導航條Vue.jsUI元件
- 考拉Android全域性滑動返回及聯動效果的實現Android
- 小程式外賣專案實踐之-左右選單聯動