FlowLayout
現(xiàn)在項目中為了美觀路鹰,設(shè)計師們都比較喜歡流式布局(FlowLayout)炎功,秉著不重復(fù)造輪子的原則,在網(wǎng)上搜索了一堆座泳,后來還是根據(jù)名氣選擇了鴻陽的??惠昔。
廢話不多說,下面是我做的Demo示例圖片:
屏幕快照 2019-08-27 下午4.38.12.png
1挑势、使用鴻陽大神的代碼首先要引入依賴:
implementation 'com.hyman:flowlayout-lib:1.1.2'
2镇防、剩下就是在代碼中使用啦
1??、布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:zhy="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.activity.FlowLayoutActivity">
<com.zhy.view.flowlayout.TagFlowLayout
android:id="@+id/tab_flowlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
zhy:max_select="-1"></com.zhy.view.flowlayout.TagFlowLayout>
<Button
android:id="@+id/btn_count"
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
2??潮饱、項目中使用来氧,寫了兩個方法:
/**
* 設(shè)置選擇事件
*/
private void setFlowItemListener() {
//選擇監(jiān)聽
tabFlowlayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
@Override
public void onSelected(Set<Integer> selectPosSet) {
Toast.makeText(FlowLayoutActivity.this, "chose" + selectPosSet.toString(), Toast.LENGTH_SHORT).show();
}
});
//標(biāo)簽點擊監(jiān)聽
tabFlowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
@Override
public boolean onTagClick(View view, int position, FlowLayout parent) {
Toast.makeText(FlowLayoutActivity.this, "標(biāo)簽" + tags.get(position), Toast.LENGTH_SHORT).show();
return true;
}
});
}
/**
* 給流式布局設(shè)置值
*/
private void setFlowData() {
tabFlowlayout.setAdapter(new TagAdapter<String>(tags) {
@Override
public View getView(FlowLayout parent, int position, String o) {
LayoutInflater inflater = LayoutInflater.from(getApplication());
TextView textView = (TextView) inflater.inflate(R.layout.tv_layout, tabFlowlayout, false);
textView.setText(o);
textView.setBackground(getDrawable(R.drawable.textview_nomal_shape));
return textView;
}
//當(dāng)選中的時候
@Override
public void onSelected(int position, View view) {
super.onSelected(position, view);
TextView textView = (TextView) view;
textView.setBackground(getDrawable(R.drawable.textview_shape));
textView.setTextColor(Color.parseColor("#FFFFFF"));
}
//當(dāng)沒選中的時候
@Override
public void unSelected(int position, View view) {
super.unSelected(position, view);
TextView textView = (TextView) view;
textView.setBackground(getDrawable(R.drawable.textview_nomal_shape));
textView.setTextColor(Color.parseColor("#000000"));
}
});
}
3、使用的時候我遇到了一個問題香拉,就是當(dāng)我給TextView設(shè)置選擇器的時候啦扬,發(fā)現(xiàn)只有邊框顏色變化,背景沒有變化凫碌。造成這種問題的原因是我Item布局父布局是LinearLayout 里面嵌套一個TextView扑毡,改成Item布局直接是TextView這個問題就解決啦,(分析的原因是盛险,這樣寫的目的是瞄摊,我們代碼設(shè)置選中監(jiān)聽的時候,方法中有一個view如果這個view不是TextView我們就不能設(shè)置字體顏色啦苦掘,這樣就局限啦换帜,有可能分析的不對,只是給自己找了個理由而已??)可以在第二個方法中隨便設(shè)置背景色啦鸟蜡,而且還能給條目字體設(shè)置顏色膜赃。
4、有現(xiàn)成的API可以直接獲取我們選中條目的索引值揉忘,代碼如下,查看了數(shù)據(jù)端铛,數(shù)據(jù)準(zhǔn)確泣矛。
List<Integer> list = new ArrayList<>();
@OnClick(R.id.btn_count)
public void onViewClicked() {
list.clear();
Set<Integer> selectedList = tabFlowlayout.getSelectedList();
Iterator<Integer> iterator = selectedList.iterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
list.add(next);
}
Toast.makeText(this, "被選中的條目數(shù)量是:" + list.size(), Toast.LENGTH_SHORT).show();
}
5、以上就是使用流程禾蚕,自學(xué)完畢您朽。