你的位置:首页 > 操作系统

[操作系统]基于CoordinatorLayout实现向上滚动导航条ToolBar滚出、向下滚动导航条滚出


 

 

activity_main.

 1 <android.support.design.widget.CoordinatorLayout ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/tools" 3   ="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.AppBarLayout10     android:id="@+id/appbar"11     android:layout_width="match_parent"12     android:layout_height="wrap_content" >13 14     <!-- app:layout_scrollFlags="scroll|enterAlways" -->15     <!-- 这句话的作用:向上滑动的时候Toolbar消失,向下滑的时候ToolBar立即出现 -->16     <!-- 如果没有scroll的话向上滑动Toolbar不会消失 -->17     <!-- 如果没有enterAlways的话向下滑动Toolbar不会立即出现,会有短暂的延迟 -->18 19 20     <!-- android:minHeight="?attr/actionBarSize"(默认为此) 设置Toolbar的NavigationIcon位置 -->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="可以添加多个view,会挤压标题栏"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="#e0e0e0"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"的作用是使上方两个数据不被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 55   <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("大家好");31     mToolbar.setSubtitle("我是xxx");32     33     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);34     for (int i = 0; i < 10; i++)35       tabLayout.addTab(tabLayout.newTab().setText("选项" + 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("测试数据:"+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 }

 

可能出现的相关问题解决:

使用android.support.v7.widget.RecyclerView出现 java.lang.reflect.InvocationTargetException:http://www.cnblogs.com/zzw1994/p/5004564.html

使用android.support.design出现java.lang.reflect.InvocationTargetException:http://www.cnblogs.com/zzw1994/p/5012467.html