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

时间: 2024-10-12 21:16:05

ParallaxViewPager(顶部带image的viewpager)的相关文章

仿网易客户端实现顶部导航效果(ViewPager+Fragment)

为了实现这个效果我竟然用了两天的时间,我真是呵呵了,废话不多说直接开始整理. 这里的Indicator是重写的LinearLayout用来弄导航栏那个蓝条的样式以及位置的(这部分代码是在别人那考过来的),代码如下: package vikicc.titletab; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android

回到顶部带动画

// 获取元素var bodyTop = document.getElementById("top");// 回到顶部的按钮var totop = document.getElementById("totop");// top 是window自带的一个属性,此属性是只读的.此属性默认是window对象// 当拖动滚动条的时候执行window.onscroll = function () { var scrollTop = document.body.scrollTo

jquery 如何实现回顶部 带滑动效果

$("#returnTop").click(function () { var speed=200;//滑动的速度 $('body,html').animate({ scrollTop: 0 }, speed); return false; }); 返回顶部:设置为body的scrollTop为0 滑动效果:animate(Jquery的自定义动画) 备注:returnTop为触发返回顶部的元素id

android学习ViewPager的简单使用

使用ViewPager需要引入android.support.v4.View.ViewPager这样的jar包,谷歌公司为解决当前版本碎片化的问题,提供的兼容的包.主要目的就是解决向下兼容问题. 1,加载显示的页卡 将layout布局转换成view对象 (1)LayoutInflater if= getLayoutInflater().from(this); if.inflater(resource,root); (2) View.inflate(context,resource,root);

ViewPager不为人知的秘密

ViewPager不为人知的秘密 ViewPager翻页控制 关于控制ViewPager的翻页,在网上已经有很多解决方法了,我们一个个来看看. setScanScroll() 我们先来看一下具体实现: public class CustomViewPager extends ViewPager { private boolean isCanScroll = true; public CustomViewPager(Context context) { super(context); } publ

wesome-android

awesome-android Introduction android libs from github System requirements Android Notice If the lib is no longer being maintained,please do not add it here. How To Contribute Step 1. Add a Item as follows: **Library Name**[one space]Short Description

Android Tips – 填坑手册

出于: androidChina   http://www.androidchina.net/3595.html 学习 Android 至今,大大小小的坑没少踩,庆幸的是,在强大的搜索引擎与无私奉献的人们的帮助下,我遇到的坑都顺利地被填平了. 为了便于日后遇到同样的问题时,能免于再次搜索带来的麻烦,我养成了收藏书签的习惯,随着书签(Tips)的日积月累,我想,是时候该有这个项目了. 如果你是个 Android 新人,那么我希望这份列表,可以成为你踩到坑时的不完全手册. 当然,这份列表一定会有遗漏

GitHub中常用开源库

awesome-android Introduction android libs from github System requirements Android Notice If the lib is no longer being maintained,please do not add it here. Libs Table of contents Framework EventBus Orm Image Loading Animations Network Widget Materia

github中的常用库

awesome-android android libs from github Download ZIP Download TAR View On GitHub This project is maintained bysnowdream awesome-android Introduction android libs from github System requirements Android Notice If the lib is no longer being maintained