Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use TagFlowLayout to add and delete tags dynamically in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to use TagFlowLayout to dynamically add and delete tags in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Effect picture

Simple rendering (using open source libraries) [FlowLayout] ("https://github.com/hongyangAndroid/FlowLayout")

Steps

Guide package

Compile 'com.zhy:flowlayout-lib:1.0.3'

Initialization data

Private String [] mVals = new String [] {"Hello", "Android", "Weclome Hi", "Button", "TextView", "Hello", "Android", "Weclome", "Button ImageView", "TextView", "Helloworld"}; arrTab = new ArrayList (); / / add a replacement arrTab.add ("tab") for tagging

Set the adapter of TagFlowLayout

/ / set adapter adapter = new TagAdapter (arrTab) {@ Override public View getView (FlowLayout parent, final int position, String s) {System.out.println (position + "- -" + (arrTab.size ()-1) If (position = = arrTab.size ()-1) {/ / set the last tagged layout LinearLayout iv = (LinearLayout) LayoutInflater.from (SecondActivity.this) .inflate (R.layout.iv, mFlowLayout,false) / / if you click, add the element and refresh adapter iv.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {int I = arrTab.size ()-1; / / add the newly added data to the last location of the collection, and the original add icon will go to the + 1 location arrTab.add (I, mVals [position]). Adapter.notifyDataChanged ();}}); return iv;} else {/ / normal label layout 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 ("I clicked is" + position); adapter.notifyDataChanged ();}}); return tv MFlowLayout.setAdapter (adapter)

Various layout files

Iv tv "how to use TagFlowLayout to dynamically add and remove tags in Android" is introduced here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report