向上滚动时折叠工具栏隐藏,向下滚动时显示

时间:2017-06-27 14:51:18

标签: android android-toolbar android-coordinatorlayout android-collapsingtoolbarlayout android-appbarlayout

在我们的聊天应用中,首先启动新消息。查看旧消息用户向上滚动。为此,我们将reverseLayout设置为true。

Ex:LayoutManager反向所以0位置是底部。当用户滚动旧消息时,假设位置为10。所以我想隐藏工具栏。当用户从10位置到9位置时,我想显示工具栏。

LinearLayoutManager linearLayoutManager = new   WrapContentLinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
linearLayoutManager.setReverseLayout(true);

我们希望在用户向上滚动以获取旧消息时隐藏折叠工具栏,并在用户向下滚动时显示折叠工具栏。

有关如何实现这一目标的任何提示?

  

XML代码如下:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/whole_background_color"
android:orientation="vertical">
<ImageView
    android:id="@+id/ivBackgroundWall"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop" />
<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/rlChatMessageSendView">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00000000"
        app:elevation="0dp">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|enterAlways">
            <include
                android:id="@+id/rlZoneDetail"
                layout="@layout/include_layout_zone_option" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="com.gochat.uIUtil.FixScrollingFooterBehavior">
        <RelativeLayout
            android:id="@+id/rel_root_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/rlChat"
            android:paddingBottom="@dimen/margin_five">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerViewChat"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:overScrollMode="never"
                android:scrollbars="none"
                android:visibility="visible"
                tools:listitem="@layout/chat_adapter_message_layout" />
            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:theme="@style/AppTheme.ProgressBarColor"
                android:visibility="gone" />
            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/tvFollow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="@dimen/padding_five"
                android:layout_marginTop="@dimen/padding_ten"
                android:background="@drawable/background_login_signup_small_radius"
                android:gravity="center"
                android:maxLines="1"
                android:paddingBottom="@dimen/padding_five"
                android:paddingLeft="@dimen/padding_twenty"
                android:paddingRight="@dimen/padding_twenty"
                android:paddingTop="@dimen/padding_five"
                android:singleLine="true"
                android:text="@string/start_follow"
                android:textAllCaps="false"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_fifteen"
                android:visibility="gone" />
            <LinearLayout
                android:id="@+id/llAnnounce"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/recyclerViewChat"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="@dimen/padding_five"
                android:layout_marginRight="@dimen/padding_five"
                android:layout_marginTop="@dimen/padding_five"
                android:background="@drawable/background_login_signup_small_radius"
                android:gravity="center"
                android:visibility="gone">
                <ImageView
                    android:layout_width="@dimen/padding_thirteen"
                    android:layout_height="@dimen/padding_thirteen"
                    android:layout_marginLeft="@dimen/margin_seven"
                    android:layout_marginStart="@dimen/margin_seven"
                    android:src="@drawable/iv_white_shout" />
                <android.support.v7.widget.AppCompatTextView
                    android:id="@+id/tvAnnouncement"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="@dimen/margin_twenty"
                    android:layout_marginLeft="@dimen/margin_ten"
                    android:layout_marginRight="@dimen/margin_twenty"
                    android:layout_marginStart="@dimen/margin_ten"
                    android:gravity="center"
                    android:maxLines="1"
                    android:paddingBottom="@dimen/padding_five"
                    android:paddingTop="@dimen/padding_five"
                    android:singleLine="true"
                    android:text="@string/announce_text"
                    android:textAllCaps="false"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/white"
                    android:textSize="@dimen/text_size_thirteen"
                    android:visibility="visible" />
                <ImageView
                    android:layout_width="@dimen/padding_thirteen"
                    android:layout_height="@dimen/padding_thirteen"
                    android:layout_marginEnd="@dimen/margin_seven"
                    android:layout_marginRight="@dimen/margin_seven"
                    android:src="@drawable/iv_white_send" />
            </LinearLayout>
        </RelativeLayout>
        <LinearLayout
            android:id="@+id/rlChat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom"
            android:background="@color/bg_bottombar"
            android:orientation="vertical"
            android:visibility="visible"
            app:layout_behavior="com.gochat.uIUtil.FixScrollingFooterBehavior">
            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/onedp"
                android:background="@color/create_zone_divider_color_whatsapp" />
            <RelativeLayout
                android:id="@+id/rl_send_message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/bottom_tab"
                android:padding="@dimen/padding_seven"
                android:visibility="visible">
                <RelativeLayout
                    android:id="@+id/rlAdd"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true"
                    android:paddingBottom="@dimen/padding_ten"
                    android:paddingRight="@dimen/margin_eight"
                    android:paddingTop="@dimen/padding_ten">
                    <ImageView
                        android:id="@+id/ivAdd"
                        android:layout_width="@dimen/margin_twenty"
                        android:layout_height="@dimen/margin_twenty"
                        android:layout_centerVertical="true"
                        android:src="@drawable/iv_add_blue" />
                </RelativeLayout>
                <ImageView
                    android:id="@+id/ivFullscreen"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/iv_add_blue"
                    android:visibility="gone" />
                <ImageView
                    android:id="@+id/ivMenuOpen"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/rel_message"
                    android:layout_centerVertical="true"
                    android:layout_marginEnd="@dimen/margin_twenty"
                    android:layout_marginRight="@dimen/margin_twenty"
                    android:layout_toLeftOf="@+id/ivFullscreen"
                    android:layout_toStartOf="@+id/ivFullscreen"
                    android:src="@drawable/iv_menu_grid"
                    android:visibility="gone" />
                <TextView
                    android:id="@+id/tv_Title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:gravity="center"
                    android:text="TITLE"
                    android:textStyle="bold"
                    android:visibility="gone" />
                <RelativeLayout
                    android:id="@+id/rel_message"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toEndOf="@+id/rlAdd"
                    android:layout_toLeftOf="@+id/iv_send"
                    android:layout_toRightOf="@+id/rlAdd"
                    android:layout_toStartOf="@+id/iv_send"
                    android:background="@drawable/chat_send_message_background"
                    android:visibility="visible">
                    <RelativeLayout
                        android:id="@+id/rl_chat_send_message"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:orientation="horizontal">
                        <EditText
                            android:id="@+id/et_chat"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="@dimen/margin_ten"
                            android:layout_marginLeft="@dimen/margin_ten"
                            android:layout_marginRight="@dimen/margin_ten"
                            android:layout_marginStart="@dimen/margin_ten"
                            android:background="@null"
                            android:hint="@string/type_messsage"
                            android:inputType="textMultiLine"
                            android:maxLines="4"
                            android:padding="@dimen/padding_eight"
                            android:textColorHint="@color/hint_text_color"
                            android:textSize="@dimen/text_size_fifteen"
                            tools:text="zxnihxkbdfcdaaa" />
                    </RelativeLayout>
                </RelativeLayout>
                <ImageView
                    android:id="@+id/iv_send"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_margin="@dimen/margin_five"
                    android:src="@drawable/iv_send_message_light"
                    android:visibility="invisible" />
                <ImageView
                    android:id="@+id/iv_record"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_margin="@dimen/margin_five"
                    android:src="@drawable/iv_audio_record"
                    android:visibility="visible" />
            </RelativeLayout>
            <FrameLayout
                android:id="@+id/frame_layout"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@color/white"
                android:padding="@dimen/padding_fifteen"
                android:visibility="gone" />
        </LinearLayout>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<RelativeLayout
    android:id="@+id/rlChatMessageSendView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">
    <LinearLayout
        android:id="@+id/llSpectatorZone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="@dimen/margin_ten"
        android:background="@color/white"
        android:orientation="horizontal"
        android:padding="@dimen/padding_ten"
        android:visibility="gone">
        <TextView
            android:id="@+id/tvSpectatorZone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/you_can_only_view_this_zone"
            android:textColor="@color/unselected_item_color"
            android:textSize="@dimen/text_size_seventeen" />
    </LinearLayout>
  </RelativeLayout>
 </RelativeLayout>

enter image description here

1 个答案:

答案 0 :(得分:0)

请尝试以下链接。它可以解决你的问题。

http://xmodulo.com/hide-show-toolbar-scrolling-android.html

谢谢!