FlowLayoutDemo

来源:互联网 发布:外交部流氓回答知乎 编辑:程序博客网 时间:2024/06/06 01:19

添加依赖 compile ‘com.hyman:flowlayout-lib:1.1.2’

MainActivity

public class MainActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final TagFlowLayout tagFlowLayout = findViewById(R.id.id_flowlayout);        Button btn=findViewById(R.id.btn);        final TextView tv=findViewById(R.id.tv);        final List<String>list=new ArrayList<>();        char a='A';        while (a<='Z'){            list.add(String.valueOf(a));            a++;        }        tagFlowLayout.setAdapter(new MyTagAdaper(list));        //事件        tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {            @Override            public boolean onTagClick(View view, int position, FlowLayout parent) {                Toast.makeText(MainActivity.this, list.get(position), Toast.LENGTH_SHORT).show();                return true;            }        });        //点击标签时回调        tagFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {            @Override            public void onSelected(Set<Integer> selectPosSet) {                Toast.makeText(MainActivity.this, selectPosSet.toString(), Toast.LENGTH_SHORT).show();            }        });        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //获取全部选择数据                HashSet<Integer>set= (HashSet<Integer>) tagFlowLayout.getSelectedList();                StringBuilder sb=new StringBuilder();                for (Integer aSet : set) {                    sb.append(list.get(aSet));                    sb.append(",");                }                tv.setText(sb.toString());            }        });    }    class MyTagAdaper extends TagAdapter<String>{        MyTagAdaper(List<String> datas) {            super(datas);        }        @Override        public View getView(FlowLayout parent, int position, String s) {            TextView tv= (TextView) View.inflate(MainActivity.this,R.layout.tv,null);            tv.setText(s);            return tv;        }    }}

activity_main

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:zhy="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.chengqi.my_application.MainActivity"    android:orientation="vertical">    <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>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/btn"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:text="显示所选内容"/>        <TextView            android:id="@+id/tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="15sp"            android:layout_margin="10dp"/>    </LinearLayout></LinearLayout>

tv.xml

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="10dp"    android:background="@drawable/tv_selector"    android:textColor="@drawable/text_color_selector"    android:textSize="14sp"    android:gravity="center"    android:text="@string/text"/>

tv_selector

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:drawable="@drawable/normal_bg"        android:state_checked="true"/>    <item        android:drawable="@drawable/checked_bg"/></selector>

normal_bg

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:drawable="@drawable/normal_bg"        android:state_checked="true"/>    <item        android:drawable="@drawable/checked_bg"/></selector>

checked_bg

<shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="#FF99CC" />    <corners android:radius="10dp"/></shape>

text_color_selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:color="@color/tag_select_textcolor_selected"        android:state_checked="true"/>    <item android:color="@color/tag_select_textcolor"/></selector>

在values/colors.xml文件中添加颜色

<color name="tag_select_textcolor">#FF99CC</color><color name="tag_select_textcolor_selected">#FFFFFF</color>
原创粉丝点击