PullToRefreshScrollView 嵌套RecyclerView实现特卖列表倒计时抢购
时间:2022-04-26
本文章向大家介绍PullToRefreshScrollView 嵌套RecyclerView实现特卖列表倒计时抢购,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
不久之前,我们谈到了通过Handler与timer及TimerTask结合实现倒计时抢购列表,那个是PullToRefreshListView实现的,今天要讲的是PullToRefreshScrollView 嵌套RecyclerView实现的抢购首页功能,相信在很多的app中都有实现的,不过我们知道特别是这种嵌套,滑动和计算高度的时候是各种冲突的,PullToRefreshScrollView 嵌套RecyclerView会有焦点的获取问题,好,今天就实现这么 一个功能。之前的功能请访问:点击打开链接
先上一张效果 图:
为了方便大家的理解,我将上面的两个子模块封装成了一个组件,我们今天只对下面的实现进行讲解。
首先这里倒计时写在子线程就不说了,还有就是用RecycleView而不用ListView这也不多说了,这方面比较的文章比较多,但是我在版本5.0的时候遇到一夜问题,就是RecyclerView的高度计算不出来,这里之前面试别人的时候也说过,这里不是对RecycleView的OnMeasure()重写,而是需要设置RecycleView的layoutManager,比如是要实现ListView的线性效果,就需要增加下面的Layoutparam.
public class WrapLinearLayoutManager extends LinearLayoutManager {
public WrapLinearLayoutManager(Context context) {
super(context);
}
public WrapLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
private int[] mMeasuredDimension = new int[2];
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int width = 0;
int height = 0;
for (int i = 0; i < getItemCount(); i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
height = height + mMeasuredDimension[1];
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
width = widthSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
try {
View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
@Override
public boolean canScrollVertically() {
return false;
}
}
如果是要实现Grid的效果,需要设置,这个GridLayoutManager系统帮我们实现了
public class WrapGridLayoutManager extends GridLayoutManager {
private int mwidth = 0;
private int mheight = 0;
public WrapGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
public WrapGridLayoutManager((Context context, int spanCount,
int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
private int[] mMeasuredDimension = new int[2];
public int getMwidth() {
return mwidth;
}
public void setMwidth(int mwidth) {
this.mwidth = mwidth;
}
public int getMheight() {
return mheight;
}
public void setMheight(int mheight) {
this.mheight = mheight;
}
@Override
public void onMeasure(RecyclerView.Recycler recycler,
RecyclerView.State state, int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int width = 0;
int height = 0;
int count = getItemCount();
int span = getSpanCount();
for (int i = 0; i < count; i++) {
measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i,
View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i,
View.MeasureSpec.UNSPECIFIED), mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
if (i % span == 0) {
width = width + mMeasuredDimension[0];
}
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
if (i % span == 0) {
height = height + mMeasuredDimension[1];
}
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
width = widthSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMheight(height);
setMwidth(width);
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler,
int position, int widthSpec, int heightSpec, int[] measuredDimension) {
if (position < getItemCount()) {
try {
View view = recycler.getViewForPosition(0);// fix
// 鍔ㄦ?佹坊鍔犳椂鎶ndexOutOfBoundsException
if (view != null) {
this.measureChild(view, 0, 0);
measuredDimension[0] = this.getDecoratedMeasuredWidth(view);
measuredDimension[1] = this.getDecoratedMeasuredHeight(view);
recycler.recycleView(view);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
outRect.top = space;
// Add top margin only for the first item to avoid double space between items
// if(parent.getChildLayoutPosition(view) == 0)
}
}
}
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
要解决PullToRefreshScrollView和RecyclerView我尝试了,通过OnIntercetor事件拦截,不起作用,最后只需要在RecycleView设置下面一段话就好了。
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false) {
@Override
public boolean canScrollVertically() {
return false;
}
};
recyclerView.setLayoutManager(linearLayoutManager);
最后附上代码的下载地址点击打开链接,欢迎留言(加群:278792776)
- Redis源码分析(一)——Redis数据结构-字符串SDS
- 使用RNN预测股票价格系列二
- 微软 WCF的几种寄宿方式,寄宿IIS、寄宿winform、寄宿控制台、寄宿Windows服务
- 深度学习框架之一:Theano | Lasagne简单教程
- 有趣的应用 | 使用RNN预测股票价格系列一
- 通过深度学习实现安全帽佩戴的检测
- python及numpy,pandas易混淆的点
- 深度学习中的损失函数总结以及Center Loss函数笔记
- TF使用例子-LSTM实现序列标注
- 图像学习-验证码识别
- ResNet原理及其在TF-Slim中的实现
- 理工男的文科梦 —— LSTM深度学习写春联
- keras学习笔记-黑白照片自动着色的神经网络-Alpha版
- keras学习笔记-黑白照片自动着色的神经网络-Beta版
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 特征锦囊:彻底了解一下WOE和IV
- Nginx专辑|05 -如何使用Nginx配置正向代理
- 详解 SIGHAN05 的目录结构
- 工业数据分析之数据归一化 | 冰水数据智能专题 | 2nd
- python写的小程序
- 不求甚解之 Spanning Tree
- C语言常用的一些转换工具函数收集
- OpenAI Gym入门级导游 | 附PDF手册下载 | 山人刷强化 | 4th
- python中常见关于Excel表格读写操作
- 2行代码搞定一个定时器!
- python算法题练习---二分法
- 局部加权线性回归 | 冰水数据智能专题 | 3rd
- 一文探讨 RPC 框架中的服务线程隔离
- Python3与Python2的具体区别
- 分布式系统的概念都搞懂了吗?(上)