Android流式布局FlowLayout詳解
現(xiàn)在商城類的APP幾乎都要用到流式布局來實(shí)現(xiàn)選擇屬性功能,在我的demo中是通過FlowLayout工具類實(shí)現(xiàn)流式布局
使用起來非常簡(jiǎn)單,十幾行代碼就可以實(shí)現(xiàn);
在我們的項(xiàng)目中大部分都是單選效果,為了防止用到多選,demo中也實(shí)現(xiàn)了多選;
FlowLayout大家不用研究怎么實(shí)現(xiàn)的,只要會(huì)使用就好;
就好比谷歌提供的ListView條目點(diǎn)擊事件一樣,只要會(huì)用就好,沒必要研究個(gè)所以然;大家在用的時(shí)候直接從demo中復(fù)制到項(xiàng)目中即可;
大家可以將FlowLayout理解為一個(gè)線性布局;將準(zhǔn)備好的一個(gè)個(gè)子view添加到FlowLayout中即可;
首先看下布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <!-- app:horizontal_spacing="8dp" 每列之間的距離 app:vertical_spacing="8dp" 每行之間的距離 --> <www.fl.com.flowlayouttext.view.FlowLayout android:id="@+id/flowlayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:paddingBottom="14dp" android:paddingLeft="21dp" android:paddingRight="14dp" app:horizontal_spacing="8dp" app:vertical_spacing="8dp"/> </LinearLayout>
布局文件非常簡(jiǎn)單,沒什么好說的,下面看下代碼實(shí)現(xiàn):
大致分為三步:
//第一步:初始化FlowLayout flowLayout= (FlowLayout) findViewById(R.id.flowlayout); //第二步:移除FlowLayout中的所有子布局 flowLayout.removeAllViews(); //第三步:循環(huán)創(chuàng)建子View,添加到FlowLayout中 for (int x=0;x<names.length;x++){ //3.1初始化子view CheckBox checkBox= (CheckBox) View.inflate(context,R.layout.item_flowlayout,null); checkBox.setText(names[x]); final int finalX = x; //3.2設(shè)置子view的點(diǎn)擊事件 checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ToastUtil.show(context, names[finalX]); //遍歷FlowLayout中的所有view,如果是當(dāng)前選中的view,設(shè)置為選中狀態(tài),其他設(shè)置為未選中狀態(tài) refreshCheckBox(names[finalX]); } }); //3.3將子view添加到FlowLayout中 flowLayout.addView(checkBox); }
從代碼中可以看到,子view使用的是CheckBox,背景和文字顏色都是使用的狀態(tài)選擇器,大家可以根據(jù)自己項(xiàng)目中的需求隨意更改:
item_flowlayout.xml
<?xml version="1.0" encoding="utf-8"?> <CheckBox xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@null" android:textSize="15sp" android:paddingLeft="5dp" android:paddingRight="5dp" android:background="@drawable/item_bg_select" android:textColor="@drawable/text_color" android:paddingTop="3dp" android:paddingBottom="3dp"> </CheckBox>
單選到此結(jié)束,大家可能感覺多選比單選要難,其實(shí)多選比單選還要簡(jiǎn)單,以上代碼只需去除子view點(diǎn)擊事件refreshCheckBox()方法即可實(shí)現(xiàn)多選;
點(diǎn)擊打開鏈接免費(fèi)下載源碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用CountDownTimer類輕松實(shí)現(xiàn)倒計(jì)時(shí)功能
Android中有個(gè)countDownTimer類,從名字上就可以看出來,它的功能是記錄下載時(shí)間,將后臺(tái)線程的創(chuàng)建和Handler隊(duì)列封裝成為了一個(gè)方便的調(diào)用。2014-07-07使用Android Studio實(shí)現(xiàn)為系統(tǒng)級(jí)的app簽名
這篇文章主要介紹了使用Android Studio實(shí)現(xiàn)為系統(tǒng)級(jí)的app簽名,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03關(guān)于Android中點(diǎn)擊通知欄的通知啟動(dòng)Activity問題解決
這篇文章主要介紹了關(guān)于解決Android中點(diǎn)擊通知欄的通知啟動(dòng)Activity問題的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03【Android 基礎(chǔ)】詳解Animation 動(dòng)畫介紹和實(shí)現(xiàn)
這篇文章主要介紹了【Android 基礎(chǔ)】詳解Animation 動(dòng)畫介紹和實(shí)現(xiàn) ,對(duì)于想要學(xué)習(xí)android開發(fā)的同學(xué)具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12Android仿UC底部菜單欄實(shí)現(xiàn)原理與代碼
最近剛看完ViewPager,開始我打算用自定義的imgBtn,但是發(fā)現(xiàn)放在pager選項(xiàng)卡中不好排版,所以最好選了GridView,接下來介紹底部菜單欄實(shí)現(xiàn)2013-01-01Android自定義View繪圖實(shí)現(xiàn)拖影動(dòng)畫
這篇文章主要介紹了Android自定義View繪圖實(shí)現(xiàn)拖影動(dòng)畫,,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android編程實(shí)現(xiàn)狀態(tài)保存的方法分析
這篇文章主要介紹了Android編程實(shí)現(xiàn)狀態(tài)保存的方法,結(jié)合實(shí)例形式分析了Android狀態(tài)保存的原理、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08基于Viewpager2實(shí)現(xiàn)登錄注冊(cè)引導(dǎo)頁面
這篇文章主要為大家詳細(xì)介紹了基于Viewpager2實(shí)現(xiàn)登錄注冊(cè)引導(dǎo)頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09