activity_main.xml:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" > <android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <!-- app:layout_scrollFlags="scroll|enterAlways" -->
<!-- 这句话的作用:向上滑动的时候Toolbar消失,向下滑的时候ToolBar立即出现 -->
<!-- 如果没有scroll的话向上滑动Toolbar不会消失 -->
<!-- 如果没有enterAlways的话向下滑动Toolbar不会立即出现,会有短暂的延迟 --> <!-- android:minHeight="?attr/actionBarSize"(默认为此) 设置Toolbar的NavigationIcon位置 --> <android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
android:background="#9C27B0"
android:minHeight="?attr/actionBarSize" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可以添加多个view,会挤压标题栏"
android:textColor="#E91E63" />
</android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e0e0e0"
app:tabIndicatorColor="#ef5350"
app:tabSelectedTextColor="#1976d2"
app:tabTextColor="#90caf9" />
</android.support.design.widget.AppBarLayout> <!-- app:layout_behavior="@string/appbar_scrolling_view_behavior"的作用是使上方两个数据不被AppBarLayout遮盖 --> <android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_marginBottom="10dip"
android:layout_marginRight="10dip"
android:src="@drawable/ic_launcher"
app:backgroundTint="#e57373"
app:borderWidth="10dp"
app:elevation="10dip"
app:fabSize="normal"
app:pressedTranslationZ="10dp"
app:rippleColor="#c62828" /> </android.support.design.widget.CoordinatorLayout>
MainActivity.java:
package com.example.testappbarlayout_zzw; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Toolbar mToolbar=(Toolbar) findViewById(R.id.toolBar);
mToolbar.setLogo(R.drawable.ic_launcher);
mToolbar.setNavigationIcon(R.drawable.ic_launcher);
mToolbar.setTitle("大家好");
mToolbar.setSubtitle("我是xxx"); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
for (int i = 0; i < 10; i++)
tabLayout.addTab(tabLayout.newTab().setText("选项" + i));
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); RecyclerView mRecyclerView=(RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager mLayoutManager=new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayout.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
MyRecyclerViewAdapter adapter=new MyRecyclerViewAdapter(this);
mRecyclerView.setAdapter(adapter); } private class MyViewHolder extends ViewHolder{ public TextView text; public MyViewHolder(View itemView) {
super(itemView);
text=(TextView) itemView.findViewById(android.R.id.text1);
}
} private class MyRecyclerViewAdapter extends Adapter<MyViewHolder>{ private LayoutInflater inflater; public MyRecyclerViewAdapter(Context context) {
inflater=LayoutInflater.from(context);
} @Override
public int getItemCount() { return 100;
} @Override
public void onBindViewHolder(MyViewHolder viewHolder, int position) { viewHolder.text.setText("测试数据:"+position);
} @Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) { View v=inflater.inflate(android.R.layout.simple_list_item_1,null);
MyViewHolder holder=new MyViewHolder(v); return holder;
} } }
可能出现的相关问题解决:
使用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