class=”htmledit_views”>
Alas, the title is so tiring to write, it may not be able to express clearly after such a long time. There is really no way, this custom layout is relatively highly customized and has many features, because it was an app for email a while ago, and I saw such a layout when referring to QQ mailbox, I thought it was cool, and I wanted to realize it. First paste the renderings to be more intuitive:
It should be very clear, flow layout, you can add a title in front (removal is also supported), and there is an input box at the end by default (you can also set it without), the main content of the layout is each label, click to select, it will The soft keyboard pops up automatically, press the delete key to delete the selected label; it also supports the automatic selection of the last label through the delete key of the soft keyboard, and then press the delete key to delete the label; it supports input in the input box to add a label, when Enter the content and add a comma, it will automatically recognize and add the content before the comma to the layout as a new label. It can be seen that this layout is still very useful when sending emails to select recipients. The plus button on the right can add labels in batches. When writing an email, you can click to jump to the address book list interface to select multiple recipients, and then bring the data back to this layout as a new label.
The layout is written with reference to Zhang Hongyang’s related blog. The core is the radio layout and label selection. The clever part is to add a layer of FrameLayout to each sub-control of the fluid layout for unified processing. The title, input The boxes and checkboxes are new additions by me. I won’t say much about the specific implementation. Zhang Hongyang’s blog is very clear. If you are interested, please read these two articles. Define ViewGroup Practical Chapter-> Implement FlowLayout, Android TagFlowLayout completely parses a layout for Tag.
Let’s see how to use this layout directly here:
Add warehouse: Add jitpack warehouse to build.gradle in the project root directory:
allprojects {
repositories {
…
maven { url ‘https://jitpack.io’ }
}
}
Add dependencies: Add dependencies in the project’s build.gradle:
dependencies {
implementation ‘com.github.qugenting:flowlayout-singlechoose-library:1.0.0’
}
Interface call:
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;
import com.qugenting.view.flowlayout.FlowLayout;
import com.qugenting.view.flowlayout.TagAdapter;
import com.qugenting.view.flowlayout.TagFlowLayout;
public class MainActivity extends Activity {
private String[] mVals = new String[]{"[email protected]" , "[email protected]", "[email protected]", "[email protected]", "[email protected]"};
private LayoutInflater mInflater;
private ImageButton mImageButtonAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window. FEATURE_NO_TITLE);
setContentView(R. layout. activity_main);
mInflater = LayoutInflater. from(this);
TagFlowLayout flowLayout = findViewById(R.id.id_flowlayout);
flowLayout.setAttachLabel(true);//Set whether to add a label, add by default
flowLayout.setAdapter(adapter);
mImageButtonAdd = findViewById(R.id.ib_add);
mImageButtonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.add("[email protected]");
adapter.add("[email protected]");adapter.add("[email protected]");
adapter. notifyDataChanged();
}
});
}
private TagAdapter adapter = new TagAdapter(mVals) {
@Override
public View getView(FlowLayout parent, int position, String s) {
TextView tv = (TextView) mInflater.inflate(R.layout.tv_item, parent, false);
tv. setText(s);
return tv;
}
@Override
public View getLabelView(FlowLayout parent) {
//If you set flowLayout.setAttachLabel(false); the label will not be displayed
TextView tv = (TextView) mInflater.inflate(R.layout.tv_label, parent, false);
tv.setText("Recipient:");
return tv;
}
@Override
public View getInputView(FlowLayout parent) {
return mInflater.inflate(R.layout.edt, parent, false);
}
};
}
It’s very simple. The adapter implements three methods, namely the layout of the label, title and input box, and then set the flowlayout to setAdapter. If the title is not used, then set setAttachLabel(false). As for the addition and deletion of data, you can Just operate the adapter and adapter.notifyDataChanged().
The layout file will not be posted. If you need it, you can also go to my github to see: https://github.com/qugenting /MyFlowLayout, feel free to report any deficiencies.