Android 滑动悬停效果

先上效果图

此代码是基于androidx版本,如果需以前直接替换即可

implementation 'androidx.appcompat:appcompat:1.1.0'

1.XML文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:elevation="0dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:scaleType="fitXY"
                    android:src="@mipmap/aa"/>
            </LinearLayout>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@android:color/white"
            android:orientation="horizontal">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:gravity="center"
                android:background="@color/colorAccent"
                android:text="我是悬浮固定"/>

        </LinearLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </RelativeLayout>



</androidx.coordinatorlayout.widget.CoordinatorLayout>

 

其中CoordinatorLayout,AppBarLayout、CollapsingToolbarLayout、RecyclerView都可替换成

android.support.design.widget.CoordinatorLayout

android.support.design.widget.AppBarLayout

android.support.design.widget.CollapsingToolbarLayout

android.support.v7.widget.RecyclerView

2.MainActivity

吸附悬停Activity不需要操作什么代码,全部在XML中实现了

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ButterKnife.bind(this);

        initView();

    }


    private void initView(){

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);

        ItemAdapter itemGroceryAdapter = new ItemAdapter(this);

        recyclerView.setAdapter(itemGroceryAdapter);
        List<String> strings = new ArrayList<>();
        for(int i = 0; i < 50; i++){
            strings.add(i+"梦幻西游");
        }
        itemGroceryAdapter.replaceData(strings);

    }
}

以上就是新版的吸附悬停效果,需要注意的地方

android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:layout_behavior="@string/appbar_scrolling_view_behavior"

 

如果滑动区域不正确,请检查xml是否顺序zh正确。

另外悬停和RecyclerView可自行替换ViewPager或者tabLayout

注意别搞混了。

PS,旧版可能还需要

<android.support.design.widget.AppBarLayout
                android:id="@+id/app_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:fitsSystemWindows="true"
                app:elevation="0dp"
                app:layout_behavior="cn.gs.m2m.phone.utils.FlingBehavior">
FlingBehavior,直接搜索就有了,或者下载下面的DEMO。

代码地址:https://download.csdn.net/download/u010090644/12294972

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

又双㕛叕、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值