[置顶] Android流式布局实现历史搜索记录
2017-05-09 10:51
519 查看
最近在开发项目的时候,有一个需求是展示历史搜索记录 ,展示的样式是流式布局(就是根据内容自动换行)。在网上看到了一个不错的类库跟大家分享一下
首先在AndroidStudio简历一个工程项目导入module类库,我会把项目demo方法GitHub上
说一下demo中的实现方式
在 activity_main.xml中
实现模拟搜索效果图
![](http://img.my.csdn.net/uploads/201705/09/1494297599_6107.png)
MainActivity.java 代码
当我们点击确定按钮的时候,通知handler 去更新UI界面
效果图如下:
![](http://img.my.csdn.net/uploads/201705/09/1494297770_1523.png)
这样就实现了一个简单的流式布局历史搜索记录
GitHub地址:https://github.com/zhangliyong114/FlowLayoutDemo
CSDN地址:http://download.csdn.net/detail/zhang_li_yong/9837216
首先在AndroidStudio简历一个工程项目导入module类库,我会把项目demo方法GitHub上
说一下demo中的实现方式
在 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>
实现模拟搜索效果图
![](http://img.my.csdn.net/uploads/201705/09/1494297599_6107.png)
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界面
效果图如下:
![](http://img.my.csdn.net/uploads/201705/09/1494297770_1523.png)
这样就实现了一个简单的流式布局历史搜索记录
GitHub地址:https://github.com/zhangliyong114/FlowLayoutDemo
CSDN地址:http://download.csdn.net/detail/zhang_li_yong/9837216
相关文章推荐
- Android流式布局实现历史搜索记录功能
- 简单商品搜索界面的实现,流失布局,历史记录,商品展示,商品详情
- FlowLayout流式布局实现搜索清空历史记录
- Android用AutoCompleteTextView实现搜索历史记录提示
- Android 类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现
- js实现搜索历史记录功能
- 简单实现Android搜索功能 显示清除历史搜索记录
- 搜索实现 app 本地搜索效果,有历史记录的
- 【玩转SQLite系列】(六)SQLite数据库应用案例实现历史搜索记录
- Android常用:手把手教你实现搜索框(含历史搜索记录)
- 实现Android搜索功能 显示清除历史搜索记录
- 实现搜索框(含历史搜索记录)
- 简单实现Android搜索功能 显示清除历史搜索记录
- jquery实现搜索框历史搜索记录功能
- Android开发————Android用AutoCompleteTextView实现搜索历史记录提示
- Android实现搜索功能并本地保存搜索历史记录
- Android实现搜索功能并本地保存搜索历史记录
- JS--历史搜索记录的实现
- 实现搜索的历史浏览记录,含数据库
- jquery实现搜索框历史搜索记录功能