Why write this library?

Sometimes there will be such requirements in the project, such as the choice of interests and hobbies, search history tags, etc., with the Android control to achieve the thief trouble, so their own yi yi, of course, the premise is that the library developers must use simple and efficient, and the function in place.

What are the characteristics?

1, the most basic function, label layout and automatic wrapping.

2. Customize a variety of styleable, in THE XML layout file can be set tag attributes.

3, you can choose single or multiple options, which are free for developers to choose, as long as you add background.

4. Finally, be simple and efficient.

How to use it?

Yeah, let’s see what it looks like before we use it. Here we go

First, introduce the library

This is GitHub address custom tag layout FloaTagLayout, source project inside, this article will not say the principle of the control, after the source analysis.

Gradle:

The compile 'com. Jetlee: FlowTagLayout: 1.0.1'Copy the code

Maven:

< the dependency > < groupId > com. Jetlee < / groupId > < artifactId > FlowTagLayout < / artifactId > < version > 1.0.1 < / version > <type>pom</type> </dependency>Copy the code

Once you have the library dependent on your project, use it in your layout and configure various properties

<com.jet.flowtaglayout.FlowTagLayout android:id="@+id/flowTagLayout" android:layout_width="wrap_content" Android :layout_height="wrap_content" app:item_leftMargin="8dp" app:item_rightMargin=" 8DP "// Right margin of tag App :item_bottomMargin="8dp" app:item_bottomMargin=" 8DP "app:item_leftPadding=" 12DP" app:item_topMargin=" 8DP "// Top margin of the tag app:item_bottomMargin=" 8DP" // Bottom margin of the tag app:item_leftPadding=" 12DP "// Left inner margin of the tag App :item_rightPadding=" 12DP "app:item_topPadding=" 6DP" app:item_bottomPadding=" 6DP "app:item_rightPadding=" 12DP" app:item_topPadding=" 6DP "app:item_bottomPadding=" 6DP App :item_background="@drawable/ripple_gray" app:item_textColor="#F25A25" // Tag text color App :item_textSize="16sp"Copy the code

The following are some of the most important methods for FlowTagLayout (note that these methods can be shared or used solely depending on your needs)

Prepare several pieces of data, all of which are labels of tags

List<String> dataList = new ArrayList<>(); DataList. Add (" data structure "); DataList. Add (" algorithm "); dataList.add("Java"); DataList. Add (" Multithreaded programming "); // Add tag flowTagLayout.addTags(dataList); Flowtaglayout.addtag ("Kotlin"); // Add tags to the list. / / add a tag in the tail flowTagLayout. AddTagOfIndex (2, "custom view"); Flowtaglayout.removetag (); flowTagLayout.removeTag(); flowTagLayout.removeTag(); / / remove the tail tag flowTagLayout. RemoveTagOfIndex (3); // Remove the tag from the specified position, for example, removing index 3, which is the fourth tag to be removedCopy the code

Finally, bind click events

flowTagLayout.setTagClickListener(new FlowTagLayout.OnTagClickListener() { @Override public void tagClick(int position) Flowtaglayout.getchildat (position).setSelected(! flowTagLayout.getChildAt(position).isSelected()); }});Copy the code

The use of this library is so simple, like to give a star bai, ha ha ha.