网站首页
手机版

Android流式布局如何实现历史搜索记录

更新时间:2022-12-07 06:05:13作者:未知

Android流式布局如何实现历史搜索记录

最近在开发项目的时候,有一个需求是展示历史搜索记录 ,展示的样式是流式布局(就是根据内容自动换行)。在网上看到了一个不错的类库跟大家分享一下

首先在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>

实现模拟搜索效果图

Android流式布局如何实现历史搜索记录

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界面

效果图如下:

Android流式布局如何实现历史搜索记录

这样就实现了一个简单的流式布局历史搜索记录

GitHub地址:https://github.com/zhangliyong114/FlowLayoutDemo

本文标签: 布局  流式  历史  

为您推荐

荣耀8a微信分身怎么打开 华为荣耀8微信分身在哪里

荣耀8a微信分身怎么打开,荣耀8a微信分身打开的方法是:1、打开进入荣耀畅玩8a的手机桌面后,在桌面上找到并点击打开【设置】。2、进入设置页面后,找到并点击【应用和通知】。3、在应用和通知页面点击选

2023-06-30 16:47

电脑能连airpods吗(电脑可以连airpods2吗)

电脑能连airpods吗,电脑是能连接airpods的。以Windows 10为例,连接airpods的方法如下:1、如果AirPods在耳机盒中,请打开盒盖,然后按住耳机盒背面的设置按钮直到状态指示灯闪烁白色。2、接着在任务

2023-06-30 16:46

iPhonexr支持动态壁纸吗 iphonexr可以设置动态锁屏壁纸吗

苹果xr支持动态壁纸吗,苹果xr支持动态壁纸,设置的方法如下:1、进入手机桌面之后,找到并且打开【设置】。2、滑动手机屏幕,就可以看见【墙纸】,点击进去。3、然后点击【选取新的墙纸】。4、可以看到【动

2023-06-30 16:46

excel分列后怎么合并 excel分列后怎么合并加逗号

excel分列后怎么合并,以何炅栀子花开为例,excel分列后合并的如下:1、打开excel,用到公式和&,&代表的是合并的意思,在第三输入公式“=A1&B1” 回车后会发现单元格变成了何炅栀子花开。2、如果要在歌手

2023-06-30 16:46

荣耀8x和9x的区别(华为荣耀8x和9x哪个好)

荣耀8x和9x的区别,荣耀8x和9x的区别是:  1、外观设计荣耀8X采用的是6.5英寸的水滴屏,边框极窄的它被称作了“千元屏霸”,采用的是后置指纹识别技术,背屏两侧有很大弧度的收缩,这也赋予了它非常不

2023-06-30 16:46

iPhonexr支持反向充电吗 iphonexr可以反向充电吗?

苹果xr支持反向充电吗,苹果xr支持反向充电,只要打开OTG功能就可以反向充电的。反向充电的意思是:可以使用自己的手机给其他手机充电,但是前提是需要两台手机使用一根据线和一根OTG线连接在一起即可。

2023-06-30 16:45