Android流式布局實現(xiàn)歷史搜索記錄功能
最近在開發(fā)項目的時候,有一個需求是展示歷史搜索記錄 ,展示的樣式是流式布局(就是根據(jù)內(nèi)容自動換行)。在網(wǎng)上看到了一個不錯的類庫跟大家分享一下
首先在AndroidStudio簡歷一個工程項目導入module類庫,我會把項目demo方法GitHub上
說一下demo中的實現(xiàn)方式
在 activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="確定" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.zhy.view.flowlayout.TagFlowLayout
android:id="@+id/id_flowlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:max_select="-1" />
</ScrollView>
</LinearLayout>
實現(xiàn)模擬搜索效果圖

MainActivity.Java 代碼
public class MainActivity extends AppCompatActivity {
private TagFlowLayout mFlowLayout;
private EditText editText;
private Button button;
private List<String> strings;
//布局管理器
private LayoutInflater mInflater;
//流式布局的子布局
private TextView tv;
public Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
mFlowLayout.setAdapter(new TagAdapter<String>(strings) {
@Override
public View getView(FlowLayout parent, int position, String s) {
tv = (TextView) mInflater.inflate(R.layout.tv,
mFlowLayout, false);
tv.setText(s);
return tv;
}
});
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInflater = LayoutInflater.from(this);
mFlowLayout = (TagFlowLayout) findViewById(R.id.id_flowlayout);
editText = (EditText) findViewById(R.id.edt);
button = (Button) findViewById(R.id.btn);
strings = new ArrayList<>();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String aa = editText.getText().toString().trim();
strings.add(aa);
//通知handler更新UI
handler.sendEmptyMessageDelayed(1, 0);
}
});
//流式布局tag的點擊方法
mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
@Override
public boolean onTagClick(View view, int position, FlowLayout parent) {
Toast.makeText(MainActivity.this, tv.getText(), Toast.LENGTH_SHORT).show();
return true;
}
});
}
當我們點擊確定按鈕的時候,通知handler 去更新UI界面
效果圖如下:

這樣就實現(xiàn)了一個簡單的流式布局歷史搜索記錄
GitHub地址:https://github.com/zhangliyong114/FlowLayoutDemo
以上所述是小編給大家介紹的Android流式布局實現(xiàn)歷史搜索記錄功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android自定義流式布局實現(xiàn)淘寶搜索記錄
- Android本地實現(xiàn)搜索歷史記錄
- Android實現(xiàn)搜索保存歷史記錄功能
- Android項目類似淘寶 電商 搜索功能,監(jiān)聽軟鍵盤搜索事件,延遲自動搜索,以及時間排序的搜索歷史記錄的實現(xiàn)
- Android實現(xiàn)搜索功能并本地保存搜索歷史記錄
- Android實現(xiàn)簡易計步器功能隔天步數(shù)清零查看歷史運動紀錄
- android中AutoCompleteTextView的簡單用法(實現(xiàn)搜索歷史)
- Android中使用 AutoCompleteTextView 實現(xiàn)手機號格式化附帶清空歷史的操作
- Android實現(xiàn)搜索歷史功能
- Android實現(xiàn)歷史搜索記錄
相關文章
Android貝塞爾曲線實現(xiàn)加入購物車拋物線動畫
這篇文章主要為大家詳細介紹了Android貝塞爾曲線實現(xiàn)加入購物車拋物線動畫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06
Android學習筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫中(Saving Data in SQL Databases)
這篇文章主要介紹了Android學習筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫中的(Saving Data in SQL Databases)2014-10-10

