Behavior的简单使用

实现效果图

20181211_160133.gif

实现步骤

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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">

    <com.google.android.material.appbar.AppBarLayout
            android:background="@color/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:layout_scrollFlags="scroll"
                android:layout_width="match_parent">

            <EditText
                    android:textSize="16sp"
                    android:hint="搜索您感兴趣的内容"
                    android:background="#fff"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginLeft="20dp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
        </androidx.appcompat.widget.Toolbar>

        <com.google.android.material.tabs.TabLayout
                android:id="@+id/tab_layout_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="scrollable"
                app:tabGravity="fill"/>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/list"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

2.填充数据

class JDScrolActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_jd_scrol)

        //设置tab栏
        tab_layout_title.addTab(tab_layout_title.newTab().setText("财经"))
        tab_layout_title.addTab(tab_layout_title.newTab().setText("财经"))
        tab_layout_title.addTab(tab_layout_title.newTab().setText("财经"))
        tab_layout_title.addTab(tab_layout_title.newTab().setText("财经"))
        tab_layout_title.addTab(tab_layout_title.newTab().setText("财经"))

        list.layoutManager = LinearLayoutManager(this)
        list.adapter = RecycleViewAdapter()
    }

    class RecycleViewAdapter : RecyclerView.Adapter<RecycleViewAdapter.RecycleViewViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecycleViewViewHolder {

            val view = LayoutInflater.from(parent.context).inflate(R.layout.simple_item, parent, false)
            return RecycleViewViewHolder(view)
        }

        override fun getItemCount(): Int {
            return 10
        }

        override fun onBindViewHolder(holder: RecycleViewViewHolder, position: Int) {
        }


        class RecycleViewViewHolder(view: View) :RecyclerView.ViewHolder(view){

        }

    }
}

关键点

使用了系统自带的Behavior


image.png
QQ交流群
微信公众号

参考文章

Android Design Support Library 控件的使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,040评论 3 119
  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,125评论 2 89
  • 昨天大半夜,一个关系一直不错的姑娘忽然给我发语音聊天。 已经好久不联系了, 因为这一段时间内她的朋友圈开始秀起了恩...
    小eeeee阅读 5,166评论 2 0
  • 一段路,走了很久,依然看不到希望,那就改变方向; 一件事,想了很久,依然纠结于心,那就选择放下;一些人,交了很久,...
    吉祥yt阅读 1,209评论 0 0
  • 晴星期二 虽然昨天下雨了,但今天还是挺暖和的。今天中午我接上二宝回到家门口,老远看到站在楼宇门那有个人,来到跟前一...
    倾月萱萱_0bd6阅读 1,482评论 0 1