Skip to content

Instantly share code, notes, and snippets.

@mzgreen
mzgreen / PartThreeActivity.java
Created June 23, 2015 11:15
HideOnScrollExample - PartThreeActivity pager and tabs initialization
private void initViewPagerAndTabs() {
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(PartThreeFragment.createInstance(20), getString(R.string.tab_1));
pagerAdapter.addFragment(PartThreeFragment.createInstance(4), getString(R.string.tab_2));
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
@mzgreen
mzgreen / activity_part_three.xml
Created June 23, 2015 11:22
HideOnScrollExample - behavior added to a ViewPager
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
@mzgreen
mzgreen / activity_part_three.xml
Created June 23, 2015 11:01
HideOnScrollExample - activity_part_three layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
@mzgreen
mzgreen / fragment_part_three.xml
Created June 23, 2015 10:48
HideOnScrollExample - fragment_part_three layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
@mzgreen
mzgreen / build.gradle
Created June 23, 2015 10:38
HideOnScrollExample - build file
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 14
targetSdkVersion 22
}
}
@mzgreen
mzgreen / HidingScrollListener.java
Created February 28, 2015 08:36
HidingScrollListener class with fixed top position hiding
public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {
private static final float HIDE_THRESHOLD = 10;
private static final float SHOW_THRESHOLD = 70;
private int mToolbarOffset = 0;
private boolean mControlsVisible = true;
private int mToolbarHeight;
private int mTotalScrolledDistance;
@mzgreen
mzgreen / PartTwoActivity.java
Created February 28, 2015 08:31
PartTwoActivity setting padding on RecyclerView
private void initRecyclerView() {
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
int paddingTop = Utils.getToolbarHeight(PartTwoActivity.this) + Utils.getTabsHeight(PartTwoActivity.this);
recyclerView.setPadding(recyclerView.getPaddingLeft(), paddingTop, recyclerView.getPaddingRight(), recyclerView.getPaddingBottom());
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// ...
}
@mzgreen
mzgreen / tabs.xml
Created February 28, 2015 08:28
Tabs layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/tabsHeight"
android:background="?attr/colorPrimary">
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:layout_width="match_parent"
@mzgreen
mzgreen / part_two_activity.xml
Last active August 29, 2015 14:16
PartTwoActivity layout with tabs
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"/>
@mzgreen
mzgreen / PartTwoActivity.java
Created February 28, 2015 08:26
PartTwoActivity class onShow and onHide implementation
private void initRecyclerView() {
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
RecyclerAdapter recyclerAdapter = new RecyclerAdapter(createItemList());
recyclerView.setAdapter(recyclerAdapter);
recyclerView.setOnScrollListener(new HidingScrollListener(this) {
@Override
public void onMoved(int distance) {