private void initData() { for (int i = 0; i < HOT_WORDS.length; i++) { TextView hotWordTextView = new TextView(this); hotWordTextView.setText(HOT_WORDS[i]); hotWordTextView.setTextSize(16); FlowLayout.LayoutParams params = new FlowLayout.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); params.rightMargin = 14; params.topMargin = 8; hotWordTextView.setBackgroundResource(R.drawable.search_item_bg); hotWordTextView.setLayoutParams(params); mHotWordsFlowLayout.addView(hotWordTextView); } initSearchWordList(); mSearchAdapter = new SearchAdapter(mSearchWordList, "abc", this); mSearchListView.setAdapter(mSearchAdapter); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View view) { switch (view.getId()) { case R.id.bt_search: mHotWordsFlowLayout.setVisibility(View.INVISIBLE); mSearchView.setVisibility(View.INVISIBLE); mResultView.setVisibility(View.VISIBLE); mIsResultShowing = true; boolean flag = new Random().nextBoolean(); if (true) { mResultBooksListView.setVisibility(View.VISIBLE); mAddBooksButton.setVisibility(View.INVISIBLE); SearchResultBooksInfo booksInfo = getBooksInfo(); showResultList(booksInfo); } else { mResultBooksListView.setVisibility(View.INVISIBLE); mAddBooksButton.setVisibility(View.VISIBLE); } break; } }
private void setListener() { mSearchButton.setOnClickListener(this); mSearchListView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {} }); mHotWordsFlowLayout.setOnItemClickListener( new FlowLayout.OnItemClickListener() { @Override public void onItemClick(View view) { String touchedHotWords = ((TextView) view).getText().toString(); mSearchEditText.setText(touchedHotWords); // 将editText的光标移到最后端 Selection.setSelection(mSearchEditText.getText(), mSearchEditText.getText().length()); } }); mSearchListView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) { mSearchEditText.setText(mSearchAdapter.getResultStringList().get(pos)); mSearchView.setVisibility(View.INVISIBLE); } }); mSearchEditText.addTextChangedListener( new TextWatcher() { @Override public void beforeTextChanged( CharSequence charSequence, int start, int count, int afte) {} @Override public void onTextChanged(CharSequence charSequence, int start, int before, int count) { if (!TextUtils.isEmpty(charSequence)) { mSearchAdapter.setSearchString(mSearchEditText.getText().toString()); ArrayList removeList = new ArrayList(); for (String searchWord : mSearchWordList) { if (!searchWord.contains(mSearchEditText.getText().toString().trim())) { removeList.add(searchWord); } } mSearchWordList.removeAll(removeList); mSearchAdapter.notifyDataSetChanged(); mHotWordsFlowLayout.setVisibility(View.INVISIBLE); mSearchView.setVisibility(View.VISIBLE); } else { mSearchView.setVisibility(View.INVISIBLE); if (mResultView.getVisibility() == View.VISIBLE) { mHotWordsFlowLayout.setVisibility(View.INVISIBLE); } else { mHotWordsFlowLayout.setVisibility(View.VISIBLE); } } } @Override public void afterTextChanged(Editable editable) {} }); }