612cb0c4f1fe8dce842f3a53160c11cf74c627aa.svn-base 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.hjq.widget;
  2. import android.content.Context;
  3. import android.os.Build;
  4. import android.support.annotation.RequiresApi;
  5. import android.util.AttributeSet;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. /**
  9. * desc : 简单的 Layout(常用于自定义组合控件继承的基类,可以起到性能优化的作用)
  10. */
  11. public class SimpleLayout extends ViewGroup {
  12. public SimpleLayout(Context context) {
  13. super(context);
  14. }
  15. public SimpleLayout(Context context, AttributeSet attrs) {
  16. super(context, attrs);
  17. }
  18. public SimpleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
  19. super(context, attrs, defStyleAttr);
  20. }
  21. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  22. public SimpleLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  23. super(context, attrs, defStyleAttr, defStyleRes);
  24. }
  25. @Override
  26. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  27. int maxHeight = 0;
  28. int maxWidth = 0;
  29. int childState = 0;
  30. // 测量子 View
  31. for (int i = 0; i < getChildCount(); i++) {
  32. final View child = getChildAt(i);
  33. // 被测量的子 View 不能是隐藏的
  34. if (child.getVisibility() != GONE) {
  35. measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
  36. final MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
  37. maxWidth = Math.max(maxWidth, child.getMeasuredWidth() + params.leftMargin + params.rightMargin);
  38. maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + params.topMargin + params.bottomMargin);
  39. childState = combineMeasuredStates(childState, child.getMeasuredState());
  40. }
  41. }
  42. maxWidth += getPaddingLeft() + getPaddingRight();
  43. maxHeight += getPaddingTop() + getPaddingBottom();
  44. maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
  45. maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
  46. // 测量自身
  47. setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
  48. resolveSizeAndState(maxHeight, heightMeasureSpec,
  49. childState << MEASURED_HEIGHT_STATE_SHIFT));
  50. }
  51. @Override
  52. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  53. // 遍历子 View
  54. for (int i = 0; i < getChildCount(); i++) {
  55. final View child = getChildAt(i);
  56. final MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
  57. // 将子 View 放置到左上角的位置
  58. child.layout(getPaddingLeft() + params.leftMargin,
  59. getPaddingTop() + params.topMargin,
  60. getPaddingRight() + child.getMeasuredWidth() + params.rightMargin,
  61. getPaddingBottom() + child.getMeasuredHeight() + params.bottomMargin);
  62. }
  63. }
  64. @Override
  65. public LayoutParams generateLayoutParams(AttributeSet attrs) {
  66. return new MarginLayoutParams(getContext(), attrs);
  67. }
  68. @Override
  69. protected LayoutParams generateDefaultLayoutParams() {
  70. return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
  71. }
  72. @Override
  73. protected LayoutParams generateLayoutParams(LayoutParams p) {
  74. return new MarginLayoutParams(p);
  75. }
  76. }