android: NestedScrollView的作用

一,NestedScrollView与ScrollView的区别:

NestedScrollView 和 ScrollView 的主要区别在于它具备“嵌套滑动”(Nested Scrolling)功能。
在 Android 中,嵌套滑动是一种滚动冲突处理机制,允许父视图和子视图协同工作,共同处理滑动事件。
这种机制非常有用,特别是当你在一个滚动视图中嵌套另一个滚动视图时,它能够有效避免滑动冲突。

ScrollView:不支持嵌套滑动,通常会出现父子滑动视图的事件冲突,导致滑动体验不佳。
NestedScrollView:内置嵌套滑动机制,能够更好地处理父子视图的滚动事件,使页面更加流畅。

二,代码:不使用NestedScrollView

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.Recycle2Activity">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:background="#ff00ff"
                android:text="头部"
                android:textSize="20sp"
                android:gravity="center" />

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:text="底部"
                android:textSize="20sp"
                android:background="#ffff00"
                android:gravity="center" />

        </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

item的部局

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textItem"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:gravity="center_vertical"
    android:background="#dddddd"
    android:padding="16dp"
    android:textSize="18sp"/>

java

activity

package com.example.okdemo1.activity;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.okdemo1.R;
import com.example.okdemo1.adapter.MyRecycleAdapter;
import com.example.okdemo1.adapter.StringRecycleAdapter;

import java.util.ArrayList;
import java.util.List;

public class Recycle2Activity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private StringRecycleAdapter adapter;
    private List<String> stringList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_recycle2);

        recyclerView = findViewById(R.id.recyclerView);
        stringList = new ArrayList<>();

        // 添加测试数据
        for (int i = 1; i <= 20; i++) {
            stringList.add("第 " + i + " 项");
        }

        adapter = new StringRecycleAdapter(Recycle2Activity.this,stringList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this)); // 垂直列表
        recyclerView.setAdapter(adapter);

    }

adapter:

package com.example.okdemo1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.okdemo1.R;
import com.example.okdemo1.model.UserListItemEntity;

import java.util.List;

public class StringRecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private static final int TYPE_TEXT = 0;
    private static final int TYPE_IMAGE = 1;

    private List<String> dataList;

    public StringRecycleAdapter(Context context, List<String> dataList) {
        this.dataList = dataList;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text, parent, false);
            return new TextViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((TextViewHolder) holder).bind(this.context,(String) dataList.get(position));
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

// ViewHolder for text item
class TextViewHolder extends RecyclerView.ViewHolder  {
    private TextView textView;

    public TextViewHolder(View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.textItem);
    }

    public void bind(Context text,String one) {
        textView.setText(one);
    }

}


}   //整个class结束

效果:
header/footer部分是固定的,不会跟着recyclerview滚动

 

 

三,代码:使用NestedScrollView

更换上面代码中的activity的layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.Recycle2Activity">

    <androidx.core.widget.NestedScrollView
        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:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:background="#ff00ff"
                android:text="头部"
                android:textSize="20sp"
                android:gravity="center" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:text="底部"
                android:textSize="20sp"
                android:background="#ffff00"
                android:gravity="center" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>

效果:

在nestedScrollview中,
header和footer跟随recyclerview的滑动而移动

posted @ 2025-06-02 12:22  刘宏缔的架构森林  阅读(159)  评论(0)    收藏  举报