How to use TagFlowLayout to add and delete tags dynamically in Android
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!