您好我有一个具体的问题。 我正在我的项目中使用库floatingsearchview(https://github.com/arimorty/floatingsearchview)。
我尝试使用设计支持库(滚动时折叠的AppBarLayout /工具栏)实现像Google工具栏这样的Google Play商店。
Searchview位于工具栏内,滚动和消失完美无瑕(当高度设置为匹配工具栏高度时)。然而,当你想在searchview下显示任何结果时,需要将searchview设置为height:match_parent,这将扩展工具栏以填满整个屏幕......
关于如何这样做的任何意见?
这就是它在Google Play中的运作方式(但您也应该可以在Google Play商店应用中进行测试)
searchview1 http://i67.tinypic.com/33col1c.png searchview2 http://i65.tinypic.com/2lmxkp0.png
答案 0 :(得分:1)
来自图书馆的作者。
View
的高度决定了建议的高度。这意味着,您可以为View
设置任何高度,但如果它低于完整高度则不会看起来很好,因为建议不会扩展到整个屏幕&#39 ;高度。
所以,不,不可能在Toolbar
内使用它。
答案 1 :(得分:0)
我设法通过使用这样的FrameLayout来实现这一点:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
layout="@layout/toolbar" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
style="@style/AppTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<com.arlib.floatingsearchview.FloatingSearchView
android:id="@+id/floating_search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
background="#5E5C62"
app:floatingSearch_searchBarMarginLeft="8dp"
app:floatingSearch_searchBarMarginRight="8dp"
app:floatingSearch_showMenuAction="true"
app:floatingSearch_searchHint="Search…"
app:floatingSearch_showSearchHintWhenNotFocused="true"
app:floatingSearch_showVoiceInput="false"
app:floatingSearch_showOverFlowMenu="false"
app:floatingSearch_hideOverflowMenuWhenFocused="false"
app:floatingSearch_showSearchKey="false"
app:floatingSearch_dismissOnOutsideTouch="true" />
</FrameLayout>
这可能不是最好的设置,但它适用于我并正确显示建议。
答案 2 :(得分:0)
对于我的工作,编辑资源xml并为FloatingSearchView添加新的自定义行为
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.HomeActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="70dp"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/searchContainer"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.arlib.floatingsearchview.FloatingSearchView
android:id="@+id/floating_search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatingSearch_close_search_on_keyboard_dismiss="true"
app:floatingSearch_leftActionMode="showHome"
app:floatingSearch_menu="@menu/menu_search"
app:floatingSearch_searchBarMarginLeft="@dimen/search_view_inset"
app:layout_behavior="com.example.utils.SearchBehavior"
app:floatingSearch_searchBarMarginRight="@dimen/search_view_inset"
app:floatingSearch_searchBarMarginTop="5dp"
app:floatingSearch_searchHint="Search..."
app:floatingSearch_showSearchKey="true"
app:floatingSearch_suggestionsListAnimDuration="250" />
</android.support.design.widget.CoordinatorLayout>
然后创建提到属性app的自定义类:FloatingSearchView标签的layout_behavior:
public class SearchBehavior extends
CoordinatorLayout.Behavior<FloatingSearchView> {
private AppBarLayout mAppBarLayout;
private android.support.design.widget.AppBarLayout.Behavior
mAppBarLayoutBehavior;
private ValueAnimator mValueAnimator;
private FloatingSearchView mSearchView;
private boolean isScrolling;
public SearchBehavior() {
}
public SearchBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean layoutDependsOn(CoordinatorLayout parent,
FloatingSearchView child, View dependency) {
if (dependency instanceof AppBarLayout) {
this.mSearchView = child;
this.mAppBarLayout = (AppBarLayout) dependency;
CoordinatorLayout.LayoutParams params =
(CoordinatorLayout.LayoutParams) this.mAppBarLayout.getLayoutParams();
if (Build.VERSION.SDK_INT >= 21) {
this.mAppBarLayout.setStateListAnimator((StateListAnimator)
null);
}
this.mAppBarLayoutBehavior =
(android.support.design.widget.AppBarLayout.Behavior)
params.getBehavior();
return true;
} else {
return super.layoutDependsOn(parent, child, dependency);
}
}
public boolean onDependentViewChanged(CoordinatorLayout parent,
FloatingSearchView child, View dependency) {
if (dependency instanceof AppBarLayout) {
this.mSearchView.setTranslationY(dependency.getY());
return true;
} else {
return super.onDependentViewChanged(parent, child, dependency);
}
}
public void onNestedPreScroll(CoordinatorLayout parent,
FloatingSearchView child, View target, int dx, int dy, int[] consumed)
{
if (dy < 0 && dy <= -10 && !this.isScrolling) {
this.isScrolling = true;
if (this.needsToAdjustSearchBar() &&
!this.isRunningAnimation()) {
int offset = this.getMinExpandHeight();
this.getValueAnimator(parent, child, -offset).start();
}
}
}
public void onNestedScroll(CoordinatorLayout coordinatorLayout,
FloatingSearchView child, View target, int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed,
dyConsumed, dxUnconsumed, dyUnconsumed);
}
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout,
FloatingSearchView child, View target) {
this.isScrolling = false;
}
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
FloatingSearchView child, View directTargetChild, View target, int
nestedScrollAxes) {
return nestedScrollAxes == 2 ||
super.onStartNestedScroll(coordinatorLayout, child, directTargetChild,
target, nestedScrollAxes);
}
private int getStatusBarHeight() {
if (Build.VERSION.SDK_INT > 19) {
return 0;
} else {
int result = 0;
int resourceId = this.mSearchView.getContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = this.mSearchView.getContext().getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
private ValueAnimator getValueAnimator(CoordinatorLayout parent, FloatingSearchView searchView, int offset) {
if (this.mValueAnimator == null) {
this.mValueAnimator = ValueAnimator.ofInt(new int[0]);
} else if (this.mValueAnimator.isRunning()) {
return this.mValueAnimator;
}
this.mValueAnimator.setInterpolator(new DecelerateInterpolator());
this.mValueAnimator.setIntValues(new int[]{this.mAppBarLayoutBehavior.getTopAndBottomOffset(), offset});
return this.mValueAnimator;
}
private boolean isRunningAnimation() {
return this.mValueAnimator != null && this.mValueAnimator.isRunning();
}
private boolean needsToAdjustSearchBar() {
float y = (float) Math.abs(this.mAppBarLayoutBehavior.getTopAndBottomOffset());
return y > (float) this.getMinExpandHeight();
}
private int getMinExpandHeight() {
return Build.VERSION.SDK_INT >= 16 ?
this.mAppBarLayout.getTotalScrollRange() -
this.mSearchView.getMinimumHeight() - this.getStatusBarHeight() / 2 :
this.mAppBarLayout.getTotalScrollRange() - this.mSearchView.getHeight() -
this.getStatusBarHeight() / 2;
}
}