Java tutorial
/* * Copyright 2014 Soichiro Kashima * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wordcamp; import android.content.res.TypedArray; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import android.widget.FrameLayout; import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks; import com.github.ksoichiro.android.observablescrollview.ScrollState; import com.github.ksoichiro.android.observablescrollview.ScrollUtils; import com.github.ksoichiro.android.observablescrollview.Scrollable; import com.github.ksoichiro.android.observablescrollview.TouchInterceptionFrameLayout; import com.nineoldandroids.animation.ValueAnimator; import com.nineoldandroids.view.ViewHelper; import org.wordcamp.adapters.CacheFragmentStatePagerAdapter; import org.wordcamp.widgets.SlidingTabLayout; /** * SlidingTabLayout and SlidingTabStrip are from google/iosched: * https://github.com/google/iosched */ public class BaseActivity extends ActionBarActivity implements ObservableScrollViewCallbacks { private View mToolbarView; private TouchInterceptionFrameLayout mInterceptionLayout; private ViewPager mPager; private WCPagerAdapter mPagerAdapter; private int mSlop; private boolean mScrolled; private ScrollState mLastScrollState; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_base); setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); ViewCompat.setElevation(findViewById(R.id.header), getResources().getDimension(R.dimen.toolbar_elevation)); mToolbarView = findViewById(R.id.toolbar); mPagerAdapter = new WCPagerAdapter(getSupportFragmentManager()); mPager = (ViewPager) findViewById(R.id.pager); mPager.setAdapter(mPagerAdapter); final int tabHeight = getResources().getDimensionPixelSize(R.dimen.tab_height); findViewById(R.id.pager_wrapper).setPadding(0, getActionBarSize() + tabHeight, 0, 0); SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs); slidingTabLayout.setCustomTabView(R.layout.tab_view, android.R.id.text1); slidingTabLayout.setSelectedIndicatorColors(Color.parseColor("#0F85D1")); slidingTabLayout.setDistributeEvenly(true); slidingTabLayout.setViewPager(mPager); ViewConfiguration vc = ViewConfiguration.get(this); mSlop = vc.getScaledTouchSlop(); mInterceptionLayout = (TouchInterceptionFrameLayout) findViewById(R.id.container); mInterceptionLayout.setScrollInterceptionListener(mInterceptionListener); } protected int getActionBarSize() { TypedValue typedValue = new TypedValue(); int[] textSizeAttr = new int[] { R.attr.actionBarSize }; int indexOfAttrTextSize = 0; TypedArray a = obtainStyledAttributes(typedValue.data, textSizeAttr); int actionBarSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1); a.recycle(); return actionBarSize; } protected int getScreenHeight() { return findViewById(android.R.id.content).getHeight(); } @Override public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) { } @Override public void onDownMotionEvent() { } @Override public void onUpOrCancelMotionEvent(ScrollState scrollState) { if (!mScrolled) { // This event can be used only when TouchInterceptionFrameLayout // doesn't handle the consecutive events. adjustToolbar(scrollState); } } private TouchInterceptionFrameLayout.TouchInterceptionListener mInterceptionListener = new TouchInterceptionFrameLayout.TouchInterceptionListener() { @Override public boolean shouldInterceptTouchEvent(MotionEvent ev, boolean moving, float diffX, float diffY) { if (!mScrolled && mSlop < Math.abs(diffX) && Math.abs(diffY) < Math.abs(diffX)) { // Horizontal scroll is maybe handled by ViewPager return false; } Scrollable scrollable = getCurrentScrollable(); if (scrollable == null) { mScrolled = false; return false; } // If interceptionLayout can move, it should intercept. // And once it begins to move, horizontal scroll shouldn't work any longer. int toolbarHeight = mToolbarView.getHeight(); int translationY = (int) ViewHelper.getTranslationY(mInterceptionLayout); boolean scrollingUp = 0 < diffY; boolean scrollingDown = diffY < 0; if (scrollingUp) { if (translationY < 0) { mScrolled = true; mLastScrollState = ScrollState.UP; return true; } } else if (scrollingDown) { if (-toolbarHeight < translationY) { mScrolled = true; mLastScrollState = ScrollState.DOWN; return true; } } mScrolled = false; return false; } @Override public void onDownMotionEvent(MotionEvent ev) { } @Override public void onMoveMotionEvent(MotionEvent ev, float diffX, float diffY) { float translationY = ScrollUtils.getFloat(ViewHelper.getTranslationY(mInterceptionLayout) + diffY, -mToolbarView.getHeight(), 0); ViewHelper.setTranslationY(mInterceptionLayout, translationY); if (translationY < 0) { FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mInterceptionLayout.getLayoutParams(); lp.height = (int) (-translationY + getScreenHeight()); mInterceptionLayout.requestLayout(); } } @Override public void onUpOrCancelMotionEvent(MotionEvent ev) { mScrolled = false; adjustToolbar(mLastScrollState); } }; private Scrollable getCurrentScrollable() { Fragment fragment = getCurrentFragment(); if (fragment == null) { return null; } View view = fragment.getView(); if (view == null) { return null; } return (Scrollable) view.findViewById(R.id.scroll); } private void adjustToolbar(ScrollState scrollState) { int toolbarHeight = mToolbarView.getHeight(); final Scrollable scrollable = getCurrentScrollable(); if (scrollable == null) { return; } int scrollY = scrollable.getCurrentScrollY(); if (scrollState == ScrollState.DOWN) { showToolbar(); } else if (scrollState == ScrollState.UP) { if (toolbarHeight <= scrollY) { hideToolbar(); } else { showToolbar(); } } else if (!toolbarIsShown() && !toolbarIsHidden()) { showToolbar(); } } private Fragment getCurrentFragment() { return mPagerAdapter.getItemAt(mPager.getCurrentItem()); } private boolean toolbarIsShown() { return ViewHelper.getTranslationY(mInterceptionLayout) == 0; } private boolean toolbarIsHidden() { return ViewHelper.getTranslationY(mInterceptionLayout) == -mToolbarView.getHeight(); } private void showToolbar() { animateToolbar(0); } private void hideToolbar() { animateToolbar(-mToolbarView.getHeight()); } private void animateToolbar(final float toY) { float layoutTranslationY = ViewHelper.getTranslationY(mInterceptionLayout); if (layoutTranslationY != toY) { ValueAnimator animator = ValueAnimator.ofFloat(ViewHelper.getTranslationY(mInterceptionLayout), toY) .setDuration(200); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float translationY = (float) animation.getAnimatedValue(); ViewHelper.setTranslationY(mInterceptionLayout, translationY); if (translationY < 0) { FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mInterceptionLayout .getLayoutParams(); lp.height = (int) (-translationY + getScreenHeight()); mInterceptionLayout.requestLayout(); } } }); animator.start(); } } private static class WCPagerAdapter extends CacheFragmentStatePagerAdapter { public WCPagerAdapter(FragmentManager fm) { super(fm); } @Override protected Fragment createItem(int position) { switch (position) { case 0: return UpcomingWCFragment.newInstance("Upcoming", ""); case 1: return MyWCFragment.newInstance("MY", ""); case 2: return UpcomingWCFragment.newInstance("Past", ""); default: return UpcomingWCFragment.newInstance("", ""); } } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "Upcoming"; case 1: return "My WordCamp"; default: return "Past"; } } } }