1.new project -> Scrolling Activity 2.Layout xml code activity_scrolling.xml content_scrolling.xml 3.Activity code ...
1.new project -> Scrolling Activity
2.Layout xml code
activity_scrolling.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:fitsSystemWindows="true" 8 tools:context="com.eve.coordinatorlayoutsample.ScrollingActivity"> 9 10 <android.support.design.widget.AppBarLayout 11 android:id="@+id/app_bar" 12 android:layout_width="match_parent" 13 android:layout_height="@dimen/app_bar_height" 14 android:fitsSystemWindows="true" 15 android:theme="@style/AppTheme.AppBarOverlay"> 16 17 <android.support.design.widget.CollapsingToolbarLayout 18 android:id="@+id/toolbar_layout" 19 android:layout_width="match_parent" 20 android:layout_height="match_parent" 21 android:fitsSystemWindows="true" 22 app:contentScrim="?attr/colorPrimary" 23 app:layout_scrollFlags="scroll|exitUntilCollapsed"> 24 25 <android.support.v7.widget.Toolbar 26 android:id="@+id/toolbar" 27 android:layout_width="match_parent" 28 android:layout_height="?attr/actionBarSize" 29 app:layout_collapseMode="pin" 30 app:popupTheme="@style/AppTheme.PopupOverlay" /> 31 32 <ImageView 33 android:src="@mipmap/bird" 34 app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" 35 android:layout_width="match_parent" 36 android:layout_height="match_parent" 37 android:scaleType="centerCrop" 38 app:layout_collapseMode="parallax" 39 android:minHeight="?attr/actionBarSize"/> 40 41 </android.support.design.widget.CollapsingToolbarLayout> 42 </android.support.design.widget.AppBarLayout> 43 44 <include layout="@layout/content_scrolling" /> 45 46 <android.support.design.widget.FloatingActionButton 47 android:id="@+id/fab" 48 android:layout_width="wrap_content" 49 android:layout_height="wrap_content" 50 android:layout_margin="@dimen/fab_margin" 51 app:layout_anchor="@id/app_bar" 52 app:layout_anchorGravity="bottom|end" 53 app:srcCompat="@android:drawable/ic_dialog_email" /> 54 55 </android.support.design.widget.CoordinatorLayout>
content_scrolling.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 app:layout_behavior="@string/appbar_scrolling_view_behavior" 8 tools:context="com.eve.coordinatorlayoutsample.ScrollingActivity" 9 tools:showIn="@layout/activity_scrolling"> 10 11 <TextView 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:layout_margin="@dimen/text_margin" 15 android:text="@string/large_text" /> 16 17 </android.support.v4.widget.NestedScrollView>
3.Activity code
1 public class ScrollingActivity extends AppCompatActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_scrolling); 7 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 8 setSupportActionBar(toolbar); 9 10 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 11 fab.setOnClickListener(new View.OnClickListener() { 12 @Override 13 public void onClick(View view) { 14 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 15 .setAction("Action", null).show(); 16 } 17 }); 18 } 19 }