移动端实现下滑上拉更新内容

97 阅读1分钟

移动端实现下滑上拉更新内容

最近在写一个头条的项目,里面有一个功能,上拉下滑更新内容,

具体实现过程:

定义两个常量用来代表上拉和下滑

//下滑
int LOADTYPE_LOAD_MORE = 1;
//上拉
int LOADTYPE_LOAD_NEW = 2;

定义上拉和下滑两个接口

 /**
     * 上拉接口
     * @param dto
     * @return: com.heima.model.common.dtos.ResponseResult
     **/
    @PostMapping("/loadnew")
    public ResponseResult loadnew(@RequestBody ArticleHomeDto dto) {
        return apArticleService.load(dto, ApArticleConstants.LOADTYPE_LOAD_NEW);
    }


    /**
     * 下滑接口
     * @param dto
     * @return: com.heima.model.common.dtos.ResponseResult
     **/
    @PostMapping("/loadmore")
    public ResponseResult loadmore(@RequestBody ArticleHomeDto dto) {
        return apArticleService.load(dto, ApArticleConstants.LOADTYPE_LOAD_MORE);
    }

业务层,两个接口调用的都是分页查询方法

@Override
    public ResponseResult load(ArticleHomeDto dto, Integer type) {
        //一.校验参数
        if (dto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer size = dto.getSize();
        String tag = dto.getTag();
        if (size == null || size <= 0) {
            //10
            dto.setSize(ApArticleConstants.SIZE);
        }
        if (StringUtils.isBlank(tag)) {
            dto.setTag(ApArticleConstants.DEFAULT_TAG);
        }

        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }

        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date(0L));
        }
        //如果type的值错误了,把type的值设为下滑
        if (type <= 0 || type > 2) {
            type = 1;
        }
        //二.实现业务
        List<ApArticle> apArticleList = apArticleMapper.load(dto, type);
        //三.封装数据
        return ResponseResult.okResult(apArticleList);
    }

mapper层

<select id="load" parameterType="com.heima.model.article.dtos.ArticleHomeDto" resultMap="BaseResultMap">
        SELECT
            aa.<include refid="Base_Column_List">
            </include>
        FROM
        ap_article  aa LEFT JOIN  ap_article_config aac ON aa.id = aac.article_id
        WHERE aac.is_down = 0 AND aac.is_delete = 0
            <if test="dto.tag!='__all__'">
                AND aa.channel_id = #{dto.tag}
            </if>
        <if test="type == 1">
            AND aa.publish_time <![CDATA[<]]> #{dto.minBehotTime}
        </if>
        <if test="type == 2">
            AND aa.publish_time <![CDATA[>]]> #{dto.maxBehotTime}
        </if>
        ORDER BY aa.publish_time DESC
            LIMIT #{dto.size};
    </select>

下滑是当我移动端传过来的一个下拉的type=1和当前页面文章最新的时间minBehotTime(当前页的最新数据的发布时间)时,我查询的发布时间就要小于minBehotTime,这样我就把最新的内容刷新出来了。

上拉是当我移动端传过来的一个下拉的type=2和当前页面文章最新的时间maxBehotTime(当前页文章最晚发布的时间)时,我查询的发布时间就要大于minBehotTime,这样我就把之前发布的文章内容刷新出来了。