這篇文章給大家分享的是有關(guān)Android中如何使用TagFlowLayout制作動(dòng)態(tài)添加刪除標(biāo)簽的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
效果圖
簡單的效果圖(使用開源庫)[FlowLayout](“ https://github.com/hongyangAndroid/FlowLayout “)
步驟
導(dǎo)包
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>
初始化數(shù)據(jù)
private String[] mVals = new String[] {"Hello", "Android", "Weclome Hi ", "Button", "TextView", "Hello", "Android", "Weclome", "Button ImageView", "TextView", "Helloworld" }; arrTab = new ArrayList(); //添加一條數(shù)據(jù)用于添加標(biāo)簽的替換 arrTab.add("tab");
設(shè)置TagFlowLayout的adapter
//設(shè)置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){ //設(shè)置最后一個(gè)添加標(biāo)簽的布局 LinearLayout iv = (LinearLayout) LayoutInflater.from(SecondActivity.this).inflate( R.layout.iv, mFlowLayout,false); //如果點(diǎn)擊就添加元素并刷新adapter iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int i = arrTab.size() - 1; //將新加入的數(shù)據(jù)加到集合的最后一個(gè)位置,而原來的添加圖標(biāo)會到 +1 的位置 arrTab.add(i , mVals[position]); adapter.notifyDataChanged(); } }); return iv ; }else{ //正常標(biāo)簽的布局 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("我點(diǎn)擊的是" + 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>
感謝各位的閱讀!關(guān)于“Android中如何使用TagFlowLayout制作動(dòng)態(tài)添加刪除標(biāo)簽”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
網(wǎng)頁題目:Android中如何使用TagFlowLayout制作動(dòng)態(tài)添加刪除標(biāo)簽-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://chinadenli.net/article8/jjeop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、響應(yīng)式網(wǎng)站、品牌網(wǎng)站建設(shè)、云服務(wù)器、企業(yè)建站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容