android behavior教程,Behavior的简单使用

实现效果图

AAffA0nNPuCLAAAAAElFTkSuQmCC

20181211_160133.gif

实现步骤

1.布局文件<?xml  version="1.0" encoding="utf-8"?>

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:background="@color/colorPrimary"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_height="?attr/actionBarSize"

android:background="@color/colorPrimary"

app:layout_scrollFlags="scroll"

android:layout_width="match_parent">

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"/>

android:id="@+id/tab_layout_title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:tabMode="scrollable"

app:tabGravity="fill"/>

android:id="@+id/list"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

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() {

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

作者:莫语莫雨

链接:https://www.jianshu.com/p/bd27519fb02b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中的收缩布局可以使用CollapsingToolbarLayout来实现。CollapsingToolbarLayout是一个可以自动折叠的Toolbar布局,可以根据ScrollView或者RecyclerView的滑动来实现Toolbar的折叠和展开。以下是一个简单的示例: ``` xml <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@drawable/image" android:scaleType="centerCrop" app:layout_collapseMode="parallax" /> <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- 内容区域 --> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout> ``` 在这个示例中,CollapsingToolbarLayout包含一个ImageView和一个Toolbar,ImageView设置了layout_collapseMode为parallax,表示在Toolbar折叠时会有视差效果,而Toolbar设置了layout_collapseMode为pin,表示在Toolbar折叠时会固定在顶部。NestedScrollView设置了app:layout_behavior为appbar_scrolling_view_behavior,表示它是一个可以被AppBarLayout滚动的View。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值