安卓吸顶效果

当列表滑动时,图片逐渐消失,toolBar悬停在头部。

java 复制代码
<?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

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


            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:statusBarScrim="@android:color/transparent">

                <ImageView
                    android:id="@+id/sortbanner"
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/back_vip" />

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

            <LinearLayout
                android:id="@+id/llsort"

                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@color/purple_200"
                android:gravity="center_vertical">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:gravity="center"
                    android:text=""
                    android:textColor="#5A5A5A" />

                <TextView
                    android:id="@+id/tv"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="综合"
                    android:textColor="#5A5A5A" />

                <TextView
                    android:id="@+id/tv1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="销量"
                    android:textColor="#5A5A5A" />


                <RelativeLayout
                    android:id="@+id/rl"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.2"
                    android:gravity="right"
                    android:paddingRight="20dp">

                    <TextView
                        android:id="@+id/tv3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_toLeftOf="@+id/ft_cb"
                        android:text="价格"
                        android:textColor="#5A5A5A" />

                    <ImageView
                        android:id="@+id/ft_cb"
                        android:layout_width="wrap_content"
                        android:layout_height="16dp"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:button="@null"
                        android:src="@drawable/sortdown" />
                </RelativeLayout>

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

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


                <androidx.recyclerview.widget.RecyclerView

                    android:id="@+id/goodsRecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />


        </androidx.core.widget.NestedScrollView>


    </androidx.coordinatorlayout.widget.CoordinatorLayout>
java 复制代码
public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    private List<String> list;
    private NormalAdapter normalAdapter;
    private LinearLayoutManager linearLayoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.goodsRecyclerView);
        initList();
        initView();
    }

    private void initList() {
        list = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            list.add(i + "");
        }
    }

    private void initView() {
        linearLayoutManager = new LinearLayoutManager(this);
        normalAdapter = new NormalAdapter(list);

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(normalAdapter);
    }
}

demo地址

相关推荐
墨狂之逸才6 分钟前
React Native 移动项目目录导致的 Android 编译失败问题及解决方案
android·react native
feng一样的男子16 分钟前
住在手机里的“小龙虾” (OpenClaw):接入本地模型,解决记忆“装死”顽疾
android·ai·智能手机·openclaw
hongtianzai1 小时前
MySQL中between and的基本用法
android·数据库·mysql
Zender Han1 小时前
Flutter Bloc / Cubit 最新详解与实战指南(2026版)
android·flutter·ios
sun0077002 小时前
pthread_once
android
阿拉斯攀登3 小时前
第 20 篇 RK 平台 NPU / 硬件编解码驱动适配与安卓调用
android·驱动开发·瑞芯微·rk安卓驱动
Volunteer Technology4 小时前
mysql面试场景题(二)
android·mysql·面试
代码s贝多芬的音符4 小时前
Android NV21 转 YUV 系列格式
android·开发语言·python
匆忙拥挤repeat4 小时前
Android Compose 《编程思想》解读
android
进击的cc5 小时前
Activity 生命周期是如何被调度的?(从源码到实战全链路拆解)
android