了解RecyclerView的分割线

android中RecyclerView的简单使用(二)——分割线 如果你觉得这些内容对你有帮助,可以扫码领取!!!! 阅读详情

在这里插入图片描述

效果

效果图

实现思路

  • 核心方法
    绘制分割线的核心方法onDraw();
    定位分割线位置(也可以说是与其他组件之间的空隙)方法getItemOffsets();

  • 计算
    每条分割线的长度都是根据每一个ItemVIew的长度计算,位置同样根据ItemView的位置来确定,详细的计算在drawTop()、drawLeft()、drawRight()、drawBottom()、四个方法中!

灵魂图解

灵魂的画师

完整代码

/**
 * 万能分割线
 * <p>
 * 横向列表分割线 {@link #HORIZONTAL_DIV}
 * 纵向列表分割线 {@link #VERTICAL_DIV}
 * 表格列表分割线 {@link #GRID_DIV}
 * </P>
 */
public class ItemDecorationPowerful extends RecyclerView.ItemDecoration {
    private static final String TAG = "ItemDecorationPowerful";
    //横向布局分割线
    public static final int HORIZONTAL_DIV = 0;
    //纵向布局分割线
    public static final int VERTICAL_DIV = 1;
    //表格布局分割线
    public static final int GRID_DIV = 2;
    private int mOrientation;
    private int mDividerWidth = 0;
    private Paint mPaint;

    /**
     * 默认纵向布分割线
     */
    public ItemDecorationPowerful() {
        this(VERTICAL_DIV);
    }

    /**
     * @param orientation 方向类型
     */
    public ItemDecorationPowerful(int orientation) {
        this(orientation, Color.parseColor("#808080"), 2);
    }

    /**
     * @param orientation 方向类型
     * @param color       分割线颜色
     * @param divWidth    分割线宽度
     */
    public ItemDecorationPowerful(int orientation, int color, int divWidth) {
        this.setOrientation(orientation);
        mDividerWidth = divWidth;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
    }


    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        switch (mOrientation) {
            case HORIZONTAL_DIV:
                //横向布局分割线
                drawHorizontal(c, parent);
                break;
            case VERTICAL_DIV:
                //纵向布局分割线
                drawVertical(c, parent);
                break;
            case GRID_DIV:
                //表格格局分割线
                drawGrid(c, parent);
                break;
            default:
                //纵向布局分割线
                drawVertical(c, parent);
                break;
        }
    }


    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int itemPosition = parent.getChildAdapterPosition(view);
        RecyclerView.Adapter mAdapter = parent.getAdapter();
        if (mAdapter != null) {
            int mChildCount = mAdapter.getItemCount();
            switch (mOrientation) {
                case HORIZONTAL_DIV:
                    /**
                     * 横向布局分割线
                     * <p>
                     *     如果是第一个Item,则不需要分割线
                     * </p>
                     */
                    if (itemPosition != 0) {
                        outRect.set(mDividerWidth, 0, 0, 0);
                    }
                    break;
                case VERTICAL_DIV:
                    /**
                     * 纵向布局分割线
                     * <p>
                     *     如果是第一个Item,则不需要分割线
                     * </p>
                     */
                    if (itemPosition != 0) {
                        outRect.set(0, mDividerWidth, 0, 0);
                    }
                    break;
                case GRID_DIV:
                    /**
                     * 表格格局分割线
                     * <p>
                     *      1:当是第一个Item的时候,四周全部需要分割线
                     *      2:当是第一行Item的时候,需要额外添加顶部的分割线
                     *      3:当是第一列Item的时候,需要额外添加左侧的分割线
                     *      4:默认情况全部添加底部和右侧的分割线
                     * </p>
                     */
                    RecyclerView.LayoutManager mLayoutManager = parent.getLayoutManager();
                    if (mLayoutManager instanceof GridLayoutManager) {
                        GridLayoutManager mGridLayoutManager = (GridLayoutManager) mLayoutManager;
                        int mSpanCount = mGridLayoutManager.getSpanCount();
                        if (itemPosition == 0) {//1
                            outRect.set(mDividerWidth, mDividerWidth, mDividerWidth, mDividerWidth);
                        } else if ((itemPosition + 1) <= mSpanCount) {//2
                            outRect.set(0, mDividerWidth, mDividerWidth, mDividerWidth);
                        } else if (((itemPosition + mSpanCount) % mSpanCount) == 0) {//3
                            outRect.set(mDividerWidth, 0, mDividerWidth, mDividerWidth);
                        } else {//4
                            outRect.set(0, 0, mDividerWidth, mDividerWidth);
                        }
                    }
                    break;
                default:
                    //纵向布局分割线
                    if (itemPosition != (mChildCount - 1)) {
                        outRect.set(0, 0, 0, mDividerWidth);
                    }
                    break;
            }
        }
    }

    /**
     * 绘制横向列表分割线
     *
     * @param c      绘制容器
     * @param parent RecyclerView
     */
    private void drawHorizontal(Canvas c, RecyclerView parent) {
        int mChildCount = parent.getChildCount();
        for (int i = 0; i < mChildCount; i++) {
            View mChild = parent.getChildAt(i);
            drawLeft(c, mChild, parent);
        }
    }

    /**
     * 绘制纵向列表分割线
     *
     * @param c      绘制容器
     * @param parent RecyclerView
     */
    private void drawVertical(Canvas c, RecyclerView parent) {
        int mChildCount = parent.getChildCount();
        for (int i = 0; i < mChildCount; i++) {
            View mChild = parent.getChildAt(i);
            drawTop(c, mChild, parent);
        }
    }

    /**
     * 绘制表格类型分割线
     *
     * @param c      绘制容器
     * @param parent RecyclerView
     */
    private void drawGrid(Canvas c, RecyclerView parent) {
        int mChildCount = parent.getChildCount();
        for (int i = 0; i < mChildCount; i++) {
            View mChild = parent.getChildAt(i);
            RecyclerView.LayoutManager mLayoutManager = parent.getLayoutManager();
            if (mLayoutManager instanceof GridLayoutManager) {
                GridLayoutManager mGridLayoutManager = (GridLayoutManager) mLayoutManager;
                int mSpanCount = mGridLayoutManager.getSpanCount();
                if (i == 0) {
                    drawTop(c, mChild, parent);
                    drawLeft(c, mChild, parent);
                }
                if ((i + 1) <= mSpanCount) {
                    drawTop(c, mChild, parent);
                }
                if (((i + mSpanCount) % mSpanCount) == 0) {
                    drawLeft(c, mChild, parent);
                }
                drawRight(c, mChild, parent);
                drawBottom(c, mChild, parent);
            }
        }
    }

    /**
     * 绘制右边分割线
     *
     * @param c            绘制容器
     * @param mChild       对应ItemView
     * @param recyclerView RecyclerView
     */
    private void drawLeft(Canvas c, View mChild, RecyclerView recyclerView) {
        RecyclerView.LayoutParams mChildLayoutParams = (RecyclerView.LayoutParams) mChild.getLayoutParams();
        int left = mChild.getLeft() - mDividerWidth - mChildLayoutParams.leftMargin;
        int top = mChild.getTop() - mChildLayoutParams.topMargin;
        int right = mChild.getLeft() - mChildLayoutParams.leftMargin;
        int bottom;
        if (isGridLayoutManager(recyclerView)) {
            bottom = mChild.getBottom() + mChildLayoutParams.bottomMargin + mDividerWidth;
        } else {
            bottom = mChild.getBottom() + mChildLayoutParams.bottomMargin;
        }
        c.drawRect(left, top, right, bottom, mPaint);
    }

    /**
     * 绘制顶部分割线
     *
     * @param c            绘制容器
     * @param mChild       对应ItemView
     * @param recyclerView RecyclerView
     */
    private void drawTop(Canvas c, View mChild, RecyclerView recyclerView) {
        RecyclerView.LayoutParams mChildLayoutParams = (RecyclerView.LayoutParams) mChild.getLayoutParams();
        int left;
        int top = mChild.getTop() - mChildLayoutParams.topMargin - mDividerWidth;
        int right = mChild.getRight() + mChildLayoutParams.rightMargin;
        int bottom = mChild.getTop() - mChildLayoutParams.topMargin;
        if (isGridLayoutManager(recyclerView)) {
            left = mChild.getLeft() - mChildLayoutParams.leftMargin - mDividerWidth;
        } else {
            left = mChild.getLeft() - mChildLayoutParams.leftMargin;
        }
        c.drawRect(left, top, right, bottom, mPaint);
    }

    /**
     * 绘制右边分割线
     *
     * @param c            绘制容器
     * @param mChild       对应ItemView
     * @param recyclerView RecyclerView
     */
    private void drawRight(Canvas c, View mChild, RecyclerView recyclerView) {
        RecyclerView.LayoutParams mChildLayoutParams = (RecyclerView.LayoutParams) mChild.getLayoutParams();
        int left = mChild.getRight() + mChildLayoutParams.rightMargin;
        int top;
        int right = left + mDividerWidth;
        int bottom = mChild.getBottom() + mChildLayoutParams.bottomMargin;
        if (isGridLayoutManager(recyclerView)) {
            top = mChild.getTop() - mChildLayoutParams.topMargin - mDividerWidth;
        } else {
            top = mChild.getTop() - mChildLayoutParams.topMargin;
        }
        c.drawRect(left, top, right, bottom, mPaint);
    }

    /**
     * 绘制底部分割线
     *
     * @param c            绘制容器
     * @param mChild       对应ItemView
     * @param recyclerView RecyclerView
     */
    private void drawBottom(Canvas c, View mChild, RecyclerView recyclerView) {
        RecyclerView.LayoutParams mChildLayoutParams = (RecyclerView.LayoutParams) mChild.getLayoutParams();
        int left = mChild.getLeft() - mChildLayoutParams.leftMargin;
        int top = mChild.getBottom() + mChildLayoutParams.bottomMargin;
        int bottom = top + mDividerWidth;
        int right;
        if (isGridLayoutManager(recyclerView)) {
            right = mChild.getRight() + mChildLayoutParams.rightMargin + mDividerWidth;
        } else {
            right = mChild.getRight() + mChildLayoutParams.rightMargin;
        }
        c.drawRect(left, top, right, bottom, mPaint);
    }

    /**
     * 判断RecyclerView所加载LayoutManager是否为GridLayoutManager
     *
     * @param recyclerView RecyclerView
     * @return 是GridLayoutManager返回true,否则返回false
     */
    private boolean isGridLayoutManager(RecyclerView recyclerView) {
        RecyclerView.LayoutManager mLayoutManager = recyclerView.getLayoutManager();
        return (mLayoutManager instanceof GridLayoutManager);
    }

    /**
     * 初始化分割线类型
     *
     * @param orientation 分割线类型
     */
    public void setOrientation(int orientation) {
        if (mOrientation != HORIZONTAL_DIV && mOrientation != VERTICAL_DIV && mOrientation != GRID_DIV) {
            throw new IllegalArgumentException("ItemDecorationPowerful:分割线类型设置异常");
        } else {
            this.mOrientation = orientation;
        }
    }
}
Android 活用RecyclerView分割线 1.前言 RecyclerVIew是我们日常开发中使用频率最高的控件了吧,之前由于比较马虎没好好整理过如何添加分割线,以前是通过文件或者在布局里投机取巧进行添加,现如今有空有必要好好的整理一番了。 宏观把控Recycyerview.ItemDecoration实现的方法 public class MyDecoration extends RecyclerView.ItemDecor... 阅读详情

相关推荐

RecyclerView使用ItemDecoration绘制分割线

本文介绍使用RecyclerView的抽象内部类ItemDecoration实现ItemView分割线的绘制。

进击的小宇宙 2185

RecyclerView系列之(2):为RecyclerView添加分隔线

一. 理解ListView和RecyclerView中的ChildView 在讲为Item加入分割线本质的前,先来介绍,认识一下ChildView,也就是平时我们用到的ListView,RecyclerView中的getChildAt(int position)这个返回的ChildView是哪一部分?到底是哪一部分呢?一开始的时候,我理解错了,但是经过下面两张图这么一比较,你就明白了:

AuthFailure的博客 1867

RecyclerView在GridLayoutManager情况下实现四周都有分割线的ItemDecoration

感谢:http://www.jianshu.com/p/d58cbd61c40a _SOLID 之前UI有个比较特别的需求,一个gridview形式的列表,但是四周都有白色的分割线: 类似这个效果,一开始以为会比较简单,然后写了之后才发现,目前网上找到的大部分方法话分割线都是省略掉屏幕边缘的部分的, 即第一行 第一列 最后一行 最后一列的没有分割线的 http://blog.csdn.n

皮皮鸡的android博客 1万+

RecyclerView添加分割线

RecyclerView 并没有 divider 属性,但是我们可以通过 RecyclerView 的 addItemDecoration() 来添加分割线,该方法参数为 RecyclerView.ItemDecoration。 介绍 当 RecyclerView 添加 ItemDecoration 后,RecyclerView 在绘制每个 item 的时候,会去绘制 decorator,也就是会调用 ItemDecoration 的 onDraw() 和 onDrawOver() 方法。 Recyc

Makenna's World 4618

自定义控件三部曲视图篇(六)——RecyclerView系列之三自定义LayoutManager

前言:把握生命里的每一分钟,全力以赴我们心中的梦,不经历风雨 怎么见彩虹,没有人能随随便便成功 在第一篇中已经讲过,LayoutManager主要用于布局其中的Item,在LayoutManager中能够对每个Item的大小,位置进行更改,将它放在我们想要的位置,在很多优秀的效果中,都是通过自定义LayoutManager来实现的,比如: 可以看到这些效果都非常棒,通过这一节的学习,大...

启舰 1万+

第四届“网鼎杯”网络安全大赛 - 青龙组

第四届“网鼎杯”网络安全大赛 - 青龙组

lpppp小公主的博客 1313

RecyclerView设置分隔线的三种方法,android高级面试题2019

/我们通过获取系统属性中的listDivider来添加,在系统中的AppTheme中设置android.R.attr.listDivider //在style.xml文件的AppTheme主题中中设定@drawable/divider ---- 见下文//设置屏幕的方向@Override}else {//画横线, 这里的parent其实是显示在屏幕显示的这部分//横线的左端必须是paddngleft,如果用left则横线过长(不显示)

2401_84132298的博客 1267

Proteus实现电子秤仿真与称重检测显示

Proteus实现电子秤仿真与称重检测显示测量原理(或设计方案)原理图代码 测量原理(或设计方案) 数字电子秤总体框图如图所示: 金属箔式应变片工作原理 电阻丝在外力作用下发生机械变形时,其电阻值发生变化,这就是电阻应变效应,描述 电阻应变效应的关系式 金属箔式应变片就是通过光刻、腐蚀等工艺制成的应变敏感元件。如图所示,将四个金属箔应变片(R1、R2、R3、R4)分别贴在双杆式悬臂梁弹性体的上下两侧,弹性体受到压力发生形变,应变片随悬臂梁形变被拉伸或被压缩 测量电路 通过应变片转换悬臂梁被测部位受力

Matataya的博客 1万+

RecyclerView源码解析之ItemDecoration

序言 RecyclerView有三大典型的功能,一个是Recycler的缓存机制,一个LayoutManager的布局管理,一个ItemDecoration的分割线绘制;本文将结合源码讲解其缓存机制 此为笔者暂时性文章,完整文章见RecyclerView之ItemDecoration 正文 一. Decoration RecyclerView提供了一个abstract的...

HusterYP的博客 404

如何实现chrome谷歌浏览器多开(独立环境 独立cookie)

由于各种各样的原因,你可能需要在一个电脑登录某个平台,比如一个电脑登录3个公众号,或者3个知乎等等。最简单的方案是,直接安装3个不同的浏览器,比如一个谷歌浏览器,一个火狐浏览器,一个 Edge 浏览器。这个方案也能解决,但如果想多开10个呢?安装10个不同的浏览器也能解决,但感觉这样比较复杂,另外一方面安装太多的应用,会导致电脑卡顿。这里给大家介绍,我在 Win 和 Mac 系统下的躲开解决方案。Win 系统下的解决方案最为简单,原理就是给不同的谷歌浏览器,指向不同的用户存储路径即可。

lzqial1987的专栏 1万+

YOLO26改进105:全网首发--使用BiFPN改进特征金字塔网络

YOLOv6 v3.0在目标检测领域实现重大突破,通过改进网络架构和训练方案显著提升性能。该版本在COCO数据集上展现出优越表现:YOLOv6-N达到37.5% AP/1187 FPS,YOLOv6-S实现45.0% AP/484 FPS,均超越同类检测器。大模型YOLOv6-M/L分别达到50.0%/52.8% AP,L6版本更创下实时检测新纪录。关键技术包括RepVGGBlock、BiFusion等新型模块,通过多分支融合和高效重参数化设计提升性能。研究团队已验证各改进模块有效性,代码已开源。

AIcurator的博客 95

RecyclerView.ItemDecoration 间隔线

内容已更新到:https://www.cnblogs.com/baiqiantao/p/19762fb101659e8f4c1cea53e7acb446.html  目录一个通用分割线ItemDecoration 分割线测试代码普通 Adapteritem 布局RecyclerView.ItemDecoration 简介API官方案例 DividerItemDecorationgetItemOff...

weixin_34403693的博客 150

第9章:Embedding、Pooling 与检索增强基础

本文介绍了律师事务所如何利用vLLM构建法律文档RAG系统,解决传统关键词检索在语义匹配上的不足。主要内容包括: 项目背景:传统Elasticsearch检索在法律术语上的低准确率问题,及多模型维护的复杂性; 技术方案:采用vLLM统一支持生成和向量化能力,通过语义向量检索提升相关性; 关键实现: 用vLLM的Embedding端点处理文档向量化 构建包含离线索引和在线检索的完整RAG流程 采用余弦相似度进行语义匹配 实践建议:切片大小选择、向量归一化处理、不同场景下的数据库选型等。

davidwang456的专栏 59

RecyclerView添加分割线的简便方法

1、前言刚开始学习RecyclerView的时候我跟着一个视频学的,当时添加分割线是从外面导入一个Java类,然后使用里面的函数来创建分割线的,所以一直以来我都是这样做的。直到前几天才无意中发现,原来v7包中提供了一个DividerItemDecoration类,利用它,我们可以很简单地实现RecyclerView的分割线!那还等什么呢?赶紧用起来。2、创建一个简单的RecyclerView首先当然

Lindroid的博客 8万+

基于深度学习的异常检测实战:从自编码器到GAN

本文通过一个完整的实战案例,展示了如何使用自编码器和GAN进行异常检测。我们详细介绍了数据准备、模型定义、训练和评估的步骤,并提供了可运行的代码。希望本文能为读者在实际项目中应用异常检测技术提供实用的指导和启发。

Programming Talk 242

9篇3章7节:基于 R 语言的 FAERS 数据提取与合并

本文主要探讨了 FAERS 数据提取与合并的操作流程。首先,通过程序化方法从本地存储的 FAERS ASCII 文件中提取指定季度或范围内的单药报告数据,并可通过参数设置控制数据存储位置、处理方式、计算资源分配以及报告者职业类别等。随后,通过批量读取的方式对多个季度的 DRUG 文件进行循环处理,并将其合并为一个综合数据框。

MD Code 1262

RecyclerView分割线

目录:         1.概述         2.分割线绘制方式与原理         3.通过paint绘制垂直布局/水平布局分割线         4.通过修改系统主题绘制垂直布局/水平布局分割线  1.概述         RecyclerView不像ListView可以通过android:divider=""布局属性来设置分割线,它需要我们去继承RecyclerVi

Andy_lau 2122

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Android RecyclerView —— 自定义分割线

Android RecyclerView —— 自定义分割线 Android RecyclerView —— 基本使用 Android RecyclerView —— 适配器封装探索 前面说了 RecyclerView 的基本使用以及对适配器的封装,但是在使用 ListView 时,有 dividerHeight 和 divider 属性用来设置分割线的高度和颜色(Drawable),但是在 Re...

ITRenj的博客 3706

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

正确使用RecyclerView分割线

RecyclerView是很强大的控件,基本可以替代ListView和GridView。但是RecyclerView没有封装一些listview的功能,例如分割线,item点击事件等等,需要自己实现。item点击事件在ViewHolder中设置Click事件就行了。实现分割线功能,则需要使用addItemDecoration添加一个自定义的分割线。 我使用的RecyclerView的版本是26....

chends 3178

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

RecyclerView去除底部分割线

RecyclerView 默认的分割线DividerItemDecoration 在最后一个item显示的时候也会有一个分割线,影响应用整体的美观。 通过DividerItemDecoration 源码发现分割线的行数是通过childCount来判断的,而childCount正是我们分割线的个数,那去除最后一个分割线也就简单了。 public void drawHori

fanenqian的博客 6631

Android RecyclerView ItemDecoration 分割线

文章目录如何设置分割线DividerItemDecoration使用 如何设置分割线 RecycleView 没有像ListView一样可以直接在 xml 中或者通过 setDivider()方法设置分割线的方法。它是通过 RecycleView 的 addItemDecoration(ItemDecoration decor) 方法来设置的。很显然,我们需要传入一个 ItemDecoration 对象,这个对象是一个抽象类,官方已经提供了一种常用分割线类:DividerItemDecoration。来看一

赵彦军 3713

去除RecyclerView底部(最后一条)分割线

做了一个横向滑动的RecyclerView 使用自定义分割线 宽度为10dp 因为左右都有padding值 最后一条如果有分割线太丑 也不符合需求 解决 网上参考了一些方法 大多都是继承 ItemDecoration 重写其方法 然后在循环创建分割线的循环里减一 但是 我发现不管用 分割线还在那 似乎是因为宽度太宽引起的? 那就继续修改,在给分割线高度赋值的时候 判断如果是最后一条...

qq_38619138的博客 6121

Android : RecyclerView 最简单优雅的添加分割线 ItemDecoration

1、自定义分割线 private class SpaceItemDecoration extends RecyclerView.ItemDecoration { private int mSpace; public SpaceItemDecoration() { float density = getResources().getDis...

beita08的博客 7839

RecyclerView GridLayoutManager 设置分割线 万能分割

万能的分割线,如有另需请自行修改 上效果图 下面是源码 package com.qianfandu.adapter; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.drawable.

u010523832的专栏 1万+
上一篇: Android自定义View实现拖动自动吸边效果
下一篇: Android自定义ViewGroup实现标签流效果
Android师哥
博客等级 码龄10年 3粉丝 14原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值