亲宝软件园·资讯

展开

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

人气:0
想了解Android Support Library 标题栏(Toolbar)滚动效果实现方法的相关内容吗,在本文为您仔细讲解android support library的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,support,library,下面大家一起来学习吧。

首先来个效果图  

 布局文件代码

在布局文件中,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)滚动效果实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论