在上一篇文章中我實現了標籤的展示,但是在實際功能中,標籤的個數基本都有一個最大值,不如不能超過8個,當添加第九個標籤的時候
要移除第一個標籤。
基於這個功能附上一小段代碼來解決這個問題。
for (String name : strings) {
//標籤最多設置8個 當大於8時移除第一個標籤
while (!(mFlexboxLayout.getChildCount() >= 0 && mFlexboxLayout.getChildCount() < 8)) {
mFlexboxLayout.removeViewAt(0);
}
TextView textView = createBaseFlexItemTextView(name);
textView.setLayoutParams(createDefaultLayoutParams());
if ("點擊跳轉到結合RecycleView界面".equals(name)) {
//添加點擊事件監聽
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextFlexViewActivity.this.startActivity(new Intent(TextFlexViewActivity.this, RvAndFlexbox.class));
}
});
}
mFlexboxLayout.addView(textView);
}