diff --git a/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java b/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java index e873b1e..b779fc0 100644 --- a/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java +++ b/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java @@ -15,12 +15,14 @@ */ package com.example.android.sunshine.app; +import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.TypedArray; import android.database.Cursor; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; @@ -195,6 +197,25 @@ public class ForecastFragment extends Fragment implements LoaderManager.LoaderCa // specify an adapter (see also next example) mRecyclerView.setAdapter(mForecastAdapter); + final View parallaxView = rootView.findViewById(R.id.parallax_bar); + if (null != parallaxView) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + int max = parallaxView.getHeight(); + if (dy > 0) { + parallaxView.setTranslationY(Math.max(-max, parallaxView.getTranslationY() - dy / 2)); + } else { + parallaxView.setTranslationY(Math.min(0, parallaxView.getTranslationY() - dy / 2)); + } + } + }); + } + } + // If there's instance state, mine it for useful information. // The end-goal here is that the user never knows that turning their device sideways // does crazy lifecycle related things. It should feel like some stuff stretched out, @@ -318,6 +339,14 @@ public class ForecastFragment extends Fragment implements LoaderManager.LoaderCa } + @Override + public void onDestroy() { + super.onDestroy(); + if (null != mRecyclerView) { + mRecyclerView.clearOnScrollListeners(); + } + } + @Override public void onLoaderReset(Loader loader) { mForecastAdapter.swapCursor(null); diff --git a/app/src/main/res/layout-land/fragment_main.xml b/app/src/main/res/layout-land/fragment_main.xml index 3ebff0e..d4be168 100644 --- a/app/src/main/res/layout-land/fragment_main.xml +++ b/app/src/main/res/layout-land/fragment_main.xml @@ -19,7 +19,7 @@ android:layout_width="match_parent" android:layout_height="match_parent"> + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/fragment_forecast" + android:name="com.example.android.sunshine.app.ForecastFragment" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:layout="@android:layout/list_content" + />