Android中使用TagFlowLayout制作动态添加删除标签
程序员文章站
2022-05-28 10:21:44
效果图
简单的效果图(使用开源库)[flowlayout](“ https://github.com/hongyangandroid/flowlayout “)
步...
效果图
简单的效果图(使用开源库)[flowlayout](“ https://github.com/hongyangandroid/flowlayout “)
步骤
导包
compile 'com.zhy:flowlayout-lib:1.0.3' <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>
初始化数据
private string[] mvals = new string[] {"hello", "android", "weclome hi ", "button", "textview", "hello", "android", "weclome", "button imageview", "textview", "helloworld" }; arrtab = new arraylist(); //添加一条数据用于添加标签的替换 arrtab.add("tab");
设置tagflowlayout的adapter
//设置adapter adapter = new tagadapter<string>(arrtab){ @override public view getview(flowlayout parent, final int position, string s) { system.out.println(position + "---" + (arrtab.size() - 1)); if (position == arrtab.size() - 1){ //设置最后一个添加标签的布局 linearlayout iv = (linearlayout) layoutinflater.from(secondactivity.this).inflate( r.layout.iv, mflowlayout,false); //如果点击就添加元素并刷新adapter iv.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int i = arrtab.size() - 1; //将新加入的数据加到集合的最后一个位置,而原来的添加图标会到 +1 的位置 arrtab.add(i , mvals[position]); adapter.notifydatachanged(); } }); return iv ; }else{ //正常标签的布局 relativelayout tv = (relativelayout) layoutinflater.from(secondactivity.this).inflate( r.layout.tv, mflowlayout,false); textview viewbyid = (textview) tv.findviewbyid(r.id.tv_name); viewbyid.settext(s); imageview imageview = (imageview) tv.findviewbyid(r.id.iv_delete); imageview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { arrtab.remove(position); system.out.println("我点击的是" + position); adapter.notifydatachanged(); } }); return tv; } } }; mflowlayout.setadapter(adapter);
各种布局文件
iv <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="88dp" android:paddingtop="5dp" android:layout_height="40dp"> <imageview android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bacs_btn_addbq" /> </linearlayout> tv <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="88dp" android:layout_height="wrap_content"> <!--android:background="@drawable/tag_bg"--> <textview android:id="@+id/tv_name" android:layout_margintop="7dp" android:layout_width="80dp" android:gravity="center" android:layout_height="32dp" android:background="@drawable/bg_tv" android:text="我是唯爱" android:textsize="13sp" android:textcolor="#fff"> </textview> <imageview android:id="@+id/iv_delete" android:layout_alignparentright="true" android:layout_width="15dp" android:layout_height="15dp" android:src="@drawable/sm_ico_sounddel" /> </relativelayout>
以上所述是小编给大家介绍的android中使用tagflowlayout制作动态添加删除标签,希望对大家有所帮助