TabLayout+ViewPager+AppBarLayout 实现滑动时Toolbar自动隐藏,tab固定在顶端
效果
正常显示Toolbar和Tab
向下滑动的适合,Toolbar自动滑出,Tab固定在顶端
关于AppbarLayout
AppBarLayout继承自LinearLayout,垂直方向。当定层的CoordinatorLayout中有滑动手势的适合,AppBarLayout中的子View可以滑出(或者重新显示在)UI,
只需要在子AppBarLayout的子View中设置app:layout_scrollFlags属性就好了
scroll:
设置了sroll才能滚出屏幕,否则一直固定在顶端
enterAlways:
设置了这个属性的子view,每次向下滚动,子view都会显示
enterAlwaysCollapsed:
当你的视图已经设置minHeight属性又使用此标志时,你的视图只能已最小高度进入,只有当滚动视图到达顶部时才扩大到完整高度
exitUntilCollapsed: 滚动退出屏幕,最后折叠在顶端
主布局xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
主要Java代码
public class CoordinatorLayoutActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private FragmentAdapter fmAapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coordinator_layout);
toolbar= (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Title");
toolbar.setSubtitle("SubTitle");
setSupportActionBar(toolbar);
tabLayout= (TabLayout) findViewById(R.id.tab_layout);
List<Fragment> fmList = new ArrayList<Fragment>();
if(fmList!=null){
ArrayList<String> nameList=new ArrayList<>();
for(int i=0;i<30;i++){
nameList.add("name "+i);
}
FrientFragment ff=FrientFragment.newInstance(nameList);
fmList.add(ff);
TabFragment tf1=TabFragment.newInstance("this is tabfragment 1");
TabFragment tf2=TabFragment.newInstance("this is tabfragment 2");
fmList.add(tf1);
fmList.add(tf2);
}
fmAapter=new FragmentAdapter(getSupportFragmentManager(),fmList);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(fmAapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setText("tab0");
tabLayout.getTabAt(1).setText("tab1");
tabLayout.getTabAt(2).setText("tab2");
}
//适配器
class FragmentAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fmList=new ArrayList<Fragment>();
public FragmentAdapter(FragmentManager fm,List<Fragment>fmList) {
super(fm);
this.fmList=fmList;
}
@Override
public Fragment getItem(int position) {
return fmList.get(position);
}
@Override
public int getCount() {
if(fmList==null){
return 0;
}else{
return fmList.size();
}
}
}
}
带滑动控件的Fragment
public class FrientFragment extends Fragment {
private static final String ARG_NAME = "namelist";
private List<String> nameList;
private RecyclerView recyclerView;
private LayoutInflater inflater;
private MyAdapter adapter;
public FrientFragment() {
// Required empty public constructor
}
public static FrientFragment newInstance(ArrayList<String> nameList) {
FrientFragment fragment = new FrientFragment();
Bundle args = new Bundle();
args.putStringArrayList(ARG_NAME,nameList);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
this.nameList = getArguments().getStringArrayList(ARG_NAME);
}
inflater=LayoutInflater.from(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_frient, container, false);
recyclerView= (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//adapter
adapter=new MyAdapter();
recyclerView.setAdapter(adapter);
return view;
}
class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.list_item,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//set item's value
holder.nameText.setText(nameList.get(position));
}
@Override
public int getItemCount() {
return nameList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
public TextView nameText;
public MyViewHolder(View itemView) {
super(itemView);
//get到相关控件的引用
nameText= (TextView) itemView.findViewById(R.id.name_text);
}
}
}
}
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/head_img"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/name_text"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="40dp"/>
</LinearLayout>