Update search in gallery list

This commit is contained in:
seven332 committed 2015-04-25 14:36:13 +08:00
1 parent 10ca8b352c
commit fbeffe53f6
16 files changed
+286 -116

No files matched your search

@@ -19,7 +19,6 @@ import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import com.hippo.effect.ViewTransition;
import com.hippo.ehviewer.R;
@@ -33,12 +32,12 @@ import com.hippo.util.Log;
import com.hippo.util.ViewUtils;
public
class GalleryListScene extends Scene implements SearchBar.Helper,
ViewTreeObserver.OnGlobalLayoutListener {
class GalleryListScene extends Scene implements SearchBar.Helper {
private final static int PAGE_INDEX_SEARCH = 0;
private final static int PAGE_INDEX_LIST = 1;
private final static int PAGE_NUMBER = 2;
private final static int STATE_NORMAL = 0;
private final static int STATE_SIMPLE_SEARCH = 1;
private final static int STATE_SEARCH = 2;
private final static int STATE_SEARCH_SHOW_LIST = 3;
private ContentActivity mActivity;
private Resources mResources;
@@ -50,6 +49,8 @@ class GalleryListScene extends Scene implements SearchBar.Helper,
private ViewTransition mViewTransition;
private int mState = STATE_NORMAL;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -72,8 +73,7 @@ class GalleryListScene extends Scene implements SearchBar.Helper,
// Search Layout
mSearchLayout.setFitPaddingTop(mSearchBar.getMeasuredHeight() +
(int) (2 * mResources.getDimension(R.dimen.search_bar_padding_vertical)));
mResources.getDimensionPixelOffset(R.dimen.search_bar_padding_vertical));
// TEST
mContentLayout.showText("四姑拉斯基");
@@ -81,12 +81,37 @@ class GalleryListScene extends Scene implements SearchBar.Helper,
@Override
protected void onGetFitPaddingBottom(int b) {
// TODO
mSearchLayout.setFitPaddingBottom(b);
}
@Override
public void onGlobalLayout() {
public void onBackPressed() {
switch (mState) {
case STATE_NORMAL:
super.onBackPressed();
break;
case STATE_SIMPLE_SEARCH:
mSearchBar.setInNormalMode();
mState = STATE_NORMAL;
break;
case STATE_SEARCH:
mViewTransition.showFirstView();
mSearchBar.setInNormalMode();
mState = STATE_NORMAL;
break;
case STATE_SEARCH_SHOW_LIST:
mSearchBar.hideImeAndSuggestionsList();
mState = STATE_SEARCH;
break;
}
}
@Override
public void onClickTitle() {
if (mState == STATE_NORMAL) {
mState = STATE_SIMPLE_SEARCH;
mSearchBar.setInEditMode(true);
}
}
@Override
@@ -95,14 +120,35 @@ class GalleryListScene extends Scene implements SearchBar.Helper,
}
@Override
public void onClickAction() {
mViewTransition.showSecondView();
public void onClickArrow() {
onBackPressed();
}
@Override
public void onClickAdvanceSearch() {
if (mState == STATE_NORMAL) {
mViewTransition.showSecondView();
mSearchLayout.scrollSearchContainerToTop();
mSearchBar.setInEditMode(false);
mState = STATE_SEARCH;
}
}
@Override
public void onSearchEditTextClick() {
if (mState == STATE_SEARCH) {
mSearchBar.showImeAndSuggestionsList();
mState = STATE_SEARCH_SHOW_LIST;
}
}
@Override
public void onApplySearch(String query) {
Log.d("onApplySearch " + query);
mSearchDatabase.addQuery(query);
mViewTransition.showFirstView();
mSearchBar.setInNormalMode();
mState = STATE_NORMAL;
}
}
@@ -47,10 +47,12 @@ import com.hippo.util.ViewUtils;
import com.hippo.widget.SimpleImageView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SearchBar extends CardView implements View.OnClickListener,
TextView.OnEditorActionListener, TextWatcher {
TextView.OnEditorActionListener, TextWatcher,
SearchEditText.SearchEditTextListener {
private static final long ANIMATION_TIME = 300;
@@ -100,7 +102,6 @@ public class SearchBar extends CardView implements View.OnClickListener,
setRadius(UiUtils.dp2pix(context, 2));
setCardElevation(UiUtils.dp2pix(context, 2));
setCardBackgroundColor(Color.WHITE);
setOnClickListener(this);
LayoutInflater.from(context).inflate(R.layout.widget_search_bar, this);
mMenuButton = (SimpleImageView) findViewById(R.id.search_menu);
@@ -112,11 +113,12 @@ public class SearchBar extends CardView implements View.OnClickListener,
mDrawerArrowDrawable = new DrawerArrowDrawable(getContext());
mAddDeleteDrawable = new AddDeleteDrawable(getContext());
mTitleTextView.setOnClickListener(this);
mMenuButton.setDrawable(mDrawerArrowDrawable);
mMenuButton.setOnClickListener(this);
mActionButton.setDrawable(mAddDeleteDrawable);
mActionButton.setOnClickListener(this);
mEditText.setSearchBar(this);
mEditText.setSearchEditTextListener(this);
mEditText.setOnEditorActionListener(this);
mEditText.addTextChangedListener(this);
@@ -130,9 +132,7 @@ public class SearchBar extends CardView implements View.OnClickListener,
String prefix = mEditText.getText().toString();
String[] suggestions = mSearchDatabase.getSuggestions(prefix);
mSuggestionList.clear();
for (String suggestion : suggestions) {
mSuggestionList.add(suggestion);
}
Collections.addAll(mSuggestionList, suggestions);
mSuggestionAdapter.notifyDataSetChanged();
}
@@ -142,11 +142,11 @@ public class SearchBar extends CardView implements View.OnClickListener,
@Override
public void onClick(View v) {
if (v == this) {
setInEditMode();
if (v == mTitleTextView) {
mHelper.onClickTitle();
} else if (v == mMenuButton) {
if (mInEditMode) {
setInNormalMode();
mHelper.onClickArrow();
} else {
mHelper.onClickMenu();
}
@@ -154,7 +154,7 @@ public class SearchBar extends CardView implements View.OnClickListener,
if (mInEditMode) {
mEditText.setText("");
} else {
mHelper.onClickAction();
mHelper.onClickAdvanceSearch();
}
}
}
@@ -173,26 +173,77 @@ public class SearchBar extends CardView implements View.OnClickListener,
return false;
}
public void setInEditMode() {
public void setInEditMode(boolean showList) {
if (!mInEditMode) {
mInEditMode = true;
setClickable(false);
ViewUtils.setVisibility(mTitleTextView, View.GONE);
ViewUtils.setVisibility(mEditText, View.VISIBLE);
mEditText.requestFocus();
// show ime
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
// update suggestion
updateSuggestions();
// start animator
ObjectAnimator oa1 = ObjectAnimator.ofFloat(mDrawerArrowDrawable, "progress", 0f, 1f);
oa1.setDuration(ANIMATION_TIME);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(mAddDeleteDrawable, "progress", 0f, 1f);
oa2.setDuration(ANIMATION_TIME);
ObjectAnimator oa3 = ObjectAnimator.ofFloat(this, "progress", 0f, 1f);
oa3.setDuration(ANIMATION_TIME);
oa3.addListener(new SimpleAnimatorListener() {
if (mDrawerArrowDrawable.getProgress() != 1f) {
ObjectAnimator oa1 = ObjectAnimator.ofFloat(mDrawerArrowDrawable, "progress", 0f, 1f);
oa1.setDuration(ANIMATION_TIME);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
oa1.setAutoCancel(true);
}
oa1.start();
}
if (mAddDeleteDrawable.getProgress() != 1f) {
ObjectAnimator oa2 = ObjectAnimator.ofFloat(mAddDeleteDrawable, "progress", 0f, 1f);
oa2.setDuration(ANIMATION_TIME);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
oa2.setAutoCancel(true);
}
oa2.start();
}
// Show list if needed
if (showList) {
showImeAndSuggestionsList();
}
}
}
public void setInNormalMode() {
if (mInEditMode) {
mInEditMode = false;
setOnClickListener(this);
ViewUtils.setVisibility(mTitleTextView, View.VISIBLE);
ViewUtils.setVisibility(mEditText, View.GONE);
// start animator
if (mDrawerArrowDrawable.getProgress() != 0f) {
ObjectAnimator oa1 = ObjectAnimator.ofFloat(mDrawerArrowDrawable, "progress", 1f, 0f);
oa1.setDuration(ANIMATION_TIME);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
oa1.setAutoCancel(true);
}
oa1.start();
}
if (mAddDeleteDrawable.getProgress() != 0f) {
ObjectAnimator oa2 = ObjectAnimator.ofFloat(mAddDeleteDrawable, "progress", 1f, 0f);
oa2.setDuration(ANIMATION_TIME);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
oa2.setAutoCancel(true);
}
oa2.start();
}
// Hide ime and suggestions list
hideImeAndSuggestionsList();
}
}
public void showImeAndSuggestionsList() {
// Show ime
mEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, 0);
// update suggestion for show suggestions list
updateSuggestions();
// Show suggestions list
if (mProgress != 1f) {
ObjectAnimator oa = ObjectAnimator.ofFloat(this, "progress", 0f, 1f);
oa.setDuration(ANIMATION_TIME);
oa.addListener(new SimpleAnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
ViewUtils.setVisibility(mList, View.VISIBLE);
@@ -205,33 +256,21 @@ public class SearchBar extends CardView implements View.OnClickListener,
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
oa1.setAutoCancel(true);
oa2.setAutoCancel(true);
oa3.setAutoCancel(true);
oa.setAutoCancel(true);
}
oa1.start();
oa2.start();
oa3.start();
oa.start();
}
}
public void setInNormalMode() {
if (mInEditMode) {
mInEditMode = false;
setOnClickListener(this);
ViewUtils.setVisibility(mTitleTextView, View.VISIBLE);
ViewUtils.setVisibility(mEditText, View.GONE);
// hide ime
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getWindowToken(), 0);
// start animator
ObjectAnimator oa1 = ObjectAnimator.ofFloat(mDrawerArrowDrawable, "progress", 1f, 0f);
oa1.setDuration(ANIMATION_TIME);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(mAddDeleteDrawable, "progress", 1f, 0f);
oa2.setDuration(ANIMATION_TIME);
ObjectAnimator oa3 = ObjectAnimator.ofFloat(this, "progress", 1f, 0f);
oa3.setDuration(ANIMATION_TIME);
oa3.addListener(new SimpleAnimatorListener() {
public void hideImeAndSuggestionsList() {
// Hide ime
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getWindowToken(), 0);
// Hide suggestions list
if (mProgress != 0f) {
ObjectAnimator oa = ObjectAnimator.ofFloat(this, "progress", 1f, 0f);
oa.setDuration(ANIMATION_TIME);
oa.addListener(new SimpleAnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
mInAnimation = true;
@@ -244,13 +283,9 @@ public class SearchBar extends CardView implements View.OnClickListener,
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
oa1.setAutoCancel(true);
oa2.setAutoCancel(true);
oa3.setAutoCancel(true);
oa.setAutoCancel(true);
}
oa1.start();
oa2.start();
oa3.start();
oa.start();
}
}
@@ -312,9 +347,23 @@ public class SearchBar extends CardView implements View.OnClickListener,
updateSuggestions();
}
@Override
public void onClick() {
mHelper.onSearchEditTextClick();
}
@Override
public void onBackPressed() {
mHelper.onBackPressed();
}
public interface Helper {
void onClickTitle();
void onClickMenu();
void onClickAction();
void onClickArrow();
void onClickAdvanceSearch();
void onSearchEditTextClick();
void onApplySearch(String query);
void onBackPressed();
}
}
@@ -97,7 +97,7 @@ public class SearchDatabase {
}
public void deleteQuery(final String query) {
mDatabase.delete(TABLE_SUGGESTIONS, COLUMN_DATE + "=?", new String[]{query});
mDatabase.delete(TABLE_SUGGESTIONS, COLUMN_QUERY + "=?", new String[]{query});
}
public void clearQuery() {
@@ -19,11 +19,12 @@ import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.EditText;
public class SearchEditText extends EditText {
private SearchBar mSearchBar;
private SearchEditTextListener mListener;
public SearchEditText(Context context) {
super(context);
@@ -37,8 +38,8 @@ public class SearchEditText extends EditText {
super(context, attrs, defStyleAttr);
}
public void setSearchBar(SearchBar searchBar) {
mSearchBar = searchBar;
public void setSearchEditTextListener(SearchEditTextListener listener) {
mListener = listener;
}
@Override
@@ -58,8 +59,8 @@ public class SearchEditText extends EditText {
state.handleUpEvent(event);
}
if (event.isTracking() && !event.isCanceled()) {
if (mSearchBar != null) {
mSearchBar.setInNormalMode();
if (mListener != null) {
mListener.onBackPressed();
return true;
}
}
@@ -67,4 +68,17 @@ public class SearchEditText extends EditText {
}
return super.onKeyPreIme(keyCode, event);
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && mListener != null) {
mListener.onClick();
}
return super.onTouchEvent(event);
}
public interface SearchEditTextListener {
void onClick();
void onBackPressed();
}
}
@@ -147,8 +147,15 @@ public class SearchLayout extends FrameLayout implements CompoundButton.OnChecke
mFab.setOnClickListener(this);
mFabMarginBottomOrigin = mResources.getDimensionPixelSize(R.dimen.content_fab_margin_bottom);
mSearchPaddingTopOrigin = 0;
mSearchPaddingBottomOrigin = mResources.getDimensionPixelSize(R.dimen.content_padding_bottom);
// Search Container
mSearchPaddingTopOrigin = mSearchContainer.getPaddingTop();
mSearchPaddingBottomOrigin = mSearchContainer.getPaddingBottom() +
mResources.getDimensionPixelSize(R.dimen.content_padding_bottom);
mSearchContainer.setPadding( mSearchContainer.getPaddingLeft(),
mSearchContainer.getPaddingTop(),
mSearchContainer.getPaddingRight(),
mSearchPaddingBottomOrigin);
}
public void setFitPaddingTop(int fitPaddingTop) {
@@ -160,16 +167,16 @@ public class SearchLayout extends FrameLayout implements CompoundButton.OnChecke
);
}
public void setFitPaddingBottom(int paddingBottom) {
public void setFitPaddingBottom(int fitPaddingBottom) {
mSearchContainer.setPadding(
mSearchContainer.getPaddingLeft(),
mSearchContainer.getPaddingTop(),
mSearchContainer.getPaddingRight(),
mSearchPaddingBottomOrigin + paddingBottom
mSearchPaddingBottomOrigin + fitPaddingBottom
);
LayoutParams lp = (LayoutParams) mFab.getLayoutParams();
lp.bottomMargin = mFabMarginBottomOrigin + paddingBottom;
lp.bottomMargin = mFabMarginBottomOrigin + fitPaddingBottom;
mFab.setLayoutParams(lp);
}
@@ -177,6 +184,10 @@ public class SearchLayout extends FrameLayout implements CompoundButton.OnChecke
mHelper = helper;
}
public void scrollSearchContainerToTop() {
mLayoutManager.scrollToPositionWithOffset(0, 0);
}
@Override
protected void dispatchSaveInstanceState(@NonNull SparseArray<Parcelable> container) {
super.dispatchSaveInstanceState(container);
@@ -494,8 +505,8 @@ public class SearchLayout extends FrameLayout implements CompoundButton.OnChecke
}
public interface SearhLayoutHelper {
public void onRequestSelectImage();
void onRequestSelectImage();
public void onRequestSearch(ListUrlBuilder lub);
void onRequestSearch(ListUrlBuilder lub);
}
}
+8 -14
View File
@@ -20,7 +20,6 @@ import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
@@ -49,6 +48,7 @@ public abstract class Scene {
private @Nullable Curtain mCurtain;
@SuppressWarnings("deprecation")
private @Nullable AbsoluteLayout mSceneView;
private int mBackgroundColor = 0xffeeeeee; // TODO Need a better to set background color
@@ -116,6 +116,7 @@ public abstract class Scene {
onCreate(savedInstanceState);
if (mSceneView == null) {
//noinspection deprecation
mSceneView = new AbsoluteLayout(getStageActivity());
initBackground(mSceneView);
}
@@ -189,6 +190,7 @@ public abstract class Scene {
protected void setContentView(int resId) {
StageActivity sa = getStageActivity();
//noinspection deprecation
mSceneView = new AbsoluteLayout(sa);
initBackground(mSceneView);
mSceneView.setBackgroundColor(mBackgroundColor);
@@ -197,6 +199,7 @@ public abstract class Scene {
protected void setContentView(View view) {
StageActivity sa = getStageActivity();
//noinspection deprecation
mSceneView = new AbsoluteLayout(sa);
initBackground(mSceneView);
mSceneView.addView(view, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
@@ -228,14 +231,13 @@ public abstract class Scene {
protected void onOut() {
}
/**
* It will be called if nav bar is transparent.
* @param b the nav bar's height
*/
protected void onGetFitPaddingBottom(int b) {
}
public View onCreateStageBar() {
return null;
}
/**
* Finds a view that was identified by the id attribute from the XML that
* was processed in {@link #onCreate}.
@@ -373,14 +375,6 @@ public abstract class Scene {
getStageActivity().startActivityForResult(intent, listener);
}
/**
* Return the FragmentManager for interacting with fragments associated
* with StageActivity.
*/
public FragmentManager getSupportFragmentManager() {
return getStageActivity().getSupportFragmentManager();
}
public interface ActivityResultListener {
void onGetResult(int resultCode, Intent data);
}
@@ -68,12 +68,6 @@ public class FloatLabelEditText extends FrameLayout {
init(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FloatLabelEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
mLabel = new TextView(context);
addView(mLabel, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
@@ -17,5 +17,5 @@ package com.hippo.widget;
public interface Hotspotable {
public void setHotspot(float x, float y);
void setHotspot(float x, float y);
}
@@ -82,12 +82,16 @@ public class SimpleImageView extends View implements Drawable.Callback {
super.onLayout(changed, left, top, right, bottom);
int width = right - left;
int height = bottom - top;
mDrawable.setBounds(getPaddingLeft(), getPaddingTop(),
width - getPaddingRight(), height - getPaddingBottom());
if (mDrawable != null) {
mDrawable.setBounds(getPaddingLeft(), getPaddingTop(),
width - getPaddingRight(), height - getPaddingBottom());
}
}
@Override
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
if (mDrawable != null) {
mDrawable.draw(canvas);
}
}
}
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.widget.recyclerview;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import com.hippo.ehviewer.R;
public class MonoRecyclerView extends EasyRecyclerView {
public MonoRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MonoRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
final TypedArray a = context
.obtainStyledAttributes(attrs, R.styleable.MonoRecyclerView);
int space = a.getDimensionPixelOffset(R.styleable.MonoRecyclerView_space, 0);
int spaceLeft = a.getDimensionPixelOffset(R.styleable.MonoRecyclerView_spaceLeft, space);
int spaceRight = a.getDimensionPixelOffset(R.styleable.MonoRecyclerView_spaceRight, space);
int spaceTop = a.getDimensionPixelOffset(R.styleable.MonoRecyclerView_spaceTop, space);
int spaceBottom = a.getDimensionPixelOffset(R.styleable.MonoRecyclerView_spaceBottom, space);
a.recycle();
int halfSpace = space / 2;
MarginItemDecoration itemDecoration = new MarginItemDecoration(halfSpace);
addItemDecoration(itemDecoration);
setPadding(spaceLeft - halfSpace, spaceTop - halfSpace,
spaceRight - halfSpace, spaceBottom - space);
setClipChildren(false);
}
}