国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android流式布局實現歷史搜索記錄功能

2019-10-23 18:29:15
字體:
來源:轉載
供稿:網友

最近在開發項目的時候,有一個需求是展示歷史搜索記錄 ,展示的樣式是流式布局(就是根據內容自動換行)。在網上看到了一個不錯的類庫跟大家分享一下

首先在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流式布局實現,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流式布局實現,android,歷史搜索記錄

這樣就實現了一個簡單的流式布局歷史搜索記錄

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

以上所述是小編給大家介紹的Android流式布局實現歷史搜索記錄功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昌都县| 龙游县| 上饶市| 沂南县| 大足县| 莱西市| 全椒县| 泰来县| 章丘市| 天等县| 卢湾区| 宾川县| 鸡泽县| 新营市| 长治市| 沈阳市| 阿巴嘎旗| 通城县| 潮安县| 肥西县| 三原县| 成都市| 陈巴尔虎旗| 长汀县| 交口县| 克东县| 宜丰县| 临湘市| 犍为县| 察哈| 襄垣县| 定州市| 阿拉善左旗| 通州市| 金坛市| 莆田市| 泰顺县| 马尔康县| 盐城市| 常熟市| 常熟市|