日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

Android流式布局如何實(shí)現(xiàn)歷史搜索記錄


Android流式布局如何實(shí)現(xiàn)歷史搜索記錄


最近在開(kāi)發(fā)項(xiàng)目的時(shí)候,有一個(gè)需求是展示歷史搜索記錄,展示的樣式是流式布局(就是根據(jù)內(nèi)容自動(dòng)換行) 。在網(wǎng)上看到了一個(gè)不錯(cuò)的類庫(kù)跟大家分享一下
首先在AndroidStudio簡(jiǎn)歷一個(gè)工程項(xiàng)目導(dǎo)入module類庫(kù),我會(huì)把項(xiàng)目demo方法GitHub上
說(shuō)一下demo中的實(shí)現(xiàn)方式
在 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"><EditTextandroid:id="@+id/edt"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="確定" /><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>實(shí)現(xiàn)模擬搜索效果圖
Android流式布局如何實(shí)現(xiàn)歷史搜索記錄


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() {@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.setText(s);return tv;}});break;}super.handleMessage(msg);}};@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);strings = new ArrayList<>();button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String aa = editText.getText().toString().trim();strings.add(aa);//通知handler更新UIhandler.sendEmptyMessageDelayed(1, 0);}});//流式布局tag的點(diǎn)擊方法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;}});}當(dāng)我們點(diǎn)擊確定按鈕的時(shí)候,通知handler 去更新UI界面
效果圖如下:
Android流式布局如何實(shí)現(xiàn)歷史搜索記錄


【Android流式布局如何實(shí)現(xiàn)歷史搜索記錄】這樣就實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的流式布局歷史搜索記錄
GitHub地址:https://github.com/zhangliyong114/FlowLayoutDemo

    推薦閱讀