ANDROID笔记:使用动画替代viewpager的header

ANDROID笔记:使用动画替代viewpager的header

 

package com.example.baofenanim;



import java.util.ArrayList;

import java.util.List;



import android.app.Activity;

import android.os.Bundle;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;

import android.support.v4.view.ViewPager.OnPageChangeListener;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.view.animation.TranslateAnimation;

import android.widget.Button;

import android.widget.HorizontalScrollView;

import android.widget.ImageView;



public class MainActivity extends Activity {

    LayoutInflater inflater;

    // 测试的数据集

    List<View> list = new ArrayList<View>();

    // 滑块状态

    int state = 0;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        inflater = LayoutInflater.from(MainActivity.this);



        // 初始化测试数据

        initData();

        final HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scroll);

        final Button button = (Button) findViewById(R.id.btn);

        final ImageView imageView = (ImageView) findViewById(R.id.image);

        android.support.v4.view.ViewPager pager = (ViewPager) findViewById(R.id.viewpager);

        // 设置监听

        pager.setOnPageChangeListener(new OnPageChangeListener() {



            @Override

            public void onPageSelected(int position) {

                int w = imageView.getWidth();

                // 动画类

                TranslateAnimation animation = new TranslateAnimation(

                        state * w, position * w, 0, 0);

                // 设置动画时间

                animation.setDuration(100);

                // 设置动画后保持位置

                animation.setFillAfter(true);

                imageView.setAnimation(animation);

                state = position;

                // 使HorizontalScrollView可以自动滑动(可以不要)

                if (scrollView.getScrollX() == 0 && position == 3) {

                    scrollView.scrollBy(button.getWidth(), 0);

                }

                if (scrollView.getScrollX() != 0 && position == 2) {

                    scrollView.scrollBy(-button.getWidth(), 0);

                }

                // 添加下面代码就可以让滑块在HorizontalScrollView中滑动

                scrollView.scrollBy(1, 0);

                scrollView.scrollBy(-1, 0);

            }



            @Override

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



            }



            @Override

            public void onPageScrollStateChanged(int arg0) {



            }

        });

        pager.setAdapter(adapter);



    }



    /**

     * 初始化测试数据

     */

    private void initData() {

        for (int i = 0; i < 6; i++) {

            View view = inflater.inflate(R.layout.view, null);

            list.add(view);

        }

    }



    /**

     * viewpager的适配器

     */

    private PagerAdapter adapter = new PagerAdapter() {



        @Override

        public boolean isViewFromObject(View arg0, Object arg1) {

            return arg0 == arg1;

        }



        @Override

        public Object instantiateItem(ViewGroup container, int position) {

            container.addView(list.get(position));

            return list.get(position);

        }



        @Override

        public void destroyItem(ViewGroup container, int position, Object object) {

            container.removeView(list.get(position));



        }



        @Override

        public int getCount() {

            return list.size();

        }

    };



}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >



    <HorizontalScrollView

        android:id="@+id/scroll"

        android:layout_width="300dp"

        android:layout_height="wrap_content"

        android:scrollbars="none" >



        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="vertical" >



            <LinearLayout

                android:layout_width="wrap_content"

                android:layout_height="50dp"

                android:orientation="horizontal" >



                <Button

                    android:id="@+id/btn"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:text="1" />



                <Button

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:text="2" />



                <Button

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:text="3" />



                <Button

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:text="4" />



                <Button

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:text="5" />



               

            </LinearLayout>



            <ImageView

                android:id="@+id/image"

                android:layout_width="65dp"

                android:layout_height="2dp"

                android:background="#0000ff" />

        </LinearLayout>

    </HorizontalScrollView>



    <android.support.v4.view.ViewPager

        android:id="@+id/viewpager"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:background="#ffffff" >

    </android.support.v4.view.ViewPager>



</LinearLayout>

 

你可能感兴趣的:(viewpager)