欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

FlowLayout流式布局实现搜索清空历史记录

程序员文章站 2022-04-30 22:54:26
本文实例为大家分享了flowlayout实现搜索清空历史记录的具体代码,供大家参考,具体内容如下 效果图:点击搜索框将搜索的历史在流式布局中展示出来,清空历史记录就会将历...

本文实例为大家分享了flowlayout实现搜索清空历史记录的具体代码,供大家参考,具体内容如下

效果图:点击搜索框将搜索的历史在流式布局中展示出来,清空历史记录就会将历史清空,每次搜索后都存入sp中,每次进入页面都先判断sp里是否有值并展示

FlowLayout流式布局实现搜索清空历史记录

首先需要导入一个module,下载地址

下载完这个工程后,需要将里面的flowlayout-lib导入到工程中,

FlowLayout流式布局实现搜索清空历史记录

导入工程的步骤:file - new - import module 选中这个flowlayout-lib

FlowLayout流式布局实现搜索清空历史记录

导入完成后,在项目的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"> 
 
 <linearlayout 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:gravity="center_vertical" 
  android:orientation="horizontal"> 
 
  <edittext 
   android:id="@+id/edt" 
   android:layout_width="0dp" 
   android:layout_height="wrap_content" 
   android:layout_weight="4" /> 
 
  <button 
   android:id="@+id/btn" 
   android:layout_width="0dp" 
   android:layout_height="wrap_content" 
   android:layout_weight="1" 
   android:text="搜索" /> 
 
  <button 
   android:id="@+id/clear" 
   android:layout_width="0dp" 
   android:layout_height="wrap_content" 
   android:layout_weight="1" 
   android:text="清空" /> 
 </linearlayout> 
 
 <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> 

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" /> 
 <stroke 
  android:width="1dp" 
  android:color="#dddddd" /> 
 
 <padding 
  android: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" /> 
 <stroke 
  android:width="1dp" 
  android:color="#dddddd" /> 
 <padding 
  android: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() { 
  @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.setvisibility(view.visible); 
       tv.settext(s); 
       return tv; 
      } 
     }); 
     break; 
 
   } 
   super.handlemessage(msg); 
  } 
 }; 
 private button clearbtn; 
 
 
 @requiresapi(api = build.version_codes.m) 
 @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); 
  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() { 
   @override 
   public 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) 
   @override 
   public 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更新ui 
     handler.sendemptymessagedelayed(1, 0); 
    }else{ 
     toast.maketext(mainactivity.this, "请输入要搜索的内容", toast.length_short).show(); 
    } 
   } 
  }); 
  //流式布局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; 
   } 
  }); 
 } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。