public void handleMessage(Message msg) { CategoryFragment obj = ref.get(); if (obj != null) { if (MSG_SHOW_SEARCH == msg.what) { obj.mCategoryListAdapter.showCoverView(false); obj.mSearchUi.setVisibility(View.VISIBLE); obj.mInput.setSelection(0, obj.mInput.getText().length()); obj.mInput.setFocusable(true); obj.mInput.setFocusableInTouchMode(true); obj.mInput.requestFocus(); Utils.SoftInput.show(obj.getActivity(), obj.mInput); } else if (MSG_HIDE_SEARCH == msg.what) { ((BaseActivity) obj.getActivity()).getTitleBarContainer().setVisibility(View.VISIBLE); ((MainActivity) obj.getActivity()).tabs.setVisibility(View.VISIBLE); obj.mFakeBg.setVisibility(View.GONE); obj.mListView.removeHeaderView(obj.mTopView); obj.mListView.removeHeaderView(obj.mFakeSearchView); obj.mTopView = LayoutInflater.from(obj.getActivity()).inflate(R.layout.fake_top_for_search, null); obj.mTopViewContent = obj.mTopView.findViewById(R.id.top_view_content); obj.mTopViewContent.setBackgroundColor( ShopApp.getContext().getResources().getColor(R.color.fake_search_background)); obj.mFakeSearchView.setBackgroundColor( ShopApp.getContext().getResources().getColor(R.color.fake_search_background)); obj.mListView.setAdapter(null); obj.mListView.addHeaderView(obj.mTopView); obj.mListView.addHeaderView(obj.mFakeSearchView); obj.mListView.setAdapter(obj.mCategoryListAdapter); } } }
@Override protected void onPostExecute(ArrayList<String> result) { if (result != null) { ArrayList<SpannableString> data = new ArrayList<SpannableString>(); for (String s : result) { data.add(new SpannableString(s)); } if (mIsHot) { mHotWords = data; } if (mInput.getText().toString().equals(mKeyword)) { if (!TextUtils.isEmpty(mKeyword)) { int len = mKeyword.length(); int dataLen = result.size(); for (int i = 0; i < dataLen; i++) { if (result.get(i).startsWith(mKeyword)) { data.get(i) .setSpan( new ForegroundColorSpan( ShopApp.getContext() .getResources() .getColor(R.color.highlight_text_color)), 0, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } } mSearchHintListAdapter.updateData(data); } } else { mSearchHintListAdapter.updateData(null); } }
@Override public void beforeOnLyaout() { if (mCategoryAdapter.hasItemViewBound && !mIsFakeSearchViewPositioned) { mListView.setOnLayoutListener(null); mIsFakeSearchViewPositioned = true; mListView.setSelectionFromTop( TRUE_DATA_ITEM_IDX, mTopView.getHeight() - (int) ShopApp.getContext() .getResources() .getDimension(R.dimen.fake_search_layout_diff)); } }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.category_fragment, container, false); mFakeBg = view.findViewById(R.id.fake_bg); mListView = (BaseListView) view.findViewById(android.R.id.list); int listPadding = (int) ShopApp.getContext().getResources().getDimension(R.dimen.list_item_padding); mListView.setPadding(0, listPadding / 2, 0, listPadding); mSearchUi = view.findViewById(R.id.search_ui); mSearchUi.setOnClickListener(mSearchClickListener); mTopView = inflater.inflate(R.layout.fake_top_for_search, null); mTopViewContent = mTopView.findViewById(R.id.top_view_content); mTopViewContent.setBackgroundColor( ShopApp.getContext().getResources().getColor(R.color.fake_search_background)); mListView.addHeaderView(mTopView); mFakeSearchView = inflater.inflate(R.layout.fake_search_view, null); mIsFakeSearchViewPositioned = false; View fakeInput = mFakeSearchView.findViewById(R.id.fake_input); fakeInput.setOnClickListener(mSearchClickListener); View fakeButton = mFakeSearchView.findViewById(R.id.fake_button); fakeButton.setOnClickListener(mSearchClickListener); mFakeSearchView.setBackgroundColor( ShopApp.getContext().getResources().getColor(R.color.fake_search_background)); mListView.addHeaderView(mFakeSearchView); mListView.setOnLayoutListener(this); mCategoryAdapter = new CategoryAdapter(getActivity()); mCategoryListAdapter = new CategoryListAdapter(getActivity(), mCategoryAdapter, mItemClickListner); mListView.setAdapter(mCategoryListAdapter); ArrayList<Object> data = new ArrayList<Object>(); data.add(1); mCategoryListAdapter.updateData(data); mSearchButton = mSearchUi.findViewById(R.id.search_button); mSearchButton.setOnClickListener(mSearchClickListener); mInput = (AutoCompleteTextView) mSearchUi.findViewById(R.id.input); mInput.setOnEditorActionListener( new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { mSearchButton.performClick(); return true; } return false; } }); mInput.addTextChangedListener( new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void afterTextChanged(Editable s) { loadHint(); } }); mSearchEmptyArea = mSearchUi.findViewById(R.id.empty_area); mSearchEmptyArea.setOnClickListener(mSearchClickListener); mSearchHintListView = (ListView) mSearchEmptyArea.findViewById(android.R.id.list); mSearchHintListView.addHeaderView(inflater.inflate(R.layout.search_hint_list_top, null)); mSearchHintListAdapter = new SearchHintListAdapter(getActivity()); mSearchHintListView.setAdapter(mSearchHintListAdapter); mSearchHintListView.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int positon, long id) { View v = view.findViewById(R.id.word); if (v != null && v instanceof TextView) { doSearch(((TextView) v).getText().toString()); } } }); mLoadingView = (EmptyLoadingView) view.findViewById(R.id.loading); return view; }
public void showSearchUi(boolean show) { View titleBar = ((BaseActivity) getActivity()).getTitleBarContainer(); View tabs = ((MainActivity) getActivity()).tabs; if (show) { if (mFakeTopBmp == null) { int w = 0, h = 0, titleBmpH = 0, tabsBmpH = (int) ShopApp.getContext().getResources().getDimension(R.dimen.tabs_visible_height); titleBar.setDrawingCacheEnabled(true); tabs.setDrawingCacheEnabled(true); Bitmap titleBmp = titleBar.getDrawingCache(); Bitmap tabsBmp = tabs.getDrawingCache(); if (titleBmp != null) { w = titleBmp.getWidth(); mTitleHeight = titleBmpH = titleBmp.getHeight(); h += titleBmpH; } if (tabsBmp != null) { if (w == 0) { w = tabsBmp.getWidth(); } h += tabsBmpH; } mFakeTopBmp = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = new Canvas(mFakeTopBmp); Paint paint = new Paint(); if (titleBmp != null) { canvas.drawBitmap(titleBmp, 0, 0, paint); titleBmp.recycle(); } titleBar.setDrawingCacheEnabled(false); if (tabsBmp != null) { paint.setColor( ShopApp.getContext().getResources().getColor(R.color.fake_search_background)); canvas.drawRect(0, titleBmpH, w, titleBmpH + tabsBmpH, paint); canvas.drawBitmap(tabsBmp, 0, titleBmpH, paint); tabsBmp.recycle(); } tabs.setDrawingCacheEnabled(false); } mCategoryListAdapter.showCoverView(true); mFakeBg.setVisibility(View.VISIBLE); mFakeBg.setLayoutParams(new LayoutParams(mFakeTopBmp.getWidth(), mFakeTopBmp.getHeight())); mFakeBg.setBackgroundDrawable(new BitmapDrawable(mFakeTopBmp)); mTopView.setLayoutParams( new AbsListView.LayoutParams(mFakeTopBmp.getWidth(), mFakeTopBmp.getHeight())); mTopViewContent.setBackgroundColor( ShopApp.getContext().getResources().getColor(R.color.transparent)); titleBar.setVisibility(View.GONE); tabs.setVisibility(View.GONE); int scrollDis = mTitleHeight + mFakeSearchView.getTop() + (int) ShopApp.getContext() .getResources() .getDimension(R.dimen.fake_search_scroll_up_diff); mListView.smoothScrollBy(scrollDis, ANIMATION_DURATION); handler.sendEmptyMessageDelayed(UiHandler.MSG_SHOW_SEARCH, ACTION_DURATION); loadHint(); } else { mInput.setText(""); Utils.SoftInput.hide(getActivity(), mInput.getWindowToken()); mSearchUi.setVisibility(View.GONE); int scrollDis = -mFakeTopBmp.getHeight() + (int) ShopApp.getContext() .getResources() .getDimension(R.dimen.fake_search_scroll_down_diff); mListView.smoothScrollBy(scrollDis, ANIMATION_DURATION); handler.sendEmptyMessageDelayed(UiHandler.MSG_HIDE_SEARCH, ACTION_DURATION); } }