dependencies { compile 'com.hyman:flowlayout-lib:1.1.2' }
maven{ url 'https://maven.aliyun.com/repository/google'}
maven{ url 'https://maven.aliyun.com/repository/jcenter'}
<com.zhy.view.flowlayout.TagFlowLayout android:id="@+id/id_flowlayout" zhy:max_select="-1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="20dp"> </com.zhy.view.flowlayout.TagFlowLayout>
mFlowLayout.setAdapter(new TagAdapter<String>(mVals) { @Override public View getView(FlowLayout parent, int position, String s) { TextView tv = (TextView) mInflater.inflate(R.layout.tv, mFlowLayout, false); tv.setText(s); return tv; } });
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/tag_select_textcolor" android:drawable="@drawable/checked_bg" android:state_checked="true"></item> <item android:drawable="@drawable/normal_bg"></item> </selector>
#Adapter @Override public void onSelected(int position, View view) { super.onSelected(position, view); } @Override public void unSelected(int position, View view) { super.unSelected(position, view); }
mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() { @Override public boolean onTagClick(View view, int position, FlowLayout parent) { Toast.makeText(getActivity(), mVals[position], Toast.LENGTH_SHORT).show(); return true; } });
mFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() { @Override public void onSelected(Set<Integer> selectPosSet) { getActivity().setTitle("choose:" + selectPosSet.toString()); } });
//预先设置选中 mAdapter.setSelectedList(1,3,5,7,8,9); //获得所有选中的pos集合 flowLayout.getSelectedList();