当前位置:网站首页>Customize ViewGroup container horizontalscrollviewex
Customize ViewGroup container horizontalscrollviewex
2022-07-20 21:06:00 【Xiao Liu xuezhuo】
Here is a copy ViewGroup To increase your awareness of View Understanding of workflow . understand view The measurement of 、 Layout and drawing process .
Code first , Then analyze it line by line .
public class HorizontalScrollViewEx extends ViewGroup {
...
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measuredWidth = 0;
int measuredHeight = 0;
final int childCount = getChildCount();
measureChildren(widthMeasureSpec, heightMeasureSpec);
int widthSpaceSize = MeasureSpec.getSize(widthMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSpaceSize = MeasureSpec.getSize(heightMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
if (childCount == 0) {
setMeasuredDimension(0, 0);
} else if (heightSpecMode == MeasureSpec.AT_MOST) {
final View childView = getChildAt(0);
measuredHeight = childView.getMeasuredHeight();
setMeasuredDimension(widthSpaceSize, childView.getMeasuredHeight());
} else if (widthSpecMode == MeasureSpec.AT_MOST) {
final View childView = getChildAt(0);
measuredWidth = childView.getMeasuredWidth() * childCount;
setMeasuredDimension(measuredWidth, heightSpaceSize);
} else {
final View childView = getChildAt(0);
measuredWidth = childView.getMeasuredWidth() * childCount;
measuredHeight = childView.getMeasuredHeight();
setMeasuredDimension(measuredWidth, measuredHeight);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childLeft = 0;
final int childCount = getChildCount();
mChildrenSize = childCount;
for (int i = 0; i < childCount; i++) {
final View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
final int childWidth = childView.getMeasuredWidth();
mChildWidth = childWidth;
childView.layout(childLeft, 0, childLeft + childWidth,
childView.getMeasuredHeight());
childLeft += childWidth;
}
}
}
...
}
This is a Slide the container horizontally , It contains containers that slide up and down , So the page can turn left and right , Slide up and down to see more elements .
One 、 measurement onMeasure()
1.1 Why copy onMeasure() function
First of all to see onMeasure() Measurement function , It is used to measure viewgroup And its internal sub view Size . We know that if we inherit directly View, Must copy onMeasure() Method . What we inherit here is View Subclasses of ViewGroup, View source discovery ViewGroup There is no duplication View Of onMeasure(), So direct inheritance ViewGroup Subclasses of must also inherit onMeasure() Method , And in this function, the layoutPrams==Wrap_content In this case, custom processing is implemented . If you don't deal with , Then even if the child view Of layoutPrams==Wrap_content, Then view The actual size is the remaining size of the parent container .
Next up View To explain the above . space in between View Of layoutPrams==Wrap_content when , Its specMode=AT_MOST,getDefault() What's back is specSize, This value is the remaining space of the parent container .
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
public static int getDefaultSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
case MeasureSpec.UNSPECIFIED:
result = size;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}
1.2 Customize onMeasure() Internal logic
This part is measurement viewgroup Oneself and all the children inside view Size .
1、 Because we only need to modify View Of onMeasure() in AT_MOST The logic of , Others don't need to be changed . therefore
First, execute super.onMeasure(widthMeasureSpec, heightMeasureSpec); Keep the original logic ;
2、 Next, for AT_MOST Special treatment for special cases .
2.1 、 obtain viewgroup Inside view Number ;---viewgroup That's what we need
2.2、 Measure all internal sub view size ;---viewgroup That's what we need
2.3、 obtain viewgroup Wide and high specMode and specSize;
2.4、 Yes viewgroup Of specMode=AT_MOST Special treatment
2.4.1、childCount==0, Set up viewgroup The size is setMeasuredDimension(0, 0);
2.4.2、widthSpecMode == MeasureSpec.AT_MOST
HorizontalScrollViewEx Suppose all children view The width is the same as the height . So get the subscript 0 The son of view, Get its
Width ,” Son view Number * Son view Width “ Namely viewgroup Total width of . call setMeasuredDimension(measuredWidth, heightSpaceSize); Set up viewgroup The size of the .
2.4.3、heightSpecMode == MeasureSpec.AT_MOST The logic is the same as above ;
2.4.4、 Other situations .
Two 、 Layout onLayout()
Its internal logic is :
1、 obtain viewgroup All internal sub view Number ;
2、for Loop through all children view, Get each piece view Object for layout processing . If son view so , Get the son view The width value of , call
childView.layout(childLeft, 0, childLeft + childWidth,
childView.getMeasuredHeight());
According to the given positions of the upper left and lower right points view. The point above x,y The size of the direction is in viewgroup The upper left corner is obtained from the origin of the coordinate system .
边栏推荐
- 5. Complex graph network
- 【微信小程序】label(86/100)
- 《Reinforcement based mobile robot navigation in dynamic environment》翻译
- Son Zhengyi's 10000 word interview: everything will be redefined in the next 30 years!
- 动画,及动画的基本使用
- Collection of functions commonly used in vs + QT interface design
- 手机浏览器的扫一扫功能在哪里,有什么作用
- Dear bosses, how does MySQL CDC slice a table without a primary key?
- 在线会议中人脸面部轮廓图像提取(三)——Dlib库人脸面部轮廓图像特征提取
- 數字經濟時代下如何滿足多種雲環境安全需求?
猜你喜欢
在线会议中人脸面部轮廓图像提取(三)——Dlib库人脸面部轮廓图像特征提取
已知一棵二叉树的前序遍历以及中序遍历顺序,求这棵树的后序遍历
Getting started with kernel PWN (4)
几点建议:给想进入Web3的创作者们
Two ways to add background image in vs+qt interface (very practical)
img加载图片失败展示占位图的onerror事件
ECCV 2022 | 旷视提出半监督目标检测模型Dense Teacher,取得SOTA性能
Node, NVM, NRM, NPM, and yarn tutorial
安全浏览器怎么安装蓝色书签插件?
2022-7-13总结
随机推荐
在线会议中人脸面部轮廓图像提取(三)——Dlib库人脸面部轮廓图像特征提取
【JVM学习01】JVM的内存管理
洛谷P2420 让我们异或吧 题解
Collection of functions commonly used in vs + QT interface design
Li Kou classic binary tree topic
【组合逻辑电路】——显示译码器
匿名内部类使用的局部变量要用final修饰
2022杭电多校联赛第一场 题解
Wechat applet map call (quick learning)
3. Introduction to 3D point cloud foundation - pointnet
Web3的企业如何用Token激励员工?
Arrays.sort()自定义比较函数
Rust 中的所有权——Rust语言小小白的入门学习11
怎么发布一个自己的npm包
LLVM pass pwn 入门 (3)
Analyze Intel's path of continuous innovation in five dimensions!
场馆系统好用吗
这 20 道 Redis 经典面试题你还不会,就别去面试了!
LeetCode每日一题(396. Rotate Function)
Anaconda安装Jupyter