实现滑动可固定header以及页面刷新

布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include layout="@layout/app_bar"></include>//自定义的ToolBar
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/deduction_record_swipefreshlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <android.support.design.widget.AppBarLayout
                    android:id="@+id/app_bar_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true">
                    <android.support.design.widget.CollapsingToolbarLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:fitsSystemWindows="true"
                        app:contentScrim="?attr/colorPrimary"
                        app:layout_scrollFlags="scroll">
                        <LinearLayout
                            android:id="@+id/top"
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:orientation="horizontal"
                            android:background="@color/black"
                            app:layout_collapseMode="pin"
                            app:layout_scrollFlags="scroll|enterAlways"></LinearLayout>
                    </android.support.design.widget.CollapsingToolbarLayout>

                    <!-- 要固定在toolbar上面的控件 -->
                    <LinearLayout
                        android:id="@+id/header"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:padding="10dp"
                        android:background="@color/gary_bg"
                        android:orientation="horizontal">
                        <TextView
                            android:layout_weight="1"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:text="类型1"/>
                        <TextView
                            android:layout_weight="1"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:text="类型2"/>
                        <TextView
                            android:layout_weight="1"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:text="类型3"/>
                    </LinearLayout>
                </android.support.design.widget.AppBarLayout>
                //可滑动的布局,可以换成ListView之类的
                <android.support.v4.widget.NestedScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="1111111111"/>
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="22222222"/>
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="333333333333"/>
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="444444444444"/>
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:text="5555555555"/>
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textColor="@color/black"
                            android:padding="@dimen/layout_padding_10dp"
                            android:background="@color/gary_bg"
                            android:text="@string/deduction_analyze"/>
                    </LinearLayout>
                </android.support.v4.widget.NestedScrollView>
            </android.support.design.widget.CoordinatorLayout>
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

java代码:

SwipeRefreshLayout mRefreshLayout;
AppBarLayout mAppBarLayout;
mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.deduction_record_swipefreshlayout);
        mRefreshLayout.setOnRefreshListener(this);


        mAppBarLayout = (AppBarLayout) findViewById(R.id.app_bar_layout);
        //主要是解决header固定之后会引起刷新,可去除看下效果
        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                Log.i("测试===","verticalOffset:"+verticalOffset);
                //顶部可隐藏布局的滑动(verticalOffset类似Y坐标)
                if (verticalOffset == 0){
                    mRefreshLayout.setEnabled(true);
                }else{
                    mRefreshLayout.setEnabled(false);
                }
            }
        });

 

转载于:https://my.oschina.net/Jieth/blog/727593

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值