Improved SearchBar

This commit is contained in:
seven332 committed 2015-08-25 12:05:35 +08:00
1 parent 98e177c58a
commit 4fe2c1ace3
3 files changed
+104 -51

No files matched your search

@@ -38,6 +38,7 @@ import android.widget.Toast;
import com.hippo.animation.SimpleAnimatorListener;
import com.hippo.conaco.Conaco;
import com.hippo.drawable.AddDeleteDrawable;
import com.hippo.drawable.DrawerArrowDrawable;
import com.hippo.effect.ViewTransition;
import com.hippo.ehviewer.Constants;
import com.hippo.ehviewer.EhApplication;
@@ -83,13 +84,13 @@ import com.hippo.yorozuya.ViewUtils;
import java.util.List;
// TODO remeber the data in ContentHelper after screen dirction change
// TODO Must refresh when change source
// TODO disable click action when animating
// TODO Dim when expand search list
public final class GalleryListScene extends Scene implements ListSearchBar.Helper,
View.OnClickListener, SearchLayout.SearhLayoutHelper, EasyRecyclerView.OnItemClickListener,
Messenger.Receiver, DrawerProvider, AppbarRecyclerView.Helper, FabLayout.OnExpandListener {
Messenger.Receiver, DrawerProvider, AppbarRecyclerView.Helper, FabLayout.OnExpandListener,
SearchBar.OnStateChangeListener {
public static final String KEY_MODE = "mode";
@@ -152,6 +153,9 @@ public final class GalleryListScene extends Scene implements ListSearchBar.Helpe
private boolean mLockLeft = false;
private boolean mLockRight = false;
private DrawerArrowDrawable mLeftDrawable;
private AddDeleteDrawable mRightDrawable;
private SimpleDialog.OnCreateCustomViewListener mGoToCreateCustomViewListener =
new SimpleDialog.OnCreateCustomViewListener() {
@Override
@@ -294,6 +298,12 @@ public final class GalleryListScene extends Scene implements ListSearchBar.Helpe
mSearchLayout.setAction2Text(mResources.getString(R.string.search_mode));
mSearchLayout.setHelper(this);
mLeftDrawable = new DrawerArrowDrawable(getContext());
mRightDrawable = new AddDeleteDrawable(getContext());
mSearchBar.setLeftDrawable(mLeftDrawable);
mSearchBar.setRightDrawable(mRightDrawable);
mSearchBar.setOnStateChangeListener(this);
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mSearchBar.getLayoutParams();
ViewUtils.measureView(mSearchBar, 200, ViewGroup.LayoutParams.WRAP_CONTENT);
int fitPaddingTop = mSearchBar.getMeasuredHeight() + lp.topMargin;
@@ -642,19 +652,21 @@ public final class GalleryListScene extends Scene implements ListSearchBar.Helpe
}
@Override
public void onClickMenu() {
mActivity.toggleDrawer();
public void onClickLeftIcon() {
if (mSearchBar.getState() == SearchBar.STATE_NORMAL) {
mActivity.toggleDrawer();
} else {
onBackPressed();
}
}
@Override
public void onClickArrow() {
onBackPressed();
}
@Override
public void onClickAdvanceSearch() {
if (mState == STATE_NORMAL) {
public void onClickRightIcon() {
if (mSearchBar.getState() == SearchBar.STATE_NORMAL) {
setState(STATE_SEARCH);
} else {
// Clear
mSearchBar.setText("");
}
}
@@ -824,6 +836,29 @@ public final class GalleryListScene extends Scene implements ListSearchBar.Helpe
}
}
@Override
public void onStateChange(int newState, int oldState, boolean animation) {
switch (oldState) {
default:
case SearchBar.STATE_NORMAL:
mLeftDrawable.setArrow(animation ? ANIMATE_TIME : 0);
mRightDrawable.setDelete(animation ? ANIMATE_TIME : 0);
break;
case SearchBar.STATE_SEARCH:
if (newState == SearchBar.STATE_NORMAL) {
mLeftDrawable.setMenu(animation ? ANIMATE_TIME : 0);
mRightDrawable.setAdd(animation ? ANIMATE_TIME : 0);
}
break;
case SearchBar.STATE_SEARCH_LIST:
if (newState == STATE_NORMAL) {
mLeftDrawable.setMenu(animation ? ANIMATE_TIME : 0);
mRightDrawable.setAdd(animation ? ANIMATE_TIME : 0);
}
break;
}
}
private class AddToQuickSearchHelper implements SimpleDialog.OnCreateCustomViewListener,
SimpleDialog.OnClickListener {
@@ -37,11 +37,12 @@ import com.hippo.scene.Announcer;
import com.hippo.scene.AppbarScene;
import com.hippo.scene.Scene;
import com.hippo.scene.SimpleDialog;
import com.hippo.yorozuya.LayoutUtils;
import com.hippo.yorozuya.Messenger;
import com.hippo.yorozuya.ViewUtils;
public class ModifyQuickSearchScene extends AppbarScene implements SearchLayout.SearhLayoutHelper,
SearchBar.Helper {
SearchBar.Helper {
public static final String KEY_QUICK_SEARCH = "quick_search";
@@ -71,6 +72,9 @@ public class ModifyQuickSearchScene extends AppbarScene implements SearchLayout.
mSearchBar.setState(SearchBar.STATE_SEARCH, false);
mSearchBar.setHelper(this);
mSearchBar.setEditTextHint(resources.getString(R.string.keyword));
mSearchBar.setLeftIconVisibility(View.GONE);
mSearchBar.setRightDrawable(resources.getDrawable(R.drawable.ic_close_x24));
mSearchBar.setEditTextMargin(LayoutUtils.dp2pix(getContext(), 16), LayoutUtils.dp2pix(getContext(), 56));
ViewUtils.measureView(mSearchBar, 200, ViewGroup.LayoutParams.WRAP_CONTENT);
mSearchLayout.setFitPaddingTop(mSearchBar.getMeasuredHeight() +
@@ -234,18 +238,13 @@ public class ModifyQuickSearchScene extends AppbarScene implements SearchLayout.
}
@Override
public void onClickMenu() {
public void onClickLeftIcon() {
}
@Override
public void onClickArrow() {
}
@Override
public void onClickAdvanceSearch() {
public void onClickRightIcon() {
mSearchBar.setText("");
}
@Override
@@ -21,6 +21,7 @@ import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
@@ -41,8 +42,6 @@ import android.widget.TextView;
import com.hippo.animation.SimpleAnimatorListener;
import com.hippo.cardsalon.CardView;
import com.hippo.drawable.AddDeleteDrawable;
import com.hippo.drawable.DrawerArrowDrawable;
import com.hippo.effect.ViewTransition;
import com.hippo.ehviewer.R;
import com.hippo.widget.SimpleImageView;
@@ -82,14 +81,12 @@ public class SearchBar extends CardView implements View.OnClickListener,
private ViewTransition mViewTransition;
private DrawerArrowDrawable mDrawerArrowDrawable;
private AddDeleteDrawable mAddDeleteDrawable;
private SearchDatabase mSearchDatabase;
private List<String> mSuggestionList;
private ArrayAdapter mSuggestionAdapter;
private Helper mHelper;
private OnStateChangeListener mOnStateChangeListener;
private boolean mInAnimation = false;
@@ -120,13 +117,8 @@ public class SearchBar extends CardView implements View.OnClickListener,
mViewTransition = new ViewTransition(mTitleTextView, mEditText);
mDrawerArrowDrawable = new DrawerArrowDrawable(getContext());
mAddDeleteDrawable = new AddDeleteDrawable(getContext());
mTitleTextView.setOnClickListener(this);
mMenuButton.setDrawable(mDrawerArrowDrawable);
mMenuButton.setOnClickListener(this);
mActionButton.setDrawable(mAddDeleteDrawable);
mActionButton.setOnClickListener(this);
mEditText.setSearchEditTextListener(this);
mEditText.setOnEditorActionListener(this);
@@ -171,6 +163,10 @@ public class SearchBar extends CardView implements View.OnClickListener,
mHelper = helper;
}
public void setOnStateChangeListener(OnStateChangeListener listener) {
mOnStateChangeListener = listener;
}
public void setText(String text) {
mEditText.setText(text);
}
@@ -179,22 +175,37 @@ public class SearchBar extends CardView implements View.OnClickListener,
return mEditText.getText().toString();
}
public void setLeftDrawable(Drawable drawable) {
mMenuButton.setDrawable(drawable);
}
public void setRightDrawable(Drawable drawable) {
mActionButton.setDrawable(drawable);
}
public void setLeftIconVisibility(int visibility) {
mMenuButton.setVisibility(visibility);
}
public void setRightIconVisibility(int visibility) {
mActionButton.setVisibility(visibility);
}
public void setEditTextMargin(int left, int right) {
MarginLayoutParams lp = (MarginLayoutParams) mEditText.getLayoutParams();
lp.leftMargin = left;
lp.rightMargin = right;
mEditText.setLayoutParams(lp);
}
@Override
public void onClick(View v) {
if (v == mTitleTextView) {
mHelper.onClickTitle();
} else if (v == mMenuButton) {
if (mState == STATE_NORMAL) {
mHelper.onClickMenu();
} else {
mHelper.onClickArrow();
}
mHelper.onClickLeftIcon();
} else if (v == mActionButton) {
if (mState == STATE_NORMAL) {
mHelper.onClickAdvanceSearch();
} else {
mEditText.setText("");
}
mHelper.onClickRightIcon();
}
}
@@ -228,38 +239,42 @@ public class SearchBar extends CardView implements View.OnClickListener,
case STATE_NORMAL:
mViewTransition.showView(1, animation);
mEditText.requestFocus();
mDrawerArrowDrawable.setArrow(animation ? ANIMATE_TIME : 0);
mAddDeleteDrawable.setDelete(animation ? ANIMATE_TIME : 0);
if (state == STATE_SEARCH_LIST) {
showImeAndSuggestionsList(animation);
}
if (mOnStateChangeListener != null) {
mOnStateChangeListener.onStateChange(state, oldState, animation);
}
break;
case STATE_SEARCH:
if (state == STATE_NORMAL) {
mViewTransition.showView(0, animation);
mDrawerArrowDrawable.setMenu(animation ? ANIMATE_TIME : 0);
mAddDeleteDrawable.setAdd(animation ? ANIMATE_TIME : 0);
} else if (state == STATE_SEARCH_LIST) {
showImeAndSuggestionsList(animation);
}
if (mOnStateChangeListener != null) {
mOnStateChangeListener.onStateChange(state, oldState, animation);
}
break;
case STATE_SEARCH_LIST:
hideImeAndSuggestionsList(animation);
if (state == STATE_NORMAL) {
mViewTransition.showView(0, animation);
mDrawerArrowDrawable.setMenu(animation ? ANIMATE_TIME : 0);
mAddDeleteDrawable.setAdd(animation ? ANIMATE_TIME : 0);
}
if (mOnStateChangeListener != null) {
mOnStateChangeListener.onStateChange(state, oldState, animation);
}
break;
}
}
}
private void showImeAndSuggestionsList() {
public void showImeAndSuggestionsList() {
showImeAndSuggestionsList(true);
}
private void showImeAndSuggestionsList(boolean animation) {
public void showImeAndSuggestionsList(boolean animation) {
// Show ime
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, 0);
@@ -408,11 +423,15 @@ public class SearchBar extends CardView implements View.OnClickListener,
public interface Helper {
void onClickTitle();
void onClickMenu();
void onClickArrow();
void onClickAdvanceSearch();
void onClickLeftIcon();
void onClickRightIcon();
void onSearchEditTextClick();
void onApplySearch(String query);
void onBackPressed();
}
public interface OnStateChangeListener {
void onStateChange(int newState, int oldState, boolean animation);
}
}