当前位置:网站首页>Custom view - extensible collapsexpendview
Custom view - extensible collapsexpendview
2022-06-13 06:06:00 【Little xian】
Custom scalable ViewGroup
design sketch
Specific code and detailed explanation :
The main points for attention
- stay OnMeasure Call in measureChild Method , This method calls the child View Of Measure Method , obtain MeasuredWidth and MeasureHeight. The parent... Can then be calculated from these measured values View Height .
- Execute... In the constructor addView The operation will prepare to add view Add to , Include those not initially shown ImageView, Add a background picture when you click the button on the right , Click again to set the background to blank , After setting View Of requestLayout and invalidate To redraw .
public class CollapsExpendView extends ViewGroup{
private static String TAG="CollapsExpendView";
// Image array
private static final int[] ARR_IMAGES = {
R.drawable.sample_0,
R.drawable.sample_1,
R.drawable.sample_2,
};
private TextView textview[];
private ImageButton button[];
private ImageView imageView[];
private Context context;
private Boolean ifexpend=false;
private int linearLayout_height,linearLayout_width;
private static final String[] names={
" Dog 111111",
" Dog 222222",
" Dog 333333"
};
private LinearLayout linearLayout[];
public CollapsExpendView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
this.context=context;
addViewFirst();
}
public CollapsExpendView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
this.context=context;
}
public CollapsExpendView(Context context) {
this(context,null);
this.context=context;
// TODO Auto-generated constructor stub
}
public void addViewFirst(){
Log.i(TAG,"addViewFirst");
linearLayout = new LinearLayout[3];
textview=new TextView[3];
button= new ImageButton[3];
imageView=new ImageView[3];
for(int i=0;i<3;i++){
linearLayout[i]=new LinearLayout(context);
imageView[i]=new ImageView(context);
// Set width and height , At this time, it is only executed in the constructor , Hasn't arrived yet View Of Measure Stage , call get The height and width obtained by the method are 0;
LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout[i].setOrientation(LinearLayout.HORIZONTAL);
// For horizontal LinearLayout Add three view
setLinearLayoutView(linearLayout[i],i);
linearLayout[i].setLayoutParams(params);
imageView[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// Holistic viewGroup Add three vertical view, It's a Layout, A dividing line , A picture ( Because it is wrap_Content Do not show at first ), One is the dividing line
this.addView(linearLayout[i]);
this.addView(imageView[i]);
this.addView(getDividerView());
}
}
public View getDividerView(){
ImageView divider=new ImageView(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
divider.setLayoutParams(params);
divider.setBackgroundResource(R.drawable.category_list_divider);
return divider;
}
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
// TODO Auto-generated method stub
int count = getChildCount();
int sumHeight=0;
// According to each sub View Set the size of , Set each child View The location of
for(int i = 0 ;i < count;i++) {
View child= getChildAt(i);
child.layout(0, sumHeight,child.getMeasuredWidth(), sumHeight+child.getMeasuredHeight());
sumHeight+=child.getMeasuredHeight();
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
}
/*
* A parent view may call more than once on its child views measure(int,int) Method .
* for example , The parent view can use unspecified dimensions To measure each of its sub views once to figure out how big they really need to be ,
* If the sum of the sizes of all these sub views is not limited to too large or too small , Then it will call again with the exact value measure()
* ( in other words , If the subviews are not satisfied with the area size they get , Then the parent view will interfere and set the second measurement rule ).
* @see android.view.View#onMeasure(int, int)
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Get parent view The widest display limit of
int rw = MeasureSpec.getSize(widthMeasureSpec);
int rh = MeasureSpec.getSize(heightMeasureSpec);
Log.i(TAG,"rw: "+rw+"rh: "+rh);
int count=this.getChildCount();
int sumHeight=0;// Height accumulation
View child=null;
for (int i = 0; i < count; i++) {
child = this.getChildAt(i);
Log.i(TAG,"child:width》》 "+child.getWidth()+"LayoutParams》》 "+child.getLayoutParams().width+"getMeasuredWidth》》 "+child.getMeasuredWidth());
this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
// In the first measurement , Will use UNSPECIFIED Measurer View Expectant size, So in customizing viewgroup in ,
// call Measurechild Then there will be getMeasureHeight and getMeasureWidth Value
Log.i(TAG,"child:width》》 "+child.getWidth()+"LayoutParams》》 "+child.getLayoutParams().width+"getMeasuredWidth》》 "+child.getMeasuredWidth());
sumHeight+=child.getMeasuredHeight();
}
setMeasuredDimension(rw, sumHeight);
}
public void setLinearLayoutView(LinearLayout layout,int i){
// Set up textView
textview[i] =new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,88);
textview[i].setLayoutParams(params);
textview[i].setText(names[i]);
textview[i].setTextSize(32);
layout.addView(textview[i],params);
// Set the fill Layout, The weight is 1
LayoutParams params2= new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
layout.addView(new View(context),params2);
// Set button
button[i]=new ImageButton(context);
LayoutParams params_button = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
button[i].setLayoutParams(params_button);
button[i].setBackgroundResource(R.drawable.btn_alarms_more);
button[i].setOnClickListener(getButtonOnclickListener(button[i],i));
layout.addView(button[i]);
}
private OnClickListener getButtonOnclickListener(final ImageButton button, final int i) {
// TODO Auto-generated method stub
return new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(ifexpend==false){// It means contraction , Click to expand
button.setBackgroundResource(R.drawable.btn_alarms_up);
ifexpend=true;
imageView[i].setBackgroundResource(ARR_IMAGES[i]);
}
else{
button.setBackgroundResource(R.drawable.btn_alarms_more);
ifexpend=false;
imageView[i].setBackground(null);
}
CollapsExpendView.this.requestLayout();
CollapsExpendView.this.invalidate();
}
};
}
}
边栏推荐
- Alibaba cloud OSS file download cannot be resumed at a breakpoint
- MySQL custom function
- Mobile end adaptation scheme
- Complete USB debugging assistant
- [spark]spark introductory practical series_ 8_ Spark_ Mllib (upper)__ Introduction to machine learning and sparkmllib
- Commit specification
- How slow is the application system on tongweb? How dead is it?
- Misunderstanding of tongweb due to ease of use
- 微信小程序:全局状态变量的使用
- Leetcode minimum absolute difference of binary search tree simple
猜你喜欢
Tongweb card, tongweb card, tongweb card
HBuilderX:HBuilderX安装以及其常用插件安装
How to view tongweb logs correctly?
微信小程序:基础复习
Sqlplus connection failure
A glimpse of [wechat applet]
Rk3399 hid gadget configuration
Feel the power of shardingsphere JDBC through the demo
arrayList && linkedList
[spark]spark introductory practical series_ 8_ Spark_ Mllib (lower)__ Machine learning library sparkmllib practice
随机推荐
Leetcode- complement of numbers - simple
高迸发解决方案2
MySQL trigger
Leetcode minimum absolute difference of binary search tree simple
HBuilderX:HBuilderX安装以及其常用插件安装
Application virtual directory static resource configuration on tongweb
Turn to 2005
[spark]spark introductory practical series_ 8_ Spark_ Mllib (upper)__ Introduction to machine learning and sparkmllib
安全基线检查脚本—— 筑梦之路
1+1>2,Share Creators可以帮助您实现
php redis 制作高迸发秒杀
Tongweb crawl performance log script
Uni app provincial and urban linkage
How to view tongweb logs correctly?
View绘制整体流程简析
Audio stereo to mono (Audio Dual Channel to mono channel)
Leetcode- maximum average of subarray i- simple
The SQL file of mysql8.0 was imported into version 5.5. There was a pit
Swift property property
微信小程序:基础复习