Based on CoordinatorLayout, roll up the navigation bar ToolBar, roll down the navigation bar, and roll out the coordinatorlayout
Activity_main.xml:
1 <android. support. design. widget. coordinatorLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 xmlns: app = "http://schemas.android.com/apk/res-auto" 4 android: id = "@ + id/main_content" 5 android: layout_width = "match_parent" 6 android: layout_height = "match_parent" 7 android: fitsSystemWindows = "true"> 8 9 <android. support. design. widget. appBar Layout10 android: id = "@ + id/appbar" 11 android: layout_width = "match_parent" 12 android: layout_height = "wrap_content"> 13 14 <! -- App: layout_scrollFlags = "scroll | enterAlways" --> 15 <! -- Role of this sentence: the Toolbar disappears when sliding up, And the ToolBar appears immediately when sliding down --> 16 <! -- If there is no scroll, sliding up the Toolbar will not disappear --> 17 <! -- If there is no enterAlways, sliding down the Toolbar will not appear immediately and there will be a short delay --> 18 19 20 <! -- Android: minHeight = "? Attr/actionBarSize "(for this purpose by default) sets the NavigationIcon location of the Toolbar --> 21 22 <android. support. v7.widget. toolbar23 android: id = "@ + id/toolBar" 24 android: layout_width = "match_parent" 25 android: layout_height = "wrap_content" 26 app: layout_scrollFlags = "scroll | enterAlways" 27 android: background = "#9C27B0" 28 android: minHeight = "? Attr/actionBarSize "> 29 30 <TextView31 android: layout_width =" wrap_content "32 android: layout_height =" wrap_content "33 android: text =" you can add multiple views, the title bar "34 android: textColor =" # E91E63 "/> 35 </android. support. v7.widget. toolbar> 36 37 <android. support. design. widget. tabLayout38 android: id = "@ + id/tabLayout" 39 android: layout_width = "match_parent" 40 android: layout_height = "wrap_content" 41 android: background = "# e0e0 E0 "42 app: tabIndicatorColor =" # ef5350 "43 app: tabSelectedTextColor =" #1976d2 "44 app: tabTextColor =" #90caf9 "/> 45 </android. support. design. widget. appBarLayout> 46 47 <! -- App: layout_behavior = "@ string/appbar_scrolling_view_behavior" is used to prevent the above two data from being overwritten by AppBarLayout --> 48 49 <android. support. v7.widget. recyclerView50 android: id = "@ + id/recyclerView" 51 android: layout_width = "match_parent" 52 android: layout_height = "match_parent" 53 app: layout_behavior = "@ string/appbar_scrolling_view_behavior"/> 54 <android. support. design. widget. floatingActionButton56 android: layout_width = "wrap_content" 57 android: layout_height = "wrap_content" 58 android: layout_gravity = "right | bottom" 59 android: layout_marginBottom = "10dip" 60 android: layout_marginRight = "10dip" 61 android: src = "@ drawable/ic_launcher" 62 app: backgroundTint = "# e57373" 63 app: borderWidth = "10dp" 64 app: elevation = "10dip" 65 app: fabSize = "normal" 66 app: pressedTranslationZ = "10dp" 67 app: rippleColor = "# c62828"/> 68 69 </android. support. design. widget. coordinatorLayout>
MainActivity. java:
1 package com. example. testappbarlayout_zzw; 2 3 4 import android. app. activity; 5 import android. content. context; 6 import android. OS. bundle; 7 import android. support. design. widget. tabLayout; 8 import android. support. v7.widget. linearLayoutManager; 9 import android. support. v7.widget. recyclerView; 10 import android. support. v7.widget. recyclerView. adapter; 11 import android. support. v7.widget. toolbar; 12 import android. support. v7.widget. recyclerView. viewHolder; 13 import android. view. layoutInflater; 14 import android. view. view; 15 import android. view. viewGroup; 16 import android. widget. linearLayout; 17 import android. widget. textView; 18 19 20 public class MainActivity extends Activity {21 22 @ Override23 protected void onCreate (Bundle savedInstanceState) {24 super. onCreate (savedInstanceState); 25 setContentView (R. layout. activity_main); 26 27 Toolbar mToolbar = (Toolbar) findViewById (R. id. toolBar); 28 mToolbar. setLogo (R. drawable. ic_launcher); 29 mToolbar. setNavigationIcon (R. drawable. ic_launcher); 30 mToolbar. setTitle ("Hello everyone"); 31 mToolbar. setSubtitle ("My name is xxx"); 32 33 TabLayout tabLayout = (TabLayout) findViewById (R. id. tabLayout); 34 for (int I = 0; I <10; I ++) 35 tabLayout. addTab (tabLayout. newTab (). setText ("option" + I); 36 tabLayout. setTabMode (TabLayout. MODE_SCROLLABLE); 37 38 RecyclerView mRecyclerView = (RecyclerView) findViewById (R. id. recyclerView); 39 LinearLayoutManager mLayoutManager = new LinearLayoutManager (this); 40 mLayoutManager. setOrientation (LinearLayout. VERTICAL); 41 mRecyclerView. setLayoutManager (mLayoutManager); 42 MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter (this); 43 mRecyclerView. setAdapter (adapter); 44 45} 46 47 private class MyViewHolder extends ViewHolder {48 49 public TextView text; 50 51 public MyViewHolder (View itemView) {52 super (itemView ); 53 text = (TextView) itemView. findViewById (android. r. id. text1); 54} 55} 56 57 private class MyRecyclerViewAdapter extends Adapter <MyViewHolder> {58 59 private LayoutInflater inflater; 60 61 public MyRecyclerViewAdapter (Context context) {62 inflater = LayoutInflater. from (context); 63} 64 65 @ Override66 public int getItemCount () {67 68 return 100; 69} 70 71 @ Override72 public void onBindViewHolder (MyViewHolder viewHolder, int position) {73 74 viewHolder. text. setText ("test data:" + position); 75} 76 77 @ Override78 public MyViewHolder onCreateViewHolder (ViewGroup viewGroup, int arg1) {79 80 View v = inflater. inflate (android. r. layout. simple_list_item_1, null); 81 MyViewHolder holder = new MyViewHolder (v); 82 83 return holder; 84} 85 86} 87 88}
Possible problems:
Java. lang. reflect. InvocationTargetException: http://www.cnblogs.com/zzw1994/p/5004564.html appears with android. support. v7.widget. RecyclerView
Java. lang. reflect. InvocationTargetException: http://www.cnblogs.com/zzw1994/p/5012467.html appears with android. support. design