android布局滑动到顶端悬浮,吸顶

ok 首先我们来看看是不是咱们想要的效果!免得浪费大家时间

效果图.gif

这里采用CoordinatorLayout+AppBarLayout配合使用

再来看一下布局结构


布局结构.jpg
现在要使用的话则Copy一下代码稍作修改即可
<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/color_two">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                boy:layout_scrollFlags="scroll|exitUntilCollapsed"
                android:orientation="vertical">


                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_200"
                    android:src="@mipmap/testimage" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_launcher" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll_topview"
                android:layout_width="match_parent"
                android:layout_height="116dp"
                android:gravity="right"
                android:background="@color/color_three"
                android:orientation="vertical">

                <View
                    android:layout_width="match_parent"
                    android:background="@color/color_two"
                    android:layout_height="66dp"/>

                <Button
                    android:id="@+id/btn_screen"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="筛选" />

            </LinearLayout>

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/reclerview"
            android:layout_width="match_parent"
            boy:layout_behavior="@string/appbar_scrolling_view_behavior"
            boy:layout_scrollFlags="scroll"
            android:layout_height="match_parent">
        </android.support.v7.widget.RecyclerView>

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

这里需要注意一点的是

1.design这个包版本需要在26以上,不然有点卡顿,加速度有点问题

2.如果我们悬浮模块以上的这部分布局很长,超过了一屏,那么我们需要给AppBar加一个判断,如下

private int mI;

appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                //为0时表示AppBarLayout完全显示
                mI = verticalOffset;
                refreshLayout.setEnableRefresh(mI >= 0);
            }
        });
//外层我是用的一个刷新控件 SmartRefreshLayout
//https://github.com/scwang90/SmartRefreshLayout/blob/master/art/md_property.md

好了这样就大功告成了,这次是不是清楚了

回手掏 鬼刀一开看不见 走位走位 哈哈 老铁心心来一个

demo下载链接:点击这里就ok,ps里面融云的包没删不用管

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

推荐阅读更多精彩内容

  • ok 首先我们来看看是不是咱们想要的效果!免得浪费大家时间 这篇没劲,我另外一篇详细一点可以看一下 哈哈 麻烦老...
    Android丶boy阅读 6,953评论 0 33
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,776评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,491评论 2 59
  • CoordinatorLayout与滚动的处理 CoordinatorLayout实现了多种Material De...
    cxm11阅读 11,648评论 1 15
  • ng-model 指令ng-model 指令 绑定 HTML 元素 到应用程序数据。ng-model 指令也可以:...
    壬万er阅读 4,361评论 0 2