CoordinatorLayout+AppBarLayout实现抽屉布局

CoordinatorLayout+AppBarLayout实现抽屉布局

最近因为一些事情耽误了很久没有持续更新博客了,真的是非常抱歉,很感谢这段时间以来大家的支持,我会持续努力保持更新的,好了废话不多说先上效果图

效果
这里使用的是app:layout_scrollFlags="scroll|enterAlways"后面会说到
下面是我的布局xml文件代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- app:layout_scrollFlags="scroll|exitUntilCollapsed"
                     设置当前view随滚动伸缩 -->
        <LinearLayout
            android:id="@+id/ll_header_layout"
            android:orientation="vertical"
            app:layout_scrollFlags="scroll|enterAlways"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF">
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recy_head"
                android:overScrollMode="never"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"/>
        </LinearLayout>

    </com.google.android.material.appbar.AppBarLayout>
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recy_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

对于这个布局的代码有几个注意的地方
第一,请务必使用CoordinatorLayout约束布局
第二,需要设置抽屉的布局请放在一个LinearLayout里面因为AppBar只允许有一个子view
第三,给你的LinearLayout设置app:layout_scrollFlags=“scroll|exitUntilCollapsed"属性,因为还有其他很多属性,只需要修改它就可以达到你想要的其他效果了,可能你需要的效果和我的这个不一样,但是你只需要修改这个就可以了,这里还没有详细的列出属性,下面会简单介绍一下
第四,在抽屉下方的布局最好设置在一个布局里该布局要想在appbar下方的话需设置app:layout_behavior=”@string/appbar_scrolling_view_behavior"
想要使用这些控件你还需要在build.gradle里面导入

    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
Activity里面我就贴一下,就是一个随便弄个adapter填充一下RecyclerView

```java
package com.android.demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    RecyclerView mHeadRecy;
    RecyclerView mContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHeadRecy = findViewById(R.id.recy_head);
        mContent = findViewById(R.id.recy_content);
        mHeadRecy.setLayoutManager(new LinearLayoutManager(this));
        mContent.setLayoutManager(new LinearLayoutManager(this));
        mHeadRecy.setAdapter(new ContentAdapter(5));
        mContent.setAdapter(new ContentAdapter(30));
    }

    public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder>{

        private int itemNum = 0;
        public ContentAdapter(){

        }
        public ContentAdapter(int itemNum){
            this.itemNum = itemNum;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.rv_item_content,parent,false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return itemNum;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
    }

}

如果还有遗漏的欢迎下方评论,好了这里是布局下面说一下
app:layout_scrollFlags=""属性
scroll
子View伴随着scrollingView的滚动事件而滚出或滚进屏幕
scroll|enterAlways
使用要两个一块使用;
enterAlways当向下滚动时Scrolling View和子View之间的滚动优先级。
scroll|exitUntilCollapsed
这里涉及到最小高度。当发生向上滚动时,
子View向上滚动退出直至最小高度,
然后Scrolling View开始滚动。子View不会完全退出屏幕。
scroll|snap
snap就是子View滚动带一个吸附效果。
也就是说,子View不会存在局部显示,之滚动子View的部分的情况;
当我们松开手指时,子View要么向上全部滚出屏幕,要么向下全部滚进屏幕
还有其他属性这里就不详解啦,喜欢的小伙伴欢迎在下方评论或者私信。感谢你们的支持!!!^ _ ^

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值