网站建设资讯

NEWS

网站建设资讯

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

 
 
  
 
   
 
  

tv.xml

 
 
 

drawable下面创建

checked_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
 
  
  
  
 
  
 
 

normal_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
 
  
  
  
  
 

tag_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
 
  
 
  
  
 

text_color.xml

<?xml version="1.0" encoding="utf-8"?> 
 
 
  
  
 
 

MainActivity

public class MainActivity extends AppCompatActivity { 
 private TagFlowLayout mFlowLayout; 
 private EditText editText; 
 private Button button; 
 private List strings; 
 String history=""; 
 int a=0; 
 List 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(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 set = new ArraySet<>(); 
     set.add(aa); 
     historylist.add(aa); 
     a++; 
     history+= aa+" "; 
     for (int i=0;i

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


新闻名称:FlowLayout流式布局实现搜索清空历史记录
网页网址:http://cdweb.net/article/jojesc.html