FlowLayout流式布局实现搜索清空历史记录
效果图:点击搜索框将搜索的历史在流式布局中展示出来,清空历史记录就会将历史清空,每次搜索后都存入sp中,每次进入页面都先判断sp里是否有值并展示
首先需要导入一个module,下载地址:https://github.com/zhangliyong114/FlowLayoutDemo
下载完这个工程后,需要将里面的flowlayout-lib导入到工程中,
导入工程的步骤:File - New - Import Module 选中这个flowlayout-lib
导入完成后,在项目的build.gradle中对导入的module进行依赖
compile project(':flowlayout-lib')
activity_main.xml
<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:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="16dp"tools:context="com.example.searchhistory.MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:orientation="horizontal"><EditTextandroid:id="@+id/edt"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="4" /><Buttonandroid:id="@+id/btn"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="搜索" /><Buttonandroid:id="@+id/clear"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="清空" /></LinearLayout><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"><com.zhy.view.flowlayout.TagFlowLayoutandroid:id="@+id/id_flowlayout"android:layout_width="fill_parent"android:layout_height="wrap_content"app:max_select="-1" /></ScrollView>
</LinearLayout>
tv.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:layout_marginTop="10dp"android:background="@drawable/tag_bg"android:text="Helloworld"android:textColor="#999999"android:textSize="16sp"></TextView>
drawable下面创建
checked_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#ffffff" /><corners android:radius="2dp" /><strokeandroid:width="1dp"android:color="#dddddd" /><paddingandroid:bottom="5dp"android:left="14dp"android:right="14dp"android:top="5dp" /></shape>
normal_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#ffffff" /><corners android:radius="2dp" /><strokeandroid:width="1dp"android:color="#dddddd" /><paddingandroid:bottom="5dp"android:left="14dp"android:right="14dp"android:top="5dp" />
</shape>
tag_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/checked_bg" android:state_checked="true"></item><item android:drawable="@drawable/normal_bg"></item>
</selector>
text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:color="#999999" android:state_checked="true" /><item android:color="#f692ff" /></selector>
MainActivity
public class MainActivity extends AppCompatActivity {private TagFlowLayout mFlowLayout;private EditText editText;private Button button;private List<String> strings;String history="";int a=0;List<String> historylist = new ArrayList<>();//布局管理器private LayoutInflater mInflater;//流式布局的子布局private TextView tv;public Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case 1:mFlowLayout.setAdapter(new TagAdapter<String>(strings) {@Overridepublic View getView(FlowLayout parent, int position, String s) {tv = (TextView) mInflater.inflate(R.layout.tv,mFlowLayout, false);tv.setVisibility(View.VISIBLE);tv.setText(s);return tv;}});break;}super.handleMessage(msg);}};private Button clearbtn;@RequiresApi(api = Build.VERSION_CODES.M)@Overrideprotected 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);clearbtn = findViewById(R.id.clear);final SharedPreferences preferences = getSharedPreferences("config", 0);final SharedPreferences.Editor editor = preferences.edit();strings = new ArrayList<>();final String string = preferences.getString("string", " ");String[] split = string.split(" ");if (split.length>0&&!string.equals(" ")){for (int i=0;i<split.length;i++){strings.add(split[i]);}handler.sendEmptyMessageDelayed(1, 0);}clearbtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {history="";historylist.clear();editor.clear().commit();//清空下面的strings.clear();handler.sendEmptyMessageDelayed(1, 0);}});button.setOnClickListener(new View.OnClickListener() {@RequiresApi(api = Build.VERSION_CODES.M)@Overridepublic void onClick(View v) {String string = preferences.getString("string", "");historylist.clear();if (!editText.getText().toString().trim().equals("")) {String aa = editText.getText().toString().trim();Set<String> set = new ArraySet<>();set.add(aa);historylist.add(aa);a++;history+= aa+" ";for (int i=0;i<historylist.size();i++){editor.putString("string",history).commit();strings.add(historylist.get(i));}//通知handler更新UIhandler.sendEmptyMessageDelayed(1, 0);}else{Toast.makeText(MainActivity.this, "请输入要搜索的内容", Toast.LENGTH_SHORT).show();}}});//流式布局tag的点击方法mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {@Overridepublic boolean onTagClick(View view, int position, FlowLayout parent) {Toast.makeText(MainActivity.this, tv.getText(), Toast.LENGTH_SHORT).show();return true;}});}
}
FlowLayout流式布局实现搜索清空历史记录相关推荐
- android 搜索历史流布局,FlowLayout流式布局实现搜索清空历史记录
本文实例为大家分享了FlowLayout实现搜索清空历史记录的具体代码,供大家参考,具体内容如下 效果图:点击搜索框将搜索的历史在流式布局中展示出来,清空历史记录就会将历史清空,每次搜索后都存入sp中 ...
- 自定义 FlowLayout流式布局搜索框 加 GreenDao存取搜索记录,使用RecyclerView展示
输入框布局的shape <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android ...
- Android FlowLayout 流式布局
FlowLayout 流式布局 Android 流式布局控件,实现自动换行,操出范围可以滑动功能,未使用控件复用功能,所以不应该有太多的子控件. 主要包含功能: 流式布局,自动换行 使用Adapter ...
- Android FlowLayout流式布局
最近使用APP的时候经常看到有 这种流式布局 ,今天我就跟大家一起来动手撸一个这种自定义控件. 首先说一下自定义控件的流程: 自定义控件一般要么继承View要么继承ViewGroup View的自定义 ...
- Flowlayout流式布局使用(轻量级)
Flowlayout属于自定义流式布局,意思就是说从左上角开始添加原件,依次往后排,第一行挤满了就换一行接着排. 本文所使用的FlowLayout来自于鸿洋大神的框架. 只取了一个自定义控件,没有鸿洋 ...
- 4.布局:FlowLayout流式布局(Java swing 入门)
FlowLayout(流式布局管理器)是 JPanel 和 JApplet 的默认布局管理器.FlowLayout 会将组件按照从上到下.从左到右的放置规律逐行进行定位.与其他布局管理器不同的是,Fl ...
- FlowLayout流式布局管理器与网格布局GridLayout
总结 1.继承JFrame类 2.在最上方定义组件 3.在构造方法中创建组件 4.在构造方法中添加组件 5.设置窗体属性 6.显示窗体 7.在主函数中创建对象 所有布局管理器都可以添加任意组件 (滚动 ...
- android 热搜词 布局,Android FlowLayout流式布局打造热门标签(高仿抖音热搜)
需要先学习下面2个内容 1.已经基本给大家介绍了如何自定义ViewGroup,如果你还不了解 2.宽高的计算 一.XML布局 从布局图中可以看到,FlowLayout中包含了很多TextView.难度 ...
- FlowLayout 流式布局加点击事件
//简单优化之后的 public class FlowLayout extends ViewGroup {private Context con;private View child;private ...
最新文章
- BI之SSAS完整实战教程3 -- 创建第一个多维数据集
- ps抠图怎么放大图片_PS教程:透明玻璃杯不会抠图?一分钟利用通道面板快速抠图...
- Struts2工作原理详解
- Pycharm使用详解
- java 解密后为空_java RSA加密解密
- TeaPot 用webgl画茶壶(3) 环境纹理和skybox
- python 升级所有库_自动更新Python所有第三方库
- oracle的undo
- LINUX任务栏上出现的小窗口是怎么回事
- SpringMVC Redirect 跳转后保存Model中的数据
- 我为什么放弃使用MyBatis3的Mapper注解
- 功能机用上下键实现MoveEvent
- 我要写整个中文互联网界最牛逼的JVM系列教程 | 「JVM与Java体系架构」章节:官方规范下载与参考书目
- 上班族们都有哪些一直坚持的业余兴趣爱好?
- 最新江苏安全员B考试判断练习题库
- 二零零九年经典雷人语录总汇四百零五条[转的]
- 实体零售纷纷转型,苏宁、乐语到底能给我们带来哪些启示?
- 一个IT售前咨询顾问是如何工作和生活的?
- A + B Proble
- 内蒙古联通云计算机,中国联通西北(呼和浩特)云计算基地