本文所使用的FlowLayout來自于鴻洋大神的框架牺丙。
正好現(xiàn)在在做項(xiàng)目需要用到。自己試著用了下抒抬,具體可以看效果圖
鴻洋的github:https://github.com/hongyangAndroid/FlowLayout
android Studio中注入依賴:
compile 'com.zhy:flowlayout-lib:1.0.3'
布局文件:
zhy:max_select=設(shè)置可選中的tag數(shù)量杨刨。
zhy:max_select="-1" 不限制。
zhy:max_select="5"限制可選中5個(gè)標(biāo)簽擦剑。適合文章發(fā)布選擇標(biāo)簽開發(fā)妖胀。
auto_select_effect="true | false"設(shè)置點(diǎn)擊效果可免,默認(rèn)顯示
<com.zhy.view.flowlayout.TagFlowLayout
android:layout_marginTop="@dimen/dp_15"
android:id="@+id/search_page_flowlayout"
zhy:max_select="-1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
標(biāo)簽TextView及風(fēng)格的設(shè)置:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flowlayout_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/flowlayout_shap"
android:text="Helloworld"
android:textColor="@color/black">
</TextView>
這里TextView用到了shap風(fēng)格:
color 選中標(biāo)簽顏色
blackground | drawable 已選中背景。這里我引用了一個(gè)shape風(fēng)格
state_checked 是否顯示選中狀態(tài)效果做粤。
blackground | drawable 未選中背景浇借。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/theme"
android:drawable="@drawable/button_shape"
android:state_checked="true"></item>
<item android:drawable="@drawable/bg_black_shape"></item>
</selector>
數(shù)據(jù)源:
final LayoutInflater mInflater = LayoutInflater.from(SearchPageActivity.this);
search_page_flowlayout.setAdapter(new TagAdapter<String>(mVals)
{
@Override
public View getView(FlowLayout parent, int position, String s)
{
TextView tv = (TextView) mInflater.inflate(R.layout.search_page_flowlayout_tv,
search_page_flowlayout, false);
tv.setText(s);
return tv;
}
});
監(jiān)聽標(biāo)簽點(diǎn)擊事件:
search_page_flowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()
{
@Override
public boolean onTagClick(View view, int position, FlowLayout parent)
{
Toast.makeText(SearchPageActivity.this, mVals[position], Toast.LENGTH_SHORT).show();
//view.setVisibility(View.GONE);
return true;
}
});
設(shè)置監(jiān)聽事件的回調(diào):
search_page_flowlayout.setOnSelectListener(new TagFlowLayout.OnSelectListener()
{
@Override
public void onSelected(Set<Integer> selectPosSet)
{
SearchPageActivity.this.setTitle("choose:" + selectPosSet.toString());
}
});
新手上路!歡迎指導(dǎo)怕品!
以后經(jīng)常會(huì)在簡(jiǎn)書妇垢,csdn更新自己開發(fā)過程中遇到的問題,和一些好用的框架肉康。
包括一些互聯(lián)網(wǎng)的經(jīng)驗(yàn)分享闯估,歡迎同行交流
源碼:https://github.com/wapchief/android-CollectionDemo