折叠工具栏布局在点击事件上展开

时间:2017-05-26 11:51:43

标签: android android-collapsingtoolbarlayout android-appbarlayout

我有一个带标题和图标的简单工具栏。我想要的是,当单击工具栏时,它会展开并显示更多字段以使用某些过滤器进行搜索。我真正想要的是与airbnb应用程序在探索栏中具有相同的效果。

任何人都可以告诉我如何处理或提供一些提示吗?

我目前拥有的是这种布局。但是折叠工具栏会覆盖顶部的工具栏。

<?xml version="1.0" encoding="utf-8"?>
<com.ezycities.ezycities.ui.homehost.DisableableCoordinatorLayout 
    android:id="@+id/coordinator"
    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">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbarUserProfilePage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/first_grey">

        <TextView android:id="@+id/textViewUserProfilePageToolbar"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:maxLines="1"
            android:text="@string/userProfile.toolbar"
            android:textColor="@color/third_grey"
            android:textSize="20sp" />

        <ImageView android:id="@+id/imageViewUserProfilePageToolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp"
            android:layout_gravity="end"
            android:tint="@color/third_grey" />

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.AppBarLayout android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView android:id="@+id/textViewEditProfilePageLastName"
                android:layout_width="86dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="16dp"
                android:text="@string/user.edit.last_name"
                style="@style/editProfilePageTitles" />

            <TextView android:id="@+id/textViewEditProfilePageLastName2"
                android:layout_width="86dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="16dp"
                android:text="@string/user.edit.last_name"
                style="@style/editProfilePageTitles" />

            <TextView android:id="@+id/textViewEditProfilePageLastName3"
                android:layout_width="86dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="16dp"
                android:text="@string/user.edit.last_name"
                style="@style/editProfilePageTitles" />

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

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

   <android.support.v4.widget.NestedScrollView android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

   </android.support.v4.widget.NestedScrollView>

</com.ezycities.ezycities.ui.homehost.DisableableCoordinatorLayout>

当我要展开或折叠工具栏时,在类中我使用setExpanded(true)和setExpanded(false)。

public class HomeHostActivity extends AppCompatActivity {

    @BindView(R.id.coordinator) DisableableCoordinatorLayout collapsingToolbarLayout;
    @BindView(R.id.toolbarUserProfilePage) Toolbar toolbar;
    @BindView(R.id.appbar) AppBarLayout searchBarLayout;


   @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       setContentView(R.layout.teste);

       ButterKnife.bind(this);

       collapsingToolbarLayout.setPassScrolling(false);
   }

   @OnClick(R.id.toolbarUserProfilePage)
   public void expandView(View view) {
       searchBarLayout.setExpanded(true, true);
   }

   @OnClick(R.id.textViewEditProfilePageLastName)
    public void collapseView(View view) {
        searchBarLayout.setExpanded(false, true);
   }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

0 个答案:

没有答案