依赖:
1.添加依赖
①.在项目的 build.gradle 文件中添加
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
②.在 module 的 build.gradle 文件中添加依赖
dependencies {
implementation 'com.github.LRH1993:AutoFlowLayout:1.0.5'
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MySearch mMysearch;
private ImageView mMainRight;
private AutoFlowLayout mMainFlow;
/**
* 清空历史搜索
*/
private Button mMainButton;
private LayoutInflater layoutInflater;
private List<String> list;
private String s;
private View view;
private TextView flow_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// 流式布局数据
list = new ArrayList<>();
}
//初始化控件
private void initView() {
mMysearch = (MySearch) findViewById(R.id.Mysearch);
mMainRight = (ImageView) findViewById(R.id.Main_right);
mMainRight.setOnClickListener(this);
mMainFlow = (AutoFlowLayout) findViewById(R.id.Main_flow);
layoutInflater = LayoutInflater.from(this);
mMainButton = (Button) findViewById(R.id.main_button);
mMainButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.Main_right:
//取得输入框内容
s = mMysearch.getEdit();
Log.d("MainActivity-----", s);
// 加入到搜索框集合
list.add(s);
Toast.makeText(this, "mData.length:" + list.size(), Toast.LENGTH_SHORT).show();
// 初始化流失布局条目布局
view = layoutInflater.inflate(R.layout.layout_flow, null);
flow_text = view.findViewById(R.id.flow_text);
// 给每个条目赋值输入的历史记录
for (int i = 0; i < list.size(); i++) {
flow_text.setText(list.get(i));
}
// 加入流式布局
mMainFlow.addView(view);
break;
case R.id.main_button:
// 清空历史
list.clear();
Log.d("MainActivity", "list.size():" + list.size());
mMainFlow.removeAllViews();
break;
}
}
}
自定义搜索控件
public class MySearch extends LinearLayout{
private EditText mysearch_edi;
public MySearch(Context context) {
this(context,null);
}
public MySearch(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MySearch(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.layout_mysearch, this);
mysearch_edi = view.findViewById(R.id.mysearch_edit);
}
// 获取输入框内容
public String getEdit(){
return mysearch_edi.getText().toString();
}
}
更新的方法
// mAtflow 流式控件
mAtflow.setAdapter(new FlowAdapter(list_flow) {
@Override
public View getView(int i) {
// 子条目布局
view_atfl = View.inflate(HistoryActivity.this, R.layout.flow_layout, null);
TextView text = view_atfl.findViewById(R.id.flw_text);
// 设置数据
text.setText(list_flow.get(i));
return view_atfl;
}
});