RecycleView和ListView 实现悬停头部效果和滑动隐藏顶部效果

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014005316/article/details/81432775

先上一张效果图

代码实现

MainActivity.java

package com.xiaopo.flying.suspensionbar.actvity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.xiaopo.flying.suspensionbar.R;
import com.xiaopo.flying.suspensionbar.adapter.FeedAdapter;
import com.xiaopo.flying.suspensionbar.bean.BackDatas;
import com.xiaopo.flying.suspensionbar.bean.StatusBean;
import com.xiaopo.flying.suspensionbar.bean.TimeBean;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements OnLoadmoreListener {
    private RecyclerView mFeedList;
    private RelativeLayout mSuspensionBar;
    private SmartRefreshLayout mRefreshLayout;
    private TextView tv_day;
    private TextView tv_week;
    private int mCurrentPosition = 0;
    private int mSuspensionHeight;
    private BackDatas mBackDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initDatas();
        updateTimes();
    }

    private void initViews() {


        mSuspensionBar = (RelativeLayout)findViewById(R.id.suspension_bar);
        tv_day = (TextView)findViewById(R.id.tv_day);
        tv_week = (TextView)findViewById(R.id.tv_week);


        mRefreshLayout = (SmartRefreshLayout)findViewById(R.id.refreshLayout);
        mRefreshLayout.setEnableRefresh(false);
        ClassicsFooter footer = new ClassicsFooter(this);
        footer.setTextSizeTitle(14);
        footer.setAccentColor(Color.parseColor("#ffffff"));
        mRefreshLayout.setRefreshFooter(footer);
        mRefreshLayout.setEnableLoadmoreWhenContentNotFull(false);//取消内容不满一页时开启上拉加载功能
        mRefreshLayout.setEnableAutoLoadmore(true);
        mRefreshLayout.setOnLoadmoreListener(this);


        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mFeedList = (RecyclerView) findViewById(R.id.feed_list);
        mFeedList.setLayoutManager(linearLayoutManager);



        mFeedList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                mSuspensionHeight = mSuspensionBar.getHeight();
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View view = linearLayoutManager.findViewByPosition(mCurrentPosition + 1);
                if (view != null) {
                    if (view.getTop() <= mSuspensionHeight) {
                        mSuspensionBar.setY(-(mSuspensionHeight - view.getTop()));
                    } else {
                        mSuspensionBar.setY(0);
                    }
                }

                if (mCurrentPosition != linearLayoutManager.findFirstVisibleItemPosition()) {
                    mCurrentPosition = linearLayoutManager.findFirstVisibleItemPosition();
                    updateTimes();
                    mSuspensionBar.setY(0);
                }
            }
        });
    }

    private void initDatas() {
        mBackDatas = new BackDatas();
        mBackDatas.setCurrent_status("关闭");
        mBackDatas.setCurrent_time("今天 12:30");
        ArrayList<StatusBean> statusBeanList = new ArrayList<>();
        StatusBean mStatusBean = new StatusBean();
        mStatusBean.setDay_time("今天");
        mStatusBean.setWeek_time("周五");
        ArrayList<TimeBean> timeBeanList = new ArrayList<>();
        TimeBean timeBean0 = new TimeBean();
        timeBean0.setCode("0");
        timeBean0.setData_time("12:10 关闭");
        timeBeanList.add(timeBean0);
        TimeBean timeBean1 = new TimeBean();
        timeBean1.setCode("1");
        timeBean1.setData_time("12:10 关闭");
        timeBeanList.add(timeBean1);
        TimeBean timeBean2 = new TimeBean();
        timeBean2.setCode("2");
        timeBean2.setData_time("12:10 关闭");
        timeBeanList.add(timeBean2);
        mStatusBean.setTimeBeanList(timeBeanList);
        statusBeanList.add(mStatusBean);
        mBackDatas.setStatusBeanList(statusBeanList);
        final FeedAdapter adapter = new FeedAdapter(this);
        mFeedList.setAdapter(adapter);
        mFeedList.setHasFixedSize(true);

    }

    private void updateTimes() {
        tv_day.setText("今天");
        tv_week.setText("周五");
    }


    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.finishLoadmore();
    }
}

布局文件

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2EB6F4"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/root"
            android:layout_width="match_parent"
            android:layout_height="46.7dp">

            <RelativeLayout
                android:id="@+id/back_root"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:paddingRight="24dp">

                <ImageView
                    android:id="@+id/back"
                    android:layout_width="12dp"
                    android:layout_height="21dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"
                    android:background="@drawable/icon_back" />
            </RelativeLayout>


            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="门窗传感器"
                android:textColor="#ffffff"
                android:textSize="18sp" />


            <ImageView
                android:id="@+id/more"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:background="@drawable/icon_more_history" />
        </RelativeLayout>
    </RelativeLayout>

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srlHeaderHeight="60dp">

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/app_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                app:elevation="0dp">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:layout_scrollFlags="scroll">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <FrameLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:layout_marginTop="50dp"
                            android:paddingTop="20dp">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:background="@drawable/sharp_gredient"
                                android:scaleType="centerCrop"
                                app:layout_collapseMode="parallax" />

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:orientation="vertical">

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center_horizontal"
                                    android:text="当前状态"
                                    android:textColor="#ffffff"

                                    android:textSize="14sp" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center_horizontal"
                                    android:layout_marginTop="15dp"
                                    android:text="关闭"
                                    android:textColor="#ffffff"
                                    android:textSize="34sp" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center_horizontal"
                                    android:layout_marginTop="5dp"
                                    android:text="今天 12:30"
                                    android:textColor="#ffffff"
                                    android:textSize="16sp" />
                            </LinearLayout>
                        </FrameLayout>


                    </LinearLayout>


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


            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:background="#2EB6F4"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/feed_list"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scrollbars="vertical" />

                <RelativeLayout
                    android:id="@+id/suspension_bar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#2EB6F4">

                    <LinearLayout
                        android:id="@+id/ll_nickname"
                        android:layout_width="wrap_content"
                        android:layout_height="45dp"
                        android:layout_centerHorizontal="true"
                        android:gravity="center"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/tv_day"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:textColor="#ffffff"
                            android:textSize="18sp" />

                        <TextView
                            android:id="@+id/tv_week"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:text="星期五"
                            android:textColor="#ffffff"
                            android:textSize="12sp" />

                    </LinearLayout>

                    <View
                        android:id="@+id/top_divider"
                        android:layout_width="match_parent"
                        android:layout_height="0.2dp"
                        android:layout_below="@id/ll_nickname"
                        android:background="#00000000"
                        android:visibility="invisible" />
                </RelativeLayout>
            </FrameLayout>

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


    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>

源码下载

源码:https://github.com/zeke123/SuspensionBar-master

猜你喜欢

转载自blog.csdn.net/u014005316/article/details/81432775