RecyclerView置顶效果

 

    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-5'  //下拉刷新依赖

 MainActivity的xml布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".OneFrgment.BlankFragment">

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

        <com.scwang.smartrefresh.layout.SmartRefreshLayout
            android:id="@+id/refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recy3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
        </com.scwang.smartrefresh.layout.SmartRefreshLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:background="@color/maincolor"
            android:src="@drawable/ic_arrow_upward"
            app:backgroundTint="@color/maincolor"
            />

    </RelativeLayout>
</FrameLayout>

 MainActivity的置顶效果主要代码 ,

   private void setRecyclerScroll(RecyclerView mRecyclerView) {
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            //RecyclerView  滑动 事件 -1 表示 向上, 1 表示向下。
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (!recyclerView.canScrollVertically(-1)) {
                } else if (!recyclerView.canScrollVertically(1)) {
                } else if (dy < 0) {
                    onScrolledUp();
                } else if (dy > 0) {
                    onScrolledDown();
                }
                super.onScrolled(recyclerView, dx, dy);
            }

            public void onScrolledUp() {
                View linearLayout = getActivity().findViewById(R.id.tab_layout);
                linearLayout.setVisibility(View.VISIBLE);
                fab.setVisibility(View.GONE);
            }

            public void onScrolledDown() {
                View view = getActivity().findViewById(R.id.tab_layout);
                view.setVisibility(View.GONE);
                fab.setVisibility(View.VISIBLE);
            }
        });
    }

 记得调用如上方法

        setRecyclerScroll(recy3);

点击时置顶 

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fab:
                recy3.smoothScrollToPosition(0);
                break;
        }
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值