Android開發(fā)之瀑布流控件的實(shí)現(xiàn)與使用方法示例
本文實(shí)例講述了Android開發(fā)之瀑布流控件的實(shí)現(xiàn)與使用方法。分享給大家供大家參考,具體如下:
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);
/**創(chuàng)建行的集合*/
private List<Line> mLines = new ArrayList<Line>();
/**當(dāng)前行*/
private Line mCurrentLine;
/**當(dāng)前行使用的寬度*/
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);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//0.先清空行集合里的數(shù)據(jù)
clear();
//1.得到父viewGroup的模式與大小
int parentWidthMode = MeasureSpec.getMode(widthMeasureSpec);//
parentWidthSize = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
int parentHeightMode = MeasureSpec.getMode(heightMeasureSpec);
parentHeightSize = MeasureSpec.getSize(heightMeasureSpec) - getPaddingBottom() - getPaddingTop();
/* 每個(gè)子view都是包裹內(nèi)容
* layout.addView(mTextView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT
* 得到每個(gè)孩子的測量規(guī)則
*/
//2.得到每個(gè)孩子的模式
int childWidthMode = parentWidthMode == MeasureSpec.EXACTLY ? MeasureSpec.EXACTLY : parentWidthMode;
int childHeightMode = parentHeightMode == MeasureSpec.EXACTLY ? MeasureSpec.EXACTLY : parentHeightMode;
//3.根據(jù)模式得到子控件的大小
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthMode, parentWidthSize);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightMode, parentHeightSize);
//得到子view的個(gè)數(shù)
int count = getChildCount();
//創(chuàng)建新的行
mCurrentLine = new Line();
for (int i = 0; i < count; i++) {
View childView = getChildAt(i);
//4.測量每個(gè)孩子
childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
//5.得到測量后的孩子的寬高
int childMeasureWidth = MeasureSpec.getSize(childWidthMeasureSpec);
//int childMeasureHeight = MeasureSpec.getSize(childHeightMeasureSpec);
//6.得到此行使用的寬度
mCurrentUseWidth += childMeasureWidth;
//7.判斷此行的寬度是否大于父控件的寬度,如果大于則換行
if (mCurrentUseWidth > parentWidthSize) {
//8.如果當(dāng)前的子view的寬度大于父容器的寬度,強(qiáng)行把這個(gè)view添加的集合里
if (mCurrentLine.getChildCount()<1) {
mLines.add(mCurrentLine);
}
//9.換行
newLine();
}else {
//8.把當(dāng)前子view添加到行里
mCurrentLine.addChild(childView);
//9.添加間隔
mCurrentUseWidth += mHorizontolSpace;
if (mCurrentUseWidth > parentWidthSize) {
//10.換行
newLine();
}
}
}
//11.如果集合里沒有添加當(dāng)前行,則把當(dāng)前添加到集合
if (!mLines.contains(mCurrentLine)) {
mLines.add(mCurrentLine);
}
//12.設(shè)置富容器的總寬高
int parentWidth = parentWidthSize + getPaddingLeft() + getPaddingRight();
int parentHeight = (mLines.size()-1) * mVerticalSpace + getPaddingBottom() + getPaddingTop();
for(Line line : mLines){
//得到所以line的高度
parentHeight += line.getHeight();
}
//13.resolveSize表示哪個(gè)高度合適,就用哪個(gè)
setMeasuredDimension(parentWidth, resolveSize(parentHeightSize, parentHeight));
/*setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));*/
}
/**
* 換行
*/
private void newLine() {
//a.先把當(dāng)前的行添加到集合
mLines.add(mCurrentLine);
//b.創(chuàng)建新的一行
mCurrentLine = new Line();
//c.新行里的使用的行必須設(shè)置為0
mCurrentUseWidth = 0;
}
public void clear() {
mLines.clear();
mCurrentLine = null;
mCurrentUseWidth = 0;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//15.得到每個(gè)line孩子的左上角的坐標(biāo)
int left = l + getPaddingLeft();
int top = t + getPaddingTop();
//現(xiàn)在容器里只有l(wèi)ine是子孩子
for (int i = 0; i < mLines.size(); i++) {
Line line = mLines.get(i);
//16.把分配位置給line去處理
line.layout(left, top);
//17.設(shè)置第一行后的其它行的top數(shù)值
top += line.getHeight() + mVerticalSpace;
}
}
/**
* 行類,用來封裝一行的view
*/
private class Line{
/**當(dāng)前行的寬度*/
private int mWidth = 0;
/**當(dāng)前行的高度*/
private int mHeight = 0;
/**每個(gè)孩子得到的剩余空間*/
int mChildPdding = 0;
private List<View> children = new ArrayList<View>();
public void addChild(View childView) {
children.add(childView);
//取得之view里最高的高度
if (childView.getMeasuredHeight() > mHeight) {
mHeight = childView.getMeasuredHeight();
}
//18.得到行寬度
mWidth += childView.getMeasuredWidth();
}
/**
* 定位每個(gè)line在富容器里的額位置
* @param left
* @param top
*/
public void layout(int left, int top) {
//18.得到行寬度
mWidth += mHorizontolSpace * (children.size() -1);
//19.得到剩余的寬度大小
//int padding = getMeasuredWidth() - mWidth;
int padding = parentWidthSize - mWidth;
if (padding > 0) {
mChildPdding = padding / children.size();
}
// getWidth()view顯示的時(shí)候大小,如果view沒顯示,這個(gè)值就為0,步包括隱藏的部分, getMeasuredWidth()控件實(shí)際大小,包括隱藏的部分
//一般來說 getMeasuredWidth() > getWidth();
for (int i = 0; i < children.size(); i++) {
View child = children.get(i);
//第一種:有間隔的flow
int bottom = child.getMeasuredHeight() + top;
//20.把剩余的空間分配給每個(gè)view
int right = child.getMeasuredWidth() + left + mChildPdding;
//第二種:無間隔的flow
// int bottom = getMeasuredHeight() + top;
// int right = getMeasuredWidth() + left;
//第一個(gè)child的位置
child.layout(left, top, right, bottom);
//第二個(gè)及后面child的right
right += child.getMeasuredWidth() + mHorizontolSpace + mChildPdding;
}
}
/**
* 得到子view的大小
* @return
*/
public int getChildCount() {
if (children != null) {
return children.size();
}
return 0;
}
public int getHeight() {
return mHeight;
}
}
}
使用方法:
public class TopFragment extends Fragment{
@Override
public 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 < mDatas.size(); i++) {
mTextView = 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 {
/**
* 創(chuàng)建隨機(jī)背景的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;
}
/**
* 創(chuàng)建帶有背景的drawable
* @return
*/
public static GradientDrawable createDrawable(int color){
GradientDrawable drawable = new GradientDrawable();
drawable.setCornerRadius(Util.px2dip(5));
drawable.setColor(color);
return drawable;
}
/**
* 狀態(tài)選擇器
* @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;
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android窗口相關(guān)操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android瀑布流照片墻實(shí)現(xiàn) 體驗(yàn)不規(guī)則排列的美感
- Android App中實(shí)現(xiàn)相冊瀑布流展示的實(shí)例分享
- Android RecyclerView詳解之實(shí)現(xiàn) ListView GridView瀑布流效果
- android中UIColletionView瀑布流布局實(shí)現(xiàn)思路以及封裝的實(shí)現(xiàn)
- android控件封裝 自己封裝的dialog控件
- android 自定義控件 自定義屬性詳細(xì)介紹
- Android中Spinner(下拉框)控件的使用詳解
- Android下拉刷新上拉加載控件(適用于所有View)
- Android控件系列之TextView使用介紹
- android ListView和ProgressBar(進(jìn)度條控件)的使用方法
- Android控件之ListView用法實(shí)例詳解
相關(guān)文章
Android AutoCompleteTextView自動(dòng)提示文本框?qū)嵗a
這篇文章主要介紹了Android AutoCompleteTextView自動(dòng)提示文本框?qū)嵗a的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android學(xué)習(xí)教程之九宮格圖片展示(13)
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)教程之九宮格圖片展示代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
詳解Android中Handler的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Android中Handler的實(shí)現(xiàn)原理,本文深入分析 Android 的消息處理機(jī)制,了解 Handler 的工作原理,感興趣的小伙伴們可以參考一下2016-04-04
輕松實(shí)現(xiàn)可擴(kuò)展自定義的Android滾輪時(shí)間選擇控件
這篇文章主要為大家詳細(xì)介紹了可擴(kuò)展自定義的Android滾輪時(shí)間選擇控件,結(jié)合WheelView實(shí)現(xiàn)滾輪選擇日期操作,感興趣的小伙伴們可以參考一下2016-07-07
基于Android實(shí)現(xiàn)的文件同步設(shè)計(jì)方案
隨著用戶對自身數(shù)據(jù)保護(hù)意識(shí)的加強(qiáng),讓用戶自己維護(hù)自己的數(shù)據(jù)也成了獨(dú)立開發(fā)產(chǎn)品時(shí)的一個(gè)賣點(diǎn),若只針對少量的文件進(jìn)行同步,則實(shí)現(xiàn)起來比較簡單,當(dāng)針對一個(gè)多層級目錄同步時(shí),情況就復(fù)雜多了,本文我分享下我的設(shè)計(jì)思路2023-10-10
Google 開發(fā)Android MVP架構(gòu)Demo深入解析
這篇文章主要為大家介紹了Google 開發(fā)Android MVP架構(gòu)Demo深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

