提交项目
This commit is contained in:
314
app/src/main/java/com/gh/gamecenter/SearchActivity.java
Normal file
314
app/src/main/java/com/gh/gamecenter/SearchActivity.java
Normal file
@ -0,0 +1,314 @@
|
||||
package com.gh.gamecenter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
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.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;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.TextView.OnEditorActionListener;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.gh.base.BaseActivity;
|
||||
import com.gh.common.constant.Config;
|
||||
import com.gh.common.util.DisplayUtils;
|
||||
import com.gh.gamecenter.db.SearchHistoryDao;
|
||||
import com.gh.gamecenter.manager.DataCollectionManager;
|
||||
import com.gh.gamecenter.manager.SystemBarTintManager;
|
||||
import com.gh.gamecenter.manager.SystemBarTintManager.SystemBarConfig;
|
||||
import com.gh.gamecenter.search.Search1DetailFragment;
|
||||
import com.gh.gamecenter.search.Search2GameListFragment;
|
||||
import com.gh.gamecenter.search.Search3HistoryFragment;
|
||||
import com.tendcloud.tenddata.TCAgent;
|
||||
|
||||
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;
|
||||
|
||||
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 String hint;
|
||||
private boolean isFromHome = false;
|
||||
|
||||
@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);
|
||||
SystemBarTintManager tintManager = new SystemBarTintManager(this);
|
||||
tintManager.setStatusBarTintEnabled(true);
|
||||
tintManager.setStatusBarTintResource(R.color.search_theme_colors);
|
||||
SystemBarConfig config = tintManager.getConfig();
|
||||
contentView.setPadding(0, config.getPixelInsetTop(false), 0,
|
||||
config.getPixelInsetBottom());
|
||||
}
|
||||
|
||||
setContentView(contentView);
|
||||
|
||||
dao = new SearchHistoryDao(this);
|
||||
|
||||
fragmentManager = getFragmentManager();
|
||||
|
||||
isFromHome = getIntent().getExtras().getBoolean("clicked");
|
||||
hint = getIntent().getExtras().getString("hint");
|
||||
|
||||
setActionBarLayout();
|
||||
|
||||
if (isFromHome && !TextUtils.isEmpty(hint)) {
|
||||
searchKey = hint;
|
||||
setResultPresentModel(GAME_DETAIL_MODEL);
|
||||
} else {
|
||||
setResultPresentModel(SEARCH_HISTORY_MODEL);
|
||||
}
|
||||
}
|
||||
|
||||
public void setActionBarLayout() {
|
||||
int actionbar_height = getSharedPreferences(Config.PREFERENCE,
|
||||
Context.MODE_PRIVATE).getInt("actionbar_height",
|
||||
DisplayUtils.dip2px(getApplicationContext(), 48));
|
||||
|
||||
LinearLayout reuse_actionbar = (LinearLayout) findViewById(R.id.search_actionbar);
|
||||
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
|
||||
LayoutParams.MATCH_PARENT, actionbar_height);
|
||||
reuse_actionbar.setLayoutParams(lparams);
|
||||
|
||||
searchInput = (EditText) findViewById(R.id.etSearch);
|
||||
searchInput.setOnEditorActionListener(new OnEditorActionListener() {
|
||||
@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) {
|
||||
if (!TextUtils.isEmpty(hint)) {
|
||||
searchInput.setText(hint);
|
||||
searchInput.setSelection(searchInput.getText()
|
||||
.length());
|
||||
searchKey = searchInput.getHint().toString();
|
||||
}
|
||||
}
|
||||
if (!TextUtils.isEmpty(searchKey)) {
|
||||
if (readyToSearch) {
|
||||
readyToSearch = false;
|
||||
setResultPresentModel(GAME_DETAIL_MODEL);
|
||||
}
|
||||
// TODO: add new history
|
||||
dao.add(searchKey);
|
||||
} else {
|
||||
Toast.makeText(SearchActivity.this, "请输入搜索内容",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
searchButton = (TextView) findViewById(R.id.btnSearch);
|
||||
searchCancel = (ImageView) findViewById(R.id.ivDeleteText);
|
||||
searchBack = (RelativeLayout) findViewById(R.id.btnGoBack);
|
||||
|
||||
if (isFromHome && !TextUtils.isEmpty(hint)) {
|
||||
searchInput.setText(hint);
|
||||
searchInput.setSelection(searchInput.getText().length());
|
||||
} else if (!TextUtils.isEmpty(hint)) {
|
||||
searchInput.setHint(hint);
|
||||
} else {
|
||||
searchInput.setHint("搜索游戏...");
|
||||
}
|
||||
|
||||
searchButton.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
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) {
|
||||
if (!TextUtils.isEmpty(hint)) {
|
||||
searchInput.setText(hint);
|
||||
searchInput
|
||||
.setSelection(searchInput.getText().length());
|
||||
searchKey = searchInput.getHint().toString();
|
||||
}
|
||||
}
|
||||
if (!TextUtils.isEmpty(searchKey)) {
|
||||
if (readyToSearch) {
|
||||
readyToSearch = false;
|
||||
setResultPresentModel(GAME_DETAIL_MODEL);
|
||||
}
|
||||
// TODO: add new history
|
||||
dao.add(searchKey);
|
||||
} else {
|
||||
Toast.makeText(SearchActivity.this, "请输入搜索内容",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
searchCancel.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
searchCancel.setVisibility(View.GONE);
|
||||
searchInput.setText("");
|
||||
}
|
||||
|
||||
});
|
||||
searchInput.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count,
|
||||
int after) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before,
|
||||
int count) {
|
||||
if (s.length() > 0) {
|
||||
searchCancel.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
searchCancel.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
searchKey = s.toString().trim();
|
||||
if (searchKey.length() < 1) {
|
||||
setResultPresentModel(SEARCH_HISTORY_MODEL);
|
||||
} else {
|
||||
setResultPresentModel(GAME_LIST_MODEL);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
searchBack.setOnClickListener(new OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void setResultPresentModel(int model) {
|
||||
FragmentTransaction transaction = fragmentManager.beginTransaction();
|
||||
hideFragments(transaction);
|
||||
switch (model) {
|
||||
case GAME_DETAIL_MODEL:
|
||||
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;
|
||||
|
||||
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(dao);
|
||||
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;
|
||||
}
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
private void hideFragments(FragmentTransaction transaction) {
|
||||
if (game_detail_fragment != null) {
|
||||
transaction.hide(game_detail_fragment);
|
||||
}
|
||||
if (game_list_fragment != null) {
|
||||
transaction.hide(game_list_fragment);
|
||||
}
|
||||
if (search_history_fragment != null) {
|
||||
transaction.hide(search_history_fragment);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user