Android开发

Android Support Library 标题栏(Toolbar)滚动效果实现方法

本文主要是介绍Android Support Library 标题栏(Toolbar)滚动效果实现方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

首先来个效果图  

 布局文件代码

在布局文件中,CoordinatorLayout作为布局文件根节点,AppBarLayout包含在CoordinatorLayout中,toolbar在AppBarLayout节点下include进来。

<?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"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="cn.manchester.androidsupportlibrary.CoordinatorLayoutActivity">

  <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">
    <include layout="@layout/toolbar" />
  </android.support.design.widget.AppBarLayout>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:scrollbars="none"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
      android:id="@+id/recyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

  </LinearLayout>


</android.support.design.widget.CoordinatorLayout>

toolbar.xml

toolbar是需要响应view的滚动事件的,所以需要为toolbar指定layout_scrollFlags属性,scroll表示toobar会随着view的向下滚动而隐藏,enterAlways表示toolbar会随着view的向上滚动而恢复到原来的位置。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#304696"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
  app:layout_scrollFlags="scroll|enterAlways"
  app:theme="@style/ThemeOverlay.AppCompat.ActionBar">

</android.support.v7.widget.Toolbar>

Java代码

public class CoordinatorLayoutActivity extends AppCompatActivity {
  Toolbar mToolbar;

  RecyclerView mRecyclerView;
  String[] mArrays = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"};
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_coordinator_layout);
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setAdapter(new MyAdapter());
  }

  class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      MyViewHolder holder = new MyViewHolder(LayoutInflater.from(CoordinatorLayoutActivity.this)
          .inflate(android.R.layout.simple_list_item_1, parent, false));
      return holder;
    }

    @Override
    public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
      holder.tv.setText(mArrays[position]);
    }

    @Override
    public int getItemCount() {
      return mArrays.length;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

      TextView tv;

      public MyViewHolder(View itemView) {
        super(itemView);
        tv = (TextView) itemView.findViewById(android.R.id.text1);
      }

    }
  }
}

以上这篇Android Support Library 标题栏(Toolbar)滚动效果实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。

这篇关于Android Support Library 标题栏(Toolbar)滚动效果实现方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!