**
Android 瀑布流控件的实现
**
—————————————————–
(java 架构师全套教程,共760G, 让你从零到架构师,每月轻松拿3万)
请先拍 购买地址, 下载请用百度盘
目录如下:
01.高级架构师四十二个阶段高
02.Java高级系统培训架构课程148课时
03.Java高级互联网架构师课程
04.Java互联网架构Netty、Nio、Mina等-视频教程
05.Java高级架构设计2016整理-视频教程
06.架构师基础、高级片
07.Java架构师必修linux运维系列课程
08.Java高级系统培训架构课程116课时
(送:hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门)
01高级架构师四十二个阶段高内容:
01高级架构师四十二个阶段高内容:
—————————————————–
public class FlowLayout extends ViewGroup {/**行里子view之间的行距离*/public int mHorizontolSpace = Util.getDimen(R.dimen.top_padding);/**行里子view之间的垂直距离*/public int mVerticalSpace = Util.getDimen(R.dimen.top_padding);/**创建行的集合*/private List mLines = new ArrayList();/**当前行*/private Line mCurrentLine;/**当前行使用的宽度*/private int mCurrentUseWidth = 0;/**父容器的宽高*/private int parentWidthSize;private int parentHeightSize;public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public FlowLayout(Context context, AttributeSet attrs) {super(context, attrs);}public FlowLayout(Context context) {super(context);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {clear();int parentWidthMode = MeasureSpec.getMode(widthMeasureSpec);parentWidthSize = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();int parentHeightMode = MeasureSpec.getMode(heightMeasureSpec);parentHeightSize = MeasureSpec.getSize(heightMeasureSpec) - getPaddingBottom() - getPaddingTop();int childWidthMode = parentWidthMode == MeasureSpec.EXACTLY ? MeasureSpec.EXACTLY : parentWidthMode;int childHeightMode = parentHeightMode == MeasureSpec.EXACTLY ? MeasureSpec.EXACTLY : parentHeightMode;int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthMode, parentWidthSize);int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightMode, parentHeightSize);int count = getChildCount();mCurrentLine = new Line();for (int i = 0; i
使用方法:
public class TopFragment extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {ScrollView scrollView = new ScrollView(getActivity());FlowLayout layout = new FlowLayout(getActivity());layout.setBackgroundDrawable(Util.getDrawable(R.drawable.list_item_bg));int padding = Util.getDimen(R.dimen.top_padding);layout.setPadding(padding, padding, padding, padding);GradientDrawable pressDrawable = DrawableUtil.createDrawable(0xffcecece);for (int i = 0; i new TextView(getActivity());mTextView.setText(mDatas.get(i));GradientDrawable randomDrawable = DrawableUtil.createRandomDrawable();StateListDrawable stateListDrawable = DrawableUtil.createStateDrawable(pressDrawable, randomDrawable);mTextView.setBackgroundDrawable(stateListDrawable);mTextView.setTextColor(Color.WHITE);int left = Util.px2dip(7);int top = Util.px2dip(4);int right = Util.px2dip(7);int bottom = Util.px2dip(4);mTextView.setPadding(left, top, right, bottom);mTextView.setTag(mDatas.get(i));mTextView.setOnClickListener(this);layout.addView(mTextView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, - 2));}scrollView.addView(layout);}return scrollView;
}
工具类:
public class DrawableUtil {/*** 创建随机背景的drawable* @return*/public static GradientDrawable createRandomDrawable(){GradientDrawable drawable = new GradientDrawable();drawable.setCornerRadius(Util.px2dip(5));Random random = new Random();int red = random.nextInt(200) + 20;int green = random.nextInt(200) + 20;int blue = random.nextInt(200) + 20;int color = Color.rgb(red, green, blue);drawable.setColor(color);return drawable;}/*** 创建带有背景的drawable* @return*/public static GradientDrawable createDrawable(int color){GradientDrawable drawable = new GradientDrawable();drawable.setCornerRadius(Util.px2dip(5));drawable.setColor(color);return drawable;}/*** 状态选择器* @param press* @param normal* @return*/public static StateListDrawable createStateDrawable(Drawable press, Drawable normal){StateListDrawable drawable = new StateListDrawable();drawable.addState(new int[]{android.R.attr.state_pressed}, press);drawable.addState(new int[]{}, normal);return drawable;}
}