scrollview和viewpager联动

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <HorizontalScrollView
        android:id="@+id/sc"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:background="#F2F2F2"
        android:scrollbars="none">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RadioGroup
                android:id="@+id/rg"
                android:layout_width="match_parent"
                android:layout_height="38dip"
                android:orientation="horizontal" >
            </RadioGroup>
            <ImageView
                android:id="@+id/img"
                android:layout_width="1dip"
                android:layout_height="5dip"
                android:layout_alignParentBottom="true"
                android:background="#0f0"
                android:scaleType="matrix"
                />
        </RelativeLayout>
    </HorizontalScrollView>

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/sc" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>

fragment的xml。。。。fragment_selection_common

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="这是CommonUIFragment界面"
        />

    <TextView
        android:id="@+id/tv_tabName"
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:textSize="20sp"
        />

</LinearLayout>

selector选择器  rb_blue_bg

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#5AB0EB" />
    <item android:state_checked="false" android:color="#000000"/>
</selector>

radiobutton按钮  nav_radiogroup_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:background="#F2F2F2"
    android:button="@null"
    android:checked="true"
    android:gravity="center"
    android:text=""
    android:textColor="@drawable/rb_blue_bg"
    android:textSize="14.0dip" />

fragment

package com.bwie.test;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class CommonUIFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_selection_common, container, false);

        TextView tv_tabName = (TextView) rootView.findViewById(R.id.tv_tabName);

        Bundle bundle = getArguments();

        tv_tabName.setText(bundle.getString(MainActivity.ARGUMENTS_NAME, ""));

        return rootView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }

}

activity中

package com.bwie.test;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
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;

public class MainActivity extends FragmentActivity {

    public static final String ARGUMENTS_NAME = null;
    private ViewPager vp;
    private RadioGroup rg;
    private ImageView img;
    private int indicatorWidth;
    public static String[] tabTitle = { "选项1", "选项2", "选项3", "选项4", "选项5" };    //标题
    private LayoutInflater inflater;
    private HorizontalScrollView sc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = (ViewPager) findViewById(R.id.vp);
        rg = (RadioGroup) findViewById(R.id.rg);
        img = (ImageView) findViewById(R.id.img);
        sc = (HorizontalScrollView) findViewById(R.id.sc);
        initView();

        setlisenten();
    }

    private void setlisenten() {
        vp.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if(rg!=null && rg.getChildCount()>position){
                    ((RadioButton)rg.getChildAt(position)).performClick();
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            private int currentIndicatorLeft;

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(rg.getChildAt(checkedId)!=null){
//                    new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
                    TranslateAnimation animation = new TranslateAnimation(
                            currentIndicatorLeft ,
                            ((RadioButton) rg.getChildAt(checkedId)).getLeft(), 0f, 0f);
//                    animation.setInterpolator(new LinearInterpolator());
                    animation.setDuration(100);
                    animation.setFillAfter(true);

                    //执行位移动画
                    img.startAnimation(animation);

                    vp.setCurrentItem(checkedId);    //ViewPager 跟随一起 切换

                    //记录当前 下标的距最左侧的 距离
                    currentIndicatorLeft = ((RadioButton) rg.getChildAt(checkedId)).getLeft();

                     sc.smoothScrollTo(
                            (checkedId > 1 ? ((RadioButton) rg.getChildAt(checkedId)).getLeft() : 0) - ((RadioButton) rg.getChildAt(2)).getLeft(), 0);
                 }
            }

        });
    }

    private void initView() {
        //DisplayMetrics 类提供了一种关于显示的通用信息,如显示大小,分辨率和字体
        DisplayMetrics dm = new DisplayMetrics();
        //将当前窗口的一些信息放在DisplayMetrics类中,
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        //radiobutton的宽度
        indicatorWidth = dm.widthPixels / 4;
        LayoutParams params = img.getLayoutParams();
        params.width=indicatorWidth;

        img.setLayoutParams(params);
        //获取布局填充器
        inflater = LayoutInflater.from(this);
        initNavigationHSV();

        TabFragmentPagerAdapter mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager());
        vp.setAdapter(mAdapter);
    }

    private void initNavigationHSV() {
        rg.removeAllViews();
        for (int i = 0; i < tabTitle.length; i++) {
            RadioButton rb = (RadioButton) inflater.inflate(R.layout.nav_radiogroup_item, null);
            rb.setId(i);
            rb.setText(tabTitle[i]);
            rb.setLayoutParams(new LayoutParams(indicatorWidth,
                    LayoutParams.MATCH_PARENT));
            rg.addView(rb);
        }

    }

    class TabFragmentPagerAdapter extends FragmentPagerAdapter{

        private com.bwie.test.CommonUIFragment ft;

        public TabFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int arg0) {
            ft = new CommonUIFragment();

            Bundle args = new Bundle();
            args.putString(ARGUMENTS_NAME, tabTitle[arg0]);
            ft.setArguments(args);
            return ft;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return tabTitle.length;
        }

    }

}
时间: 2024-10-26 05:08:52

scrollview和viewpager联动的相关文章

(转)ViewPager,ScrollView 嵌套ViewPager滑动冲突解决

ViewPager,ScrollView 嵌套ViewPager滑动冲突解决 本篇主要讲解一下几个问题 粗略地介绍一下View的事件分发机制 解决事件滑动冲突的思路及方法 ScrollView 里面嵌套ViewPager导致的滑动冲突 ViewPager里面嵌套ViewPager 导致的滑动冲突 轮播图的几种实现方式 先看一下效果图 ScrollView里面嵌套ViewPager ViewPager里面嵌套ViewPager View的 事件分发机制 这篇博客大打算详细讲解View的事件分发机制

完美解决ScrollView嵌套ViewPager滑动失效和无法正常滑动冲突问题

/******************************************************************************* * Copyright 2011, 2012 Chris Banes. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the Li

【Android实战】ScrollView+GridView+ViewPager实现导航页

按照常规思路,实现导航页有专门的TabHost或ViewPager,但自定义空间不大,再者,自己想熟悉一下多重布局和动画效果的使用,因此采用这种ScrollView+GridView+ViewPager的效果. 其中比较麻烦的是GridView实现横向的加载,并且下面的滚动条随着滑动也得滚动和动态发生位置变化. public class MainAct extends FragmentActivity { ViewPager viewPager; HorizontalScrollView scr

改动ScrollView的滑动速度和解决ScrollView与ViewPager的冲突

话不多说,非常easy,能够从凝视中知道做法,直接上代码: 1.改动ScrollView的滑动速度: public class MyHorizontalScrollView extends HorizontalScrollView { public MyHorizontalScrollView(Context context) { super(context); } public MyHorizontalScrollView(Context context, AttributeSet attrs

ScrollView嵌套Viewpager和ListView的整合

1.ScrollView嵌套Viewpager解决滑动冲突: (1)重新ScrollView(法一) /**  * 能够兼容ViewPager的ScrollView  * @Description: 解决了ViewPager在ScrollView中的滑动反弹问题  */ public class ScrollViewExtend extends ScrollView {     // 滑动距离及坐标     private float xDistance, yDistance, xLast, y

修改ScrollView的滑动速度和解决ScrollView与ViewPager的冲突

话不多说,很简单,可以从注释中知道做法,直接上代码: 1.修改ScrollView的滑动速度: public class MyHorizontalScrollView extends HorizontalScrollView { public MyHorizontalScrollView(Context context) { super(context); } public MyHorizontalScrollView(Context context, AttributeSet attrs, i

ScrollView中ViewPager无法正常滑动问题

本文主要介绍如何解决ViewPager在ScrollView中滑动经常失效.无法正常滑动问题. 解决方法只需要在接近水平滚动时ScrollView不处理事件而交由其子View(即这里的ViewPager)处理即可,重写ScrollView的onInterceptTouchEvent函数,如下: package cc.newnews.view; import android.content.Context; import android.util.AttributeSet; import andr

解决ScrollView嵌套ViewPager,导致ViewPager不能滑动的问题

import java.lang.reflect.Field; import java.lang.reflect.Method; /** * 解决ScrollView嵌套ViewPager,导致ViewPager不能滑动的问题 */ public class CustomScrollView extends ScrollView { private GestureDetector mGestureDetector; private int Scroll_height = 0; private i

TabLayout 和ViewPager联动

文档位置:https://developer.android.google.cn/reference/android/support/design/widget/TabLayout.html 依赖 compile 'com.android.support:design:25.3.1' 简介 继承体系 java.lang.Object ? android.view.View ? android.view.ViewGroup ? android.widget.FrameLayout ? androi