滑动,实现tab动态切换

html

<div class="headmm"  id="head2">

js

//获取head2元素的高度

head2hei:function () {

      return document.getElementById('head2').offsetHeight;

    }

//点击导航后控制滚动条移动的位置

 changeFirstNav: function (data) {
      var idheight = document.getElementById(data).offsetTop
      let len = this.head2hei
      if(!this.firstNavShowTop ){
        len= len +parseInt(this.head2hei)
      };
      document.documentElement.scrollTop = document.body.scrollTop = idheight - len
      // this.firstNavShowTop = true
//    document.getElementById(data).scrollIntoView(true)
      this.pageSlideControl(this.head2hei )

    },


    //根据滚动条跟顶部到某元素的距比较现实导航动态切换
    pageSlideControl: function (data) {
      let that = this
      window.onscroll = function () {
        let banner = document.getElementById('banner').offsetTop
        let afterServerSection = document.getElementById('afterServerSection').offsetTop
        let problemSection = document.getElementById('problemSection').offsetTop
        let t = document.documentElement.scrollTop  || document.body.scrollTop
       if(data){
          t = t + parseInt(data)
       }
       let t2= t
       if(!that.firstNavShowTop){
         t2 = t2 + parseInt(data)
       }
        // 第二层导航滑动在可视窗口里面,则固定在最上面
        if ( banner < 0 || t < banner) {
          that.firstNavShowTop = false
        } else {
          that.firstNavShowTop = true
        }


        // 控制滑块的地址 详情
        if (afterServerSection > t2) {
          that.firstNavList.productDetailSection = true
        } else {
          that.firstNavList.productDetailSection = false
        }
        // 控制滑块的地址 售后
        if (afterServerSection <= t2  && t2 < problemSection) {
          that.firstNavList.afterServerSection = true
        } else {
          that.firstNavList.afterServerSection = false
        }
        // 控制滑块的地址 常见问题
        if (t2  >= problemSection) {
          that.firstNavList.problemSection = true
        } else {
          that.firstNavList.problemSection = false
        }
      }
    },
可以滑动的界面切换tabhost package com.caigang.test; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; import android.widget.TabWidget; public class MainActivity extends TabActivity implements OnTabChangeListener,OnGestureListener { private GestureDetector gestureDetector; private FrameLayout frameLayout; private CustomTabHost tabHost; private TabWidget tabWidget; private static final int FLEEP_DISTANCE = 120; /** 记录当前分页ID */ private int currentTabID = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tabHost = (CustomTabHost) findViewById(android.R.id.tabhost); tabWidget = (TabWidget) findViewById(android.R.id.tabs); tabHost.setOnTabChangedListener(this); init(); gestureDetector = new GestureDetector(this); new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } return false; } }; frameLayout = tabHost.getTabContentView(); } private void init() { setIndicator(R.drawable.icon1, 0, new Intent(this,TabActivity01.class)); setIndicator(R.drawable.icon2, 1, new Intent(this,TabActivity02.class)); setIndicator(R.drawable.icon3, 2, new Intent(this,TabActivity03.class)); setIndicator(R.drawable.icon4, 3, new Intent(this,TabActivity04.class)); } private void setIndicator(int icon, int tabId, Intent intent) { View localView = LayoutInflater.from(this.tabHost.getContext()).inflate(R.layout.tab, null); ((ImageView) localView.findViewById(R.id.tab_image)).setBackgroundResource(icon); String str = String.valueOf(tabId); TabHost.TabSpec localTabSpec = tabHost.newTabSpec(str).setIndicator(localView).setContent(intent); tabHost.addTab(localTabSpec); } @Override public void onTabChanged(String tabId) { //tabId值为要切换到的tab页的索引位置 int tabID = Integer.valueOf(tabId); for (int i = 0; i < tabWidget.getChildCount(); i++) { if (i == tabID) { tabWidget.getChildAt(Integer.valueOf(i)).setBackgroundColor(R.color.bule); } else { tabWidget.getChildAt(Integer.valueOf(i)).setBackgroundColor(R.color.white); } } } @Override public boolean dispatchTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { event.setAction(MotionEvent.ACTION_CANCEL); } return super.dispatchTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { if (e1.getX() - e2.getX() <= (-FLEEP_DISTANCE)) {//从左向右滑动 currentTabID = tabHost.getCurrentTab() - 1; if (currentTabID < 0) { currentTabID = tabHost.getTabCount() - 1; } } else if (e1.getX() - e2.getX() >= FLEEP_DISTANCE) {//从右向左滑动 currentTabID = tabHost.getCurrentTab() + 1; if (currentTabID >= tabHost.getTabCount()) { currentTabID = 0; } } tabHost.setCurrentTab(currentTabID); return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值