首页 > 代码库 > ParallaxViewPager(顶部带image的viewpager)

ParallaxViewPager(顶部带image的viewpager)

MainActivity.java

package com.desmond.parallaxheaderviewpager;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.view.ViewPager;import android.widget.ImageView;import android.widget.LinearLayout;import com.desmond.parallaxheaderviewpager.slidingTab.SlidingTabLayout;import com.desmond.parallaxviewpager.ParallaxFragmentPagerAdapter;import com.desmond.parallaxviewpager.ParallaxViewPagerBaseActivity;public class MainActivity extends ParallaxViewPagerBaseActivity {    private ImageView mTopImage;    private SlidingTabLayout mNavigBar;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initValues();        mTopImage = (ImageView) findViewById(R.id.image);        mViewPager = (ViewPager) findViewById(R.id.view_pager);        mNavigBar = (SlidingTabLayout) findViewById(R.id.navig_tab);        mHeader = findViewById(R.id.header);        if (savedInstanceState != null) {            mTopImage.setTranslationY(savedInstanceState.getFloat(IMAGE_TRANSLATION_Y));            mHeader.setTranslationY(savedInstanceState.getFloat(HEADER_TRANSLATION_Y));        }        setupAdapter();    }    @Override    protected void initValues() {        int tabHeight = getResources().getDimensionPixelSize(R.dimen.tab_height);        mMinHeaderHeight = getResources().getDimensionPixelSize(R.dimen.min_header_height);        mHeaderHeight = getResources().getDimensionPixelSize(R.dimen.header_height);        mMinHeaderTranslation = -mMinHeaderHeight + tabHeight;        mNumFragments = 4;    }    @Override    protected void onSaveInstanceState(Bundle outState) {        outState.putFloat(IMAGE_TRANSLATION_Y, mTopImage.getTranslationY());        outState.putFloat(HEADER_TRANSLATION_Y, mHeader.getTranslationY());        super.onSaveInstanceState(outState);    }    @Override    protected void setupAdapter() {        if (mAdapter == null) {            mAdapter = new ViewPagerAdapter(getSupportFragmentManager(), mNumFragments);        }        mViewPager.setAdapter(mAdapter);        mViewPager.setOffscreenPageLimit(mNumFragments);        mNavigBar.setOnPageChangeListener(getViewPagerChangeListener());        mNavigBar.setViewPager(mViewPager);    }    @Override    protected void scrollHeader(int scrollY) {        float translationY = Math.max(-scrollY, mMinHeaderTranslation);        mHeader.setTranslationY(translationY);        mTopImage.setTranslationY(-translationY / 3);    }//    private int getActionBarHeight() {//        if (mActionBarHeight != 0) {//            return mActionBarHeight;//        }////        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB){//            getTheme().resolveAttribute(android.R.attr.actionBarSize, mTypedValue, true);//        } else {//            getTheme().resolveAttribute(R.attr.actionBarSize, mTypedValue, true);//        }////        mActionBarHeight = TypedValue.complexToDimensionPixelSize(//                mTypedValue.data, getResources().getDisplayMetrics());////        return mActionBarHeight;//    }    private static class ViewPagerAdapter extends ParallaxFragmentPagerAdapter {        public ViewPagerAdapter(FragmentManager fm, int numFragments) {            super(fm, numFragments);        }        @Override        public Fragment getItem(int position) {            Fragment fragment;            switch (position) {                case 0:                    fragment = FirstScrollViewFragment.newInstance(0);                    break;                case 1:                    fragment = SecondScrollViewFragment.newInstance(1);                    break;                case 2:                    fragment = DemoListViewFragment.newInstance(2);                    break;                case 3:                    fragment = DemoRecyclerViewFragment.newInstance(3);                    break;                default:                    throw new IllegalArgumentException("Wrong page given " + position);            }            return fragment;        }        @Override        public CharSequence getPageTitle(int position) {            switch (position) {                case 0:                    return "ScrollView";                case 1:                    return "ScrollView";                case 2:                    return "ListView";                case 3:                    return "RecyclerView";                default:                    throw new IllegalArgumentException("wrong position for the fragment in vehicle page");            }        }    }}

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/parent_view"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <android.support.v4.view.ViewPager        android:id="@+id/view_pager"        android:layout_width="match_parent"        android:layout_height="match_parent"/>    <LinearLayout        android:id="@+id/header"        android:layout_width="match_parent"        android:layout_height="@dimen/header_height"        android:orientation="vertical">        <ImageView            android:id="@+id/image"            android:layout_width="match_parent"            android:layout_height="@dimen/imageview_height"            android:src="@drawable/scenery"            android:scaleType="centerCrop"/>        <com.desmond.parallaxviewpager.slidingTab.SlidingTabLayout            android:id="@+id/navig_tab"            android:layout_width="match_parent"            android:layout_height="@dimen/tab_height"            android:background="@android:color/white"            app:shouldExpand="true"            app:indicatorColor="@color/theme_color"            app:tabBackground="@layout/sliding_tab_view"            app:tabBackgroundTextViewId="@+id/tab_textview"            />    </LinearLayout></RelativeLayout>

sliding_tab_view.xml

<?xml version="1.0" encoding="utf-8"?><TextView    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/tab_textview"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="5sp"    android:gravity="center"    tools:text="Inbox"    android:textSize="13sp"    android:maxLines="1"    android:ellipsize="end"    android:textColor="@android:color/black"></TextView>

导入parallaxviewpager.jar

ParallaxViewPager(顶部带image的viewpager)