Java tutorial
/* * Copyright 2015 Joao Paulo Fernandes Ventura. * * 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 com.jpventura.xyzreader.ui; import android.app.Fragment; import android.app.FragmentManager; import android.app.LoaderManager; import android.content.Loader; import android.database.Cursor; import android.graphics.drawable.ColorDrawable; import android.os.Build; import android.os.Bundle; import android.support.v13.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.util.TypedValue; import android.view.View; import android.view.ViewGroup; import android.view.WindowInsets; import com.jpventura.xyzreader.R; import com.jpventura.xyzreader.data.ArticleLoader; import com.jpventura.xyzreader.data.ItemsContract; /** * An activity representing a single Article detail screen, letting you swipe between articles. */ public class ArticleDetailActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> { private Cursor mCursor; private long mStartId; private long mSelectedItemId; private int mSelectedItemUpButtonFloor = Integer.MAX_VALUE; private int mTopInset; private ViewPager mPager; private MyPagerAdapter mPagerAdapter; private View mUpButtonContainer; private View mUpButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); } setContentView(R.layout.activity_article_detail); getLoaderManager().initLoader(0, null, this); mPagerAdapter = new MyPagerAdapter(getFragmentManager()); mPager = (ViewPager) findViewById(R.id.pager); mPager.setAdapter(mPagerAdapter); mPager.setPageMargin((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics())); mPager.setPageMarginDrawable(new ColorDrawable(0x22000000)); mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageScrollStateChanged(int state) { super.onPageScrollStateChanged(state); if (null == mUpButton) return; mUpButton.animate().alpha((state == ViewPager.SCROLL_STATE_IDLE) ? 1f : 0f).setDuration(300); } @Override public void onPageSelected(int position) { if (mCursor != null) { mCursor.moveToPosition(position); } mSelectedItemId = mCursor.getLong(ArticleLoader.Query._ID); updateUpButtonPosition(); } }); mUpButtonContainer = findViewById(R.id.up_container); mUpButton = findViewById(R.id.action_up); if (null != mUpButton) { mUpButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onSupportNavigateUp(); } }); } if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) && (null != mUpButtonContainer)) { mUpButtonContainer.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { @Override public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) { view.onApplyWindowInsets(windowInsets); mTopInset = windowInsets.getSystemWindowInsetTop(); mUpButtonContainer.setTranslationY(mTopInset); updateUpButtonPosition(); return windowInsets; } }); } if (savedInstanceState == null) { if (getIntent() != null && getIntent().getData() != null) { mStartId = ItemsContract.Items.getItemId(getIntent().getData()); mSelectedItemId = mStartId; } } } @Override public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { return ArticleLoader.newAllArticlesInstance(this); } @Override public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) { mCursor = cursor; mPagerAdapter.notifyDataSetChanged(); // Select the start ID if (mStartId > 0) { mCursor.moveToFirst(); // TODO: optimize while (!mCursor.isAfterLast()) { if (mCursor.getLong(ArticleLoader.Query._ID) == mStartId) { final int position = mCursor.getPosition(); mPager.setCurrentItem(position, false); break; } mCursor.moveToNext(); } mStartId = 0; } } @Override public void onLoaderReset(Loader<Cursor> cursorLoader) { mCursor = null; mPagerAdapter.notifyDataSetChanged(); } public void onUpButtonFloorChanged(long itemId, ArticleDetailFragment fragment) { if (itemId == mSelectedItemId) { mSelectedItemUpButtonFloor = fragment.getUpButtonFloor(); updateUpButtonPosition(); } } private void updateUpButtonPosition() { if (null == mUpButton) return; int upButtonNormalBottom = mTopInset + mUpButton.getHeight(); mUpButton.setTranslationY(Math.min(mSelectedItemUpButtonFloor - upButtonNormalBottom, 0)); } private class MyPagerAdapter extends FragmentStatePagerAdapter { public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public void setPrimaryItem(ViewGroup container, int position, Object object) { super.setPrimaryItem(container, position, object); ArticleDetailFragment fragment = (ArticleDetailFragment) object; if (fragment != null) { mSelectedItemUpButtonFloor = fragment.getUpButtonFloor(); updateUpButtonPosition(); } } @Override public Fragment getItem(int position) { mCursor.moveToPosition(position); return ArticleDetailFragment.newInstance(mCursor.getLong(ArticleLoader.Query._ID)); } @Override public int getCount() { return (mCursor != null) ? mCursor.getCount() : 0; } } }