实用地更改AppBar的高度和浮动搜索视图

时间:2019-05-02 03:27:25

标签: android-appbarlayout

FloatingSearchView内实现了AppBarLayout,就像Google Play应用程序一样。我面临着一个挑战,那就是搜索时建议无法正确显示。 这是我的xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:elevation="0dp" >

    <com.arlib.floatingsearchview.FloatingSearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:floatingSearch_close_search_on_keyboard_dismiss="true"
        app:floatingSearch_leftActionMode="showHamburger"
        app:floatingSearch_menu="@menu/main"
        app:floatingSearch_searchBarMarginLeft="5dp"
        app:floatingSearch_searchBarMarginRight="5dp"
        app:floatingSearch_searchBarMarginTop="5dp"
        app:floatingSearch_searchHint="@string/main_title"
        app:floatingSearch_showSearchKey="false"
        app:floatingSearch_suggestionsListAnimDuration="250" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabPaddingEnd="16dp"
        app:tabPaddingStart="16dp"
        app:tabTextAppearance="@style/TabLayoutStyle" />

</android.support.design.widget.AppBarLayout>

<com.oyonde.myapp.components.CustomViewPager
    android:id="@+id/mainViewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

在Java代码中,我尝试在搜索时执行此操作

private void searchViewDisplay(boolean isSearching) {        
    FloatingSearchView mSearchView = findViewById(R.id.search_view);
    AppBarLayout appbarLayout = findViewById(R.id.appbarLayout);
    TabLayout tabLayout = findViewById(R.id.tabs);
    ViewPager viewPager = findViewById(R.id.mainViewPager);

    ViewGroup.LayoutParams appbarParams = appbarLayout.getLayoutParams();
    ViewGroup.LayoutParams searchParams = mSearchView.getLayoutParams();

    if (isSearching) {
        tabLayout.setVisibility(View.GONE);
        viewPager.setVisibility(View.GONE);

        AppBarLayout.LayoutParams aprams = new AppBarLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        appbarParams.height = 500;
        searchParams.height = 500;
    } else {
        tabLayout.setVisibility(View.VISIBLE);
        viewPager.setVisibility(View.VISIBLE);

        appbarParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        searchParams.height = 120;
    }

    appbarLayout.setLayoutParams(appbarParams);
    mSearchView.setLayoutParams(searchParams);
}

由于我能够隐藏诸如viewpager之类的某些元素,因此希望它能正常工作对您有所帮助。

到目前为止,我尝试在appbarlayout中使用xml使用match_parent高度,并且浮动搜索视图相同,并且根据需要显示了建议,但是现在我希望实用地更改其高度。

1 个答案:

答案 0 :(得分:1)

我认为您尝试用Java代码实用地解决问题不是解决此问题的最佳方法。尝试尽可能多地使用xml。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <View
        android:id="@+id/header_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.oyonde.myapp.components.CustomViewPager
            android:id="@+id/mainViewPager"
            android:layout_marginTop="105dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="60dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:elevation="0dp">

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="0dp"
                app:tabGravity="fill"
                app:tabMode="scrollable"
                app:tabPaddingEnd="16dp"
                app:tabPaddingStart="16dp"
                app:tabTextAppearance="@style/TabLayoutStyle" />

        </android.support.design.widget.AppBarLayout>

        <com.arlib.floatingsearchview.FloatingSearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:floatingSearch_close_search_on_keyboard_dismiss="true"
            app:floatingSearch_leftActionMode="showHamburger"
            app:floatingSearch_menu="@menu/main"
            app:floatingSearch_searchBarMarginLeft="5dp"
            app:floatingSearch_searchBarMarginRight="5dp"
            app:floatingSearch_searchBarMarginTop="5dp"
            app:floatingSearch_searchHint="@string/main_title"
            app:floatingSearch_showSearchKey="false"
            app:floatingSearch_suggestionsListAnimDuration="250" />

    </FrameLayout>

</android.support.design.widget.CoordinatorLayout>

在这里使用FrameLayout应该可以解决您的问题