Android中使用TagFlowLayout制作動態(tài)添加刪除標簽
效果圖
簡單的效果圖(使用開源庫)[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ù)用于添加標簽的替換
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è)置最后一個添加標簽的布局
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;
//將新加入的數(shù)據(jù)加到集合的最后一個位置,而原來的添加圖標會到 +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制作動態(tài)添加刪除標簽,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android 用Time和Calendar獲取系統(tǒng)當前時間源碼分享(年月日時分秒周幾)
這篇文章主要介紹了Android 用Time和Calendar獲取系統(tǒng)當前時間源碼分享,包括年月日時分秒周幾的源碼,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01
在android中實現(xiàn)類似uc和墨跡天氣的左右拖動效果
本文主要介紹下怎樣在android實現(xiàn)uc和墨跡天氣那樣的左右拖動效果,具體代碼如下,感興趣的朋友可以參考下哈2013-06-06
Android快速開發(fā)之定制BaseTemplate
這篇文章主要為大家詳細介紹了Android快速開發(fā)之定制BaseTemplate的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android應(yīng)用開發(fā)中數(shù)據(jù)的保存方式總結(jié)
這篇文章主要介紹了Android應(yīng)用開發(fā)中數(shù)據(jù)的保存方式總結(jié),包括對ROM、SD卡、SharedPreference這三種方式實現(xiàn)的核心代碼的精選,需要的朋友可以參考下2016-02-02
Flutter 實現(xiàn)網(wǎng)易云音樂字幕的代碼
這篇文章主要介紹了Flutter 實現(xiàn)網(wǎng)易云音樂字幕的代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Android中在GridView網(wǎng)格視圖上實現(xiàn)item拖拽交換的方法
這篇文章主要介紹了Android中在GridView上實現(xiàn)item拖拽交換效果的方法,比起ListView的列表條目交換稍顯復(fù)雜,文中先介紹了關(guān)于創(chuàng)建GridView的一些基礎(chǔ)知識,需要的朋友可以參考下2016-04-04
Android使用NumberPicker實現(xiàn)滑輪日期選擇器
這篇文章主要為大家介紹了如何使用Android中的NumberPicker控件,以一種簡單而直觀的方式實現(xiàn)滑輪式的日期選擇器,需要的小伙伴可以參考一下2023-06-06


