搜索界面逻辑修改

This commit is contained in:
huangzhuanghua
2016-06-14 18:34:36 +08:00
parent 169ddbd375
commit 59024c95c4
17 changed files with 729 additions and 772 deletions

View File

@ -1,16 +1,15 @@
package com.gh.gamecenter;
import android.app.ActionBar.LayoutParams;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
@ -22,10 +21,11 @@ import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import com.gh.base.BaseActivity;
import com.gh.base.BaseFragmentActivity;
import com.gh.common.constant.Config;
import com.gh.common.util.DisplayUtils;
import com.gh.gamecenter.db.SearchHistoryDao;
import com.gh.gamecenter.eventbus.EBSearch;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.SystemBarTintManager;
import com.gh.gamecenter.manager.SystemBarTintManager.SystemBarConfig;
@ -35,40 +35,39 @@ import com.gh.gamecenter.search.Search3HistoryFragment;
import com.tendcloud.tenddata.TCAgent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SearchActivity extends BaseActivity {
private final static int SEARCH_HISTORY_MODEL = 0;
private final static int GAME_LIST_MODEL = 1;
private final static int GAME_DETAIL_MODEL = 2;
private final static int FIND_NOTHING_MODEL = 3;
public static String searchKey;
public static boolean readyToSearch = true;
public static TextView searchButton;
public static EditText searchInput;
private SearchHistoryDao dao;
public class SearchActivity extends BaseFragmentActivity {
private Search1DetailFragment game_detail_fragment;
private Search2GameListFragment game_list_fragment;
private Search3HistoryFragment search_history_fragment;
private FragmentManager fragmentManager;
private ImageView searchCancel;
private RelativeLayout searchBack;
private EditText searchInput;
private ImageView searchCancel;
private TextView searchButton;
private String hint;
private boolean isFromHome = false;
private SearchHistoryDao dao;
private TextWatcher mTextWatcher;
private String searchKey;
private int currentTab;
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentTab", currentTab);
outState.putString("searchKey", searchKey);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = View.inflate(this, R.layout.activity_search, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTheme(R.style.AppTheme);
setTranslucentStatus(true);
@ -79,27 +78,38 @@ public class SearchActivity extends BaseActivity {
contentView.setPadding(0, config.getPixelInsetTop(false), 0,
config.getPixelInsetBottom());
}
setContentView(contentView);
dao = new SearchHistoryDao(this);
fragmentManager = getFragmentManager();
boolean isFromHome = getIntent().getBooleanExtra("clicked", false);
String hint = getIntent().getStringExtra("hint");
isFromHome = getIntent().getExtras().getBoolean("clicked");
hint = getIntent().getExtras().getString("hint");
setActionBarLayout(hint, isFromHome);
setActionBarLayout();
if (isFromHome && !TextUtils.isEmpty(hint)) {
searchKey = hint;
setResultPresentModel(GAME_DETAIL_MODEL);
if (savedInstanceState != null) {
currentTab = savedInstanceState.getInt("currentTab", 0);
searchKey = savedInstanceState.getString("searchKey", null);
if (TextUtils.isEmpty(searchKey)) {
currentTab = 0;
} else {
searchInput.setText(searchKey);
searchInput.setSelection(searchInput.getText().length());
}
} else {
setResultPresentModel(SEARCH_HISTORY_MODEL);
currentTab = 0;
if (isFromHome && !TextUtils.isEmpty(hint)) {
searchKey = hint;
currentTab = 2;
}
}
setResultPresentModel(currentTab);
searchInput.addTextChangedListener(mTextWatcher);
}
public void setActionBarLayout() {
public void setActionBarLayout(final String hint, final boolean isFromHome) {
int actionbar_height = getSharedPreferences(Config.PREFERENCE,
Context.MODE_PRIVATE).getInt("actionbar_height",
DisplayUtils.dip2px(getApplicationContext(), 48));
@ -114,28 +124,25 @@ public class SearchActivity extends BaseActivity {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
searchKey = searchInput.getText().toString().trim();
if (searchKey.length() < 1) {
String newSearchKey = searchInput.getText().toString().trim();
if (newSearchKey.length() < 1) {
if (!TextUtils.isEmpty(hint)) {
newSearchKey = searchInput.getHint().toString();
searchInput.setText(hint);
searchInput.setSelection(searchInput.getText()
.length());
searchKey = searchInput.getHint().toString();
searchInput.setSelection(searchInput.getText().length());
}
}
if (!TextUtils.isEmpty(searchKey)) {
if (readyToSearch) {
readyToSearch = false;
setResultPresentModel(GAME_DETAIL_MODEL);
if (!newSearchKey.equals(searchKey) || currentTab != 2) {
searchKey = newSearchKey;
if (!TextUtils.isEmpty(searchKey)) {
setResultPresentModel(2);
dao.add(searchKey);
} else {
toast("请输入搜索内容");
}
// TODO: add new history
dao.add(searchKey);
} else {
toast("请输入搜索内容");
}
}
return false;
@ -159,24 +166,22 @@ public class SearchActivity extends BaseActivity {
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
searchKey = searchInput.getText().toString().trim();
if (searchKey.length() < 1) {
String newSearchKey = searchInput.getText().toString().trim();
if (newSearchKey.length() < 1) {
if (!TextUtils.isEmpty(hint)) {
newSearchKey = searchInput.getHint().toString();
searchInput.setText(hint);
searchInput
.setSelection(searchInput.getText().length());
searchKey = searchInput.getHint().toString();
searchInput.setSelection(searchInput.getText().length());
}
}
if (!TextUtils.isEmpty(searchKey)) {
if (readyToSearch) {
readyToSearch = false;
setResultPresentModel(GAME_DETAIL_MODEL);
if (!newSearchKey.equals(searchKey) || currentTab != 2) {
searchKey = newSearchKey;
if (!TextUtils.isEmpty(searchKey)) {
setResultPresentModel(2);
dao.add(searchKey);
} else {
toast("请输入搜索内容");
}
// TODO: add new history
dao.add(searchKey);
} else {
toast("请输入搜索内容");
}
}
});
@ -189,15 +194,15 @@ public class SearchActivity extends BaseActivity {
}
});
searchInput.addTextChangedListener(new TextWatcher() {
mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
searchCancel.setVisibility(View.VISIBLE);
} else {
@ -207,17 +212,19 @@ public class SearchActivity extends BaseActivity {
@Override
public void afterTextChanged(Editable s) {
searchKey = s.toString().trim();
if (searchKey.length() < 1) {
setResultPresentModel(SEARCH_HISTORY_MODEL);
} else {
setResultPresentModel(GAME_LIST_MODEL);
String newSearchKey = s.toString().trim();
if (!newSearchKey.equals(searchKey)) {
searchKey = newSearchKey;
if (searchKey.length() < 1) {
setResultPresentModel(0);
} else {
setResultPresentModel(1);
}
}
}
});
};
searchBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
@ -227,53 +234,75 @@ public class SearchActivity extends BaseActivity {
}
private void setResultPresentModel(int model) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
hideFragments(transaction);
switch (model) {
case GAME_DETAIL_MODEL:
TCAgent.onEvent(SearchActivity.this, "搜索页面", searchKey);
case 0:
if (search_history_fragment == null) {
List<Fragment> list = getSupportFragmentManager().getFragments();
if (list != null) {
for (Fragment fragment : list) {
if (fragment.getClass().equals(Search3HistoryFragment.class)) {
transaction.remove(fragment);
break;
}
}
}
} else {
transaction.remove(search_history_fragment);
}
search_history_fragment = new Search3HistoryFragment();
transaction.add(R.id.search_result, search_history_fragment);
currentTab = 0;
break;
case 1:
if (game_list_fragment == null) {
List<Fragment> list = getSupportFragmentManager().getFragments();
if (list != null) {
for (Fragment fragment : list) {
if (fragment.getClass().equals(Search2GameListFragment.class)) {
transaction.remove(fragment);
break;
}
}
}
} else {
transaction.remove(game_list_fragment);
}
game_list_fragment = new Search2GameListFragment();
game_list_fragment.setKey(searchKey);
transaction.add(R.id.search_result, game_list_fragment);
currentTab = 1;
break;
case 2:
TCAgent.onEvent(SearchActivity.this, "搜索页面", searchKey);
Map<String, Object> map = new HashMap<String, Object>();
map.put("key", searchKey);
map.put("from", "搜索页面");
map.put("createdOn", System.currentTimeMillis() / 1000);
DataCollectionManager.onEvent(this, "search", map);
if (game_detail_fragment != null) {
transaction.remove(game_detail_fragment);
game_detail_fragment.clearSearchResult();
game_detail_fragment = null;
}
game_detail_fragment = new Search1DetailFragment();
game_detail_fragment.SetKeys(searchKey);
transaction.add(R.id.search_result, game_detail_fragment);
break;
Map<String, Object> map = new HashMap<String, Object>();
map.put("key", searchKey);
map.put("from", "搜索页面");
map.put("createdOn", System.currentTimeMillis() / 1000);
DataCollectionManager.onEvent(this, "search", map);
case SEARCH_HISTORY_MODEL:
if (search_history_fragment != null) {
transaction.remove(search_history_fragment);
search_history_fragment.clearSearchResult();
search_history_fragment = null;
}
search_history_fragment = new Search3HistoryFragment();
transaction.add(R.id.search_result, search_history_fragment);
break;
case GAME_LIST_MODEL:
if (game_list_fragment != null) {
transaction.remove(game_list_fragment);
game_list_fragment.clearSearchResult();
game_list_fragment = null;
}
game_list_fragment = new Search2GameListFragment();
game_list_fragment.setKeys(searchKey);
transaction.add(R.id.search_result, game_list_fragment);
break;
case FIND_NOTHING_MODEL:
break;
default:
break;
if (game_detail_fragment == null) {
List<Fragment> list = getSupportFragmentManager().getFragments();
if (list != null) {
for (Fragment fragment : list) {
if (fragment.getClass().equals(Search1DetailFragment.class)) {
transaction.remove(fragment);
break;
}
}
}
} else {
transaction.remove(game_detail_fragment);
}
game_detail_fragment = new Search1DetailFragment();
game_detail_fragment.setKey(searchKey);
transaction.add(R.id.search_result, game_detail_fragment);
currentTab = 2;
break;
default:
break;
}
transaction.commit();
}
@ -288,24 +317,36 @@ public class SearchActivity extends BaseActivity {
if (search_history_fragment != null) {
transaction.hide(search_history_fragment);
}
List<Fragment> list = getSupportFragmentManager().getFragments();
if (list != null) {
for (Fragment fragment : list) {
transaction.hide(fragment);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
public void onEvent(EBSearch search) {
String str = search.getKey();
searchInput.setText(str);
searchInput.setFocusable(true);
searchInput.requestFocus();
searchInput.setSelection(str.length());
if (search.isSearch()) {
searchButton.performClick();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
dao = null;
game_detail_fragment = null;
game_list_fragment = null;
search_history_fragment = null;
fragmentManager = null;
searchCancel = null;
searchBack = null;
hint = null;
searchInput = null;
searchCancel = null;
searchButton = null;
dao = null;
searchKey = null;
}
}