当前位置:网站首页>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();
}
};
}
}边栏推荐
- You still can't remotely debug idea? Come and have a look at my article. It's easy to use
- Leetcode- reverse vowels in string - simple
- USB debugging assistant
- Leetcode Hamming distance simple
- Minimum spanning tree (prim+kruskal) learning notes (template +oj topic)
- Sqlplus connection failure
- How to view APK version number from apk
- Time conversion'2015-04-20t11:12:00.000+0000 '
- Echart折线图:多条折线图每次仅展示一条
- Feel the power of shardingsphere JDBC through the demo
猜你喜欢

The Boys x PUBGMOBILE 联动火热来袭!来看最新游戏海报

软件测试——接口常见问题汇总

USB debugging assistant

Zero copy technology

Tongweb card, tongweb card, tongweb card

中断处理过程
![[to]12 common IP commands in the iproute installation package](/img/65/a214d137e230b1a1190feb03660f2c.jpg)
[to]12 common IP commands in the iproute installation package

Application virtual directory static resource configuration on tongweb

USB debugging assistant (20191028)

arrayList && linkedList
随机推荐
Echart柱状图:echart实现堆叠柱状图
Timeout thread log for tongweb
The problem of distinguishing and sharing sessions for multiple applications in tongweb
Tongweb crawl performance log script
Leetcode longest harmonic subsequence simple
Leetcode judge subsequence simple
Leetcode- maximum average of subarray i- simple
【美团笔试题】
Echart矩形树图:简单实现矩形树图
Security baseline check script - the road to dream
How slow is the application system on tongweb? How dead is it?
What happens when the MySQL union index ABC encounters a "comparison operator"?
Multiple reception occurs in the uniapp message delivery
Leetcode- string addition - simple
自定义View
JNDI configuration for tongweb7
Leetcode- number of words in string - simple
Experience of redis installation under Linux system (an error is reported at the same time. The struct redis server does not have a member named XXXX)
1+1>2,Share Creators可以帮助您实现
Introduction to USB learning (I) -- Dongfeng night flower tree