草庐IT

(一)Flutter 实现 Android CollapsingToolbarLayout折叠布局效果

Darleane 2023-03-28 原文

作为一名Flutter 浩瀚码海 中的一名Android 转学生,最近开发中遇到一个功能,要实现一个类似Android CollapsingToolbarLayout 折叠布局的效果,在Android 开发中我们通过 CoordinatorLayout + AppBarLayout +CollapsingToolbarLayout 来实现这个效果,但是在Flutter 中,则是通过 SliverAppBar ,借助它的属性 flexibleSpace 来实现。

咱们可以先来看看Android 实现代码:

<?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"
    tools:context=".chat.activitys.IntimacyActivity">

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

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

                <!-- 需要折叠的布局-->
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_310">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/intimacy_bg" />
                    <TextView
                        android:id="@+id/tv_familiar_count"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/dp_26"
                        android:layout_marginTop="@dimen/dp_74"
                        android:background="@drawable/familiar_count_bg"
                        android:gravity="center"
                        android:text="相识天数\n1000天"
                        android:textColor="@color/white"
                        android:textSize="@dimen/sp_11"
                        android:textStyle="bold" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@id/iv_my_head"
                        android:layout_marginStart="@dimen/dp_30"
                        android:layout_marginTop="@dimen/dp_18"
                        android:layout_toRightOf="@id/tv_familiar_count"
                        android:background="@drawable/intimacy_lighting" />

                    <com.donews.renrenplay.android.views.CircleImageView
                        android:id="@+id/iv_my_head"
                        android:layout_width="@dimen/dp_72"
                        android:layout_height="@dimen/dp_72"
                        android:layout_alignTop="@id/tv_familiar_count"
                        android:layout_marginTop="@dimen/dp_31"
                        android:layout_toRightOf="@id/tv_familiar_count"
                        android:src="@drawable/intimacy_head_bg"
                        app:borderColor="@color/white"
                        app:borderWidth="@dimen/dp_1" />

                    <com.donews.renrenplay.android.views.CircleImageView
                        android:id="@+id/iv_other_head"
                        android:layout_width="@dimen/dp_72"
                        android:layout_height="@dimen/dp_72"
                        android:layout_alignTop="@id/iv_my_head"
                        android:layout_marginStart="@dimen/dp_60"
                        android:layout_toRightOf="@id/iv_my_head"
                        android:src="@drawable/intimacy_head_bg"
                        app:borderColor="@color/white"
                        app:borderWidth="@dimen/dp_1" />

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/dp_51"
                        android:layout_below="@id/iv_my_head"
                        android:layout_marginStart="@dimen/dp_51"
                        android:layout_marginTop="@dimen/dp_34"
                        android:layout_marginEnd="@dimen/dp_51"
                        android:layout_marginBottom="@dimen/dp_11"
                      android:background="@drawable/shape_14_gradient_ffffff_f7e8ff">

                        <TextView
                            android:id="@+id/tv_intimacy_tip"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="@dimen/dp_14"
                            android:layout_marginTop="@dimen/dp_8"
                      android:background="@drawable/shape_4_gradient_c3a5ff_dfcdff"
                            android:paddingStart="@dimen/dp_4"
                            android:paddingEnd="@dimen/dp_4"
                            android:text="亲密度"
                            android:textColor="@color/white"
                            android:textSize="@dimen/sp_12" />

                        <TextView
                            android:id="@+id/tv_intimacy_percent_value"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_marginTop="@dimen/dp_6"
                            android:layout_marginEnd="@dimen/dp_15"
                            android:includeFontPadding="false"
                            android:text="0/0"
                            android:textColor="@color/c_8B5AED"
                            android:textSize="@dimen/sp_9" />

                        <ProgressBar
                            android:id="@+id/pb_intimacy_progress"
                            style="@android:style/Widget.ProgressBar.Horizontal"
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/dp_6"
                            android:layout_alignBaseline="@id/tv_intimacy_tip"
                            android:layout_marginStart="@dimen/dp_7"
                            android:layout_marginEnd="@dimen/dp_14"
                            android:layout_toRightOf="@id/tv_intimacy_tip"
                            android:max="100"
  android:progressDrawable="@drawable/layer_intimacy_pb_drawable" />

                        <TextView
                            android:id="@+id/tv_intimacy_authentication"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_below="@id/pb_intimacy_progress"
                            android:layout_marginStart="@dimen/dp_17"
                            android:layout_marginTop="@dimen/dp_4"
                            android:text="亲密度1000可认证亲密关系,快去提升亲密度吧~"
                            android:textColor="@color/c_C4A7FF"
                            android:textSize="@dimen/sp_10" />
                    </RelativeLayout>
                </RelativeLayout>

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/tb_intimacy"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_80"
                    android:background="@color/white"
                    app:contentInsetStart="0dp"
                    app:layout_collapseMode="pin">

                    <com.donews.renrenplay.android.views.TitleLayout
                        android:id="@+id/tl_intimacy"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="@dimen/dp_20"
                        app:showTitleStr="你和朋友的亲密关系" />
                </androidx.appcompat.widget.Toolbar>
            </com.google.android.material.appbar.CollapsingToolbarLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginTop="-20dp"
                android:background="@drawable/shape_22_top_ffffff"
                android:orientation="vertical">

                <com.donews.renrenplay.android.views.SimpleViewpagerIndicator
                    android:id="@+id/indicator_intimacy"
                    android:layout_width="wrap_content"
                    android:layout_height="@dimen/dp_58"
                    android:layout_gravity="center"
                    android:tag="overScroll"
                    app:isshow_underline="true"
                    app:selected_textcolor="@color/c_333333"
                    app:selected_textsize="@dimen/sp_16"
                    app:unselected_textcolor="@color/c_999999"
                    app:unselected_textsize="@dimen/sp_16" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_1"
                    android:background="@color/c_F5F5F5" />

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

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vp_intimacy"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

这个是实现的效果就是 折叠完布局之后,有个tab 指示器,下面是viewpage 页。

我们在实现这个效果之前先来看看 SliverAppbar 的一些比较重要的属性,

title :SliverAppbar 的标题 title;
centerTitle :标题居中;
pinned :true时 SliverAppBar 会固定在页面顶部;false时,SliverAppBar 会
随着滑动向上滑动;
floating :当值为true时 SliverAppBar设置的title会随着上滑动隐藏
然后配置的bottom会显示在原AppBar的位置
当值为false时 SliverAppBar设置的title会不会隐藏
然后配置的bottom会显示在原AppBar设置的title下面;
snap: 当snap配置为true时,向下滑动页面,SliverAppBar(以及其中配置的
flexibleSpace内容)会立即显示出来,
反之当snap配置为false时,向下滑动时,只有当ListView的数据滑动到
顶部时,SliverAppBar才会下拉显示出来;
expandedHeight:完全展开的高度;
elevation:阴影高度;
flexibleSpaceappbar 折叠的内容区域
bottomappbar 底部区域
1、SliverAppbar 主要有三部分,
第一部分:标题, 通过 title 属性配置;
第二部分:折叠的内容部分,通过 flexibleSpace 属性配置的
FlexibleSpaceBar 中配置 background;
第三部分:tabbar 标签栏, 通过bottom属性配置,在这里结合
PreferredSize 来使用。
2、 接下来我们将这三部分进行配置,我们现在initState 函数中 设置tabController 属性,以及tabs ,tabviews,其中HomePage(),ProfilePage() 就是两个单独拎出来的widget,vsync: this 中的这个this ,得让state 继承SingleTickerProviderStateMixin。

  TabController? tabController;
 //tabs
  List<Tab> tabs = [
    Tab(text: 'Home'),
    Tab(text: 'Profile'),
  ];

  //tabviews
  List<Widget> tabViews = [HomePage(), ProfilePage()];
  @override
  void initState() {
    super.initState();
    this.tabController = TabController(length: 2, vsync: this);
  }

3、接下来就是 build 函数,配置sliverAppbar ,TabBar, TabView 。

 @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: ExtendedNestedScrollView(
      onlyOneScrollInBody: true,
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [
          // sliverappbar 有三部分, 第一部分 标题,通过title属性配置;
          // 第二部分就是用来折叠部分的轮播图,通过 flexibleSpace 属性
          // 配置的FlexibleSpaceBar中配置;
          //第三部分就是通过 bottom 配置的 TabBar 标签栏,在这里结合 PreferredSize 来使用;
          SliverAppBar(
            //SliverAppbar 的标题 title
            title: buildHeader(),
            //标题居中
            centerTitle: true,
            //true时 SliverAppBar 会固定在页面顶部;false时,SliverAppBar 会随着滑动向上滑动
            pinned: true,
            //当值为true时 SliverAppBar设置的title会随着上滑动隐藏
            //然后配置的bottom会显示在原AppBar的位置
            //当值为false时 SliverAppBar设置的title会不会隐藏
            //然后配置的bottom会显示在原AppBar设置的title下面
             floating: false,
            // //当snap配置为true时,向下滑动页面,SliverAppBar(以及其中配置的flexibleSpace内容)会立即显示出来,
            // //反之当snap配置为false时,向下滑动时,只有当ListView的数据滑动到顶部时,SliverAppBar才会下拉显示出来。
             snap: false,
            // automaticallyImplyLeading: true,
            //展开的高度
            expandedHeight: 200,
            elevation: 0,
            //appbr 下的内容区域
           flexibleSpace: FlexibleSpaceBar(
              //背景
              //配置的是一个widget也就是说在这里可以使用任意的
              //Widget组合 在这里直接使用的是一个图片
              background: buildFlexibleSpaceWidget(),
        ),
            bottom: buildFlexibleTooBarWidget(), //appbar 底部区域
          ),
        ];
      },
      body: TabBarView(
        controller: this.tabController,
        children: tabViews,
      ),
    ));
  }

可以看到在这里我们用到了一个 插件 ExtendedNestedScrollView,为啥用这个呢?而不是CustomScrollview 呢? 可以看个效果图,
1655982361669.jpg

,使用CustomScrollview 时,当我们将列表滑动上去之后,会出现这种情况,item1,2 在上面固定拉不下来,除非当我们将折叠内容展开之后,列表才能拉下来正常展示。所以我们这里用到了ExtendedNestedScrollView插件。
3、在配置SliverAppbar 中用到的方法函数

buildFlexibleSpaceWidget() {
    return Stack(
      children: [
        Image.asset('assets/images/integral_center_bg.png', width: MediaQuery.of(context).size.width,fit: BoxFit.fill,),
        Container(
            margin: EdgeInsets.only(top: 100),
            child: Column(
              children: [
                Text('我是小米'),
                SizedBox(
                  height: 20,
                ),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('我的性别女'),
                    SizedBox(
                      width: 20,
                    ),
                    Text('我的年龄19'),
                  ],
                )
              ],
            )),
      ],
    );
  }
  //构建SliverAppBar的标题title
  buildHeader() {
    //透明组件
    return Container(
      width: double.infinity,
      padding: EdgeInsets.only(left: 10),
      height: 38,
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(color: Colors.white),
        borderRadius: BorderRadius.circular(30),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            Icons.search_rounded,
            size: 18,
          ),
          SizedBox(
            width: 4,
          ),
          Text(
            "搜索",
            style: TextStyle(
              fontSize: 14,
            ),
          ),
        ],
      ),
    );
  }
  //[SliverAppBar]的bottom属性配制
  buildFlexibleTooBarWidget() {
    //[PreferredSize]用于配置在AppBar或者是SliverAppBar
    //的bottom中 实现 PreferredSizeWidget
    return PreferredSize(
      //定义大小
      preferredSize: Size(MediaQuery.of(context).size.width, 44),
      //配置任意的子Widget
      child: Container(
        alignment: Alignment.center,
        child: Container(
          // color: Colors.grey,
          //随着向上滑动,TabBar的宽度逐渐增大
          //父布局Container约束为 center对齐
          //所以程现出来的是中间x轴放大的效果
          width: MediaQuery.of(context).size.width,
          child: TabBar(
            controller: tabController,
            tabs: tabs,
          ),
        ),
      ),
    );
  }

看下最终效果:(原谅下?,目前我的级别还不能插入视频,先上图片看看吧)


1655983825671.jpg

1655983851869.jpg

1655983881794.jpg

这时你会发现一个现象, tabbar 现在是在sliverAppbar 底部,有些人可能不太想要这个效果,就想要将tabbar 放在SliverAppbar 的下面,其实也是可以的哦~~

我们可以将SliverAppbar 原来的基础上将bottom属性去掉,然后 再追加个SliverPersistentHeader 组件,我把整个修改后的build函数中的代码展示下,这样方便和上面的代码做对比。

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: ExtendedNestedScrollView(
      onlyOneScrollInBody: true,
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [
          // sliverappbar 有三部分, 第一部分 标题,通过title属性配置;
          // 第二部分就是用来折叠部分的轮播图,通过 flexibleSpace 属性
          // 配置的FlexibleSpaceBar中配置;
          //第三部分就是通过 bottom 配置的 TabBar 标签栏,在这里结合 PreferredSize 来使用;
          SliverAppBar(
            //SliverAppbar 的标题 title
            title: buildHeader(),
            //标题居中
            centerTitle: true,
            //true时 SliverAppBar 会固定在页面顶部;false时,SliverAppBar 会随着滑动向上滑动
            pinned: true,

            //当值为true时 SliverAppBar设置的title会随着上滑动隐藏
            //然后配置的bottom会显示在原AppBar的位置
            //当值为false时 SliverAppBar设置的title会不会隐藏
            //然后配置的bottom会显示在原AppBar设置的title下面
            // floating: false,
            //
            // //当snap配置为true时,向下滑动页面,SliverAppBar(以及其中配置的flexibleSpace内容)会立即显示出来,
            // //反之当snap配置为false时,向下滑动时,只有当ListView的数据滑动到顶部时,SliverAppBar才会下拉显示出来。
            // snap: false,
            // automaticallyImplyLeading: true,
            //展开的高度
            expandedHeight: 200,
            elevation: 0,
            //appbr 下的内容区域
            flexibleSpace: FlexibleSpaceBar(
              //背景
              //配置的是一个widget也就是说在这里可以使用任意的
              //Widget组合 在这里直接使用的是一个图片
              background: buildFlexibleSpaceWidget(),
            ),

            // bottom: buildFlexibleTooBarWidget(), //appbar 底部区域
          ),
          SliverPersistentHeader(
            pinned: true,
            delegate: StickyTabBarDelegate(
              child: TabBar(
                labelColor: Colors.black,
                controller: this.tabController,
                tabs: tabs,
              ),
            ),
          ),
        ];
      },
      body: TabBarView(
        controller: this.tabController,
        children: tabViews,
      ),
    ));
  }

其中我们会发现,SliverPersistentHeader 中的delegate 是我们创建的一个class类StickyTabBarDelegate,

 class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar child;

  StickyTabBarDelegate({required this.child});

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      color: Colors.green,
      child: this.child,
    );
  }

  @override
  double get maxExtent => this.child.preferredSize.height;

  @override
  double get minExtent => this.child.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}

在build 函数中我们可以设置tabbar 的背景色哟~~~
看下效果图,大家可以对比上面的效果图,其他滑动效果和上面是一样的哈!


微信图片_20220624150323.jpg

有关(一)Flutter 实现 Android CollapsingToolbarLayout折叠布局效果的更多相关文章

  1. ruby - 如何根据特征实现 FactoryGirl 的条件行为 - 2

    我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden

  2. ruby - nanoc 和多种布局 - 2

    是否可以为特定(或所有)项目使用多个布局?例如,我有几个项目,我想对其应用两种不同的布局。一个是绿色的,一个是蓝色的(但是)。我想将它们编译到我的输出目录中的两个不同文件夹中(例如v1和v2)。我一直在玩弄规则和编译block,但我不知道这是怎么回事。因为,每个项目在编译过程中只编译一次,我不能告诉nanoc第一次用layout1编译,第二次用layout2编译。我试过这样的东西,但它导致输出文件损坏。compile'*'doifitem.binary?#don’tfilterbinaryitemselsefilter:erblayout'layout1'layout'layout2'

  3. 华为OD机试用Python实现 -【明明的随机数】 2023Q1A - 2

    华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o

  4. 基于C#实现简易绘图工具【100010177】 - 2

    C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

  5. MIMO-OFDM无线通信技术及MATLAB实现(1)无线信道:传播和衰落 - 2

     MIMO技术的优缺点优点通过下面三个增益来总体概括:阵列增益。阵列增益是指由于接收机通过对接收信号的相干合并而活得的平均SNR的提高。在发射机不知道信道信息的情况下,MIMO系统可以获得的阵列增益与接收天线数成正比复用增益。在采用空间复用方案的MIMO系统中,可以获得复用增益,即信道容量成倍增加。信道容量的增加与min(Nt,Nr)成正比分集增益。在采用空间分集方案的MIMO系统中,可以获得分集增益,即可靠性性能的改善。分集增益用独立衰落支路数来描述,即分集指数。在使用了空时编码的MIMO系统中,由于接收天线或发射天线之间的间距较远,可认为它们各自的大尺度衰落是相互独立的,因此分布式MIMO

  6. 【Java入门】使用Java实现文件夹的遍历 - 2

    遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg

  7. ruby - Arrays Sets 和 SortedSets 在 Ruby 中是如何实现的 - 2

    通常,数组被实现为内存块,集合被实现为HashMap,有序集合被实现为跳跃列表。在Ruby中也是如此吗?我正在尝试从性能和内存占用方面评估Ruby中不同容器的使用情况 最佳答案 数组是Ruby核心库的一部分。每个Ruby实现都有自己的数组实现。Ruby语言规范只规定了Ruby数组的行为,并没有规定任何特定的实现策略。它甚至没有指定任何会强制或至少建议特定实现策略的性能约束。然而,大多数Rubyist对数组的性能特征有一些期望,这会迫使不符合它们的实现变得默默无闻,因为实际上没有人会使用它:插入、前置或追加以及删除元素的最坏情况步骤复

  8. ruby - "public/protected/private"方法是如何实现的,我该如何模拟它? - 2

    在ruby中,你可以这样做:classThingpublicdeff1puts"f1"endprivatedeff2puts"f2"endpublicdeff3puts"f3"endprivatedeff4puts"f4"endend现在f1和f3是公共(public)的,f2和f4是私有(private)的。内部发生了什么,允许您调用一个类方法,然后更改方法定义?我怎样才能实现相同的功能(表面上是创建我自己的java之类的注释)例如...classThingfundeff1puts"hey"endnotfundeff2puts"hey"endendfun和notfun将更改以下函数定

  9. ruby - Sublime Text 3 多行法折叠 - 2

    所以...SublimeText具有折叠方法的内置功能,但是一旦方法声明跨越多行,它就会失去这种能力。有谁知道插件或使它工作的方法吗?具体来说,我在使用ruby​​时遇到了这个问题(我的团队遵守关于行长度的严格风格指南),但语言应该无关紧要。 最佳答案 无需单击出现在函数定义第一行旁边的装订线中的向下箭头,您需要做的就是将光标放在函数的一个缩进行上(不是缩进的函数参数,而是在函数定义本身)并使用CtrlShift[键绑定(bind)(在OSX上使用⌘Alt[)折叠函数及其参数。使用CtrlShift](⌘Alt]在OSX上)展开,或

  10. ruby - 实现k最近邻需要哪些数据? - 2

    我目前有一个reddit克隆类型的网站。我正在尝试根据我的用户之前喜欢的帖子推荐帖子。看起来K最近邻或k均值是执行此操作的最佳方法。我似乎无法理解如何实际实现它。我看过一些数学公式(例如k表示维基百科页面),但它们对我来说并没有真正意义。有人可以推荐一些伪代码,或者可以查看的地方,以便我更好地了解如何执行此操作吗? 最佳答案 K最近邻(又名KNN)是一种分类算法。基本上,您采用包含N个项目的训练组并对它们进行分类。如何对它们进行分类完全取决于您的数据,以及您认为该数据的重要分类特征是什么。在您的示例中,这可能是帖子类别、谁发布了该项

随机推荐