【问题标题】:CoordinatorLayout collapse Toolbar in Activity from Fragmentcoordinatorlayout折叠工具栏从片段中的活动
【发布时间】:2016-11-20 19:09:14
【问题描述】:

我有一个带有 DrawerLayout 的活动,用于导航菜单、工具栏和 FrameLayout,其中显示带有内容的片段。在片段中,我有一个包含一些内容的视图,例如:用于个人资料头像的 ImageView 和用于个人资料名称的 TextView。当 Fragment 向下滚动时,我需要从 Activity 折叠工具栏(在工具栏中使用 Fragment 中的内容隐藏视图/Toobar 重叠视图/),当 Fragment 向上滚动时,我需要展开工具栏(使用来自工具栏的内容显示视图)。

活动

    <android.support.v4.widget.DrawerLayout
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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:orientation="vertical"
        tools:context=".activity.MainActivity">

        <android.support.design.widget.AppBarLayout
            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="wrap_content">

            <include
                android:id="@+id/myToolBar"
                layout="@layout/activity_main_toolbar" />

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

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

    <ListView
        android:id="@+id/list_slider_menu"
        android:layout_width="@dimen/main_activity_slider_menu_list_view_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="@dimen/main_activity_slider_menu_list_view_divider_height"
        android:listSelector="@drawable/drawer_list_selector" />

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

片段

    <android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cabinetSwipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.geodevteam.geopay.project.activity.UserProfileActivity">

    <ScrollView
        android:id="@+id/cabinetScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/app_child_layout_background"
        android:fillViewport="false">

        <LinearLayout
            android:id="@+id/cabinetRootLayout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/cabinetTopLayout"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <RelativeLayout
                    android:id="@+id/cabinetInformationLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="5dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingBottom="1dp"
                    android:background="@color/cello_transparent">

                    <com.geodevteam.geopay.project.utils.graphics.RoundedImageView
                        android:id="@+id/cabinetAvatarImageView"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginTop="5dp"
                        android:layout_centerHorizontal="true" />

                    <TextView
                        android:id="@+id/cabinetNameTextView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/cabinetAvatarImageView"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="5dp"
                        style="@style/ProfileNameTextView"/>

                    <TextView
                        android:id="@+id/cabinetTelephoneTextView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/cabinetNameTextView"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="5dp"
                        android:visibility="gone"
                        style="@style/ProfileSimpleTextView"/>

                    <TextView
                        android:id="@+id/cabinetAddTelephoneTextView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/cabinetTelephoneTextView"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="5dp"
                        android:visibility="gone"
                        style="@style/ProfileSimpleTextView"/>

                    <TextView
                        android:id="@+id/cabinetBalanceTextView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/cabinetAddTelephoneTextView"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="5dp"
                        style="@style/ProfileSimpleTextView"/>

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/cabinetTrustLineLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="3dp"
                    android:background="@color/cello_transparent">

                </RelativeLayout>

            </LinearLayout>

            <RelativeLayout
                android:id="@+id/cabinetContractorListLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/trustLineTitleTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="8dp"
                    style="@style/CabinetTrustLinesTextView"
                    android:text="@string/trust_lines" />

                <ProgressBar
                    android:id="@+id/cabinetProgressBar"
                    style="?android:progressBarStyleLarge"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:layout_centerHorizontal="true"
                    android:visibility="gone"/>

                <ListView
                    android:id="@+id/cabinetContractorsListView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/trustLineTitleTextView"
                    android:layout_marginTop="2dp">

                </ListView>

            </RelativeLayout>

        </LinearLayout>

    </ScrollView>

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

如何在工具栏中隐藏cabinetTopLayout(向下滚动时折叠工具栏)?有可能吗?

【问题讨论】:

    标签: android scroll toolbar collapse


    【解决方案1】:
    Fragment can access the parent activity instance with getActivity().by using that instance you can access the parent activity views
    
    //This is your view reference
    View view = getActivity().findViewById(R.id.toobar);
    
    //This is your scroll view reference
    scroll = (ScrollView) rootView.findViewById(R.id.scrooll_id);
    //Adding on touch listener
    scroll.setOnTouchListener(this);
    
    @Override
        public boolean onTouch(View v, MotionEvent event) {
            // mGestureDetector.onTouchEvent(event);
    
            switch (event.getAction) {  
    
                switch (action) {
    
                case MotionEvent.ACTION_DOWN:
                //Initial values
                    initialX = event.getX();
                    initialY = event.getY();
    
                    // Log.d(TAG, "Action was DOWN");
                    break;
    
                case MotionEvent.ACTION_MOVE:
                    // Log.d(TAG, "Action was MOVE");
                    break;
    
                case MotionEvent.ACTION_UP:
                //final values
                    float finalX = event.getX();
                    float finalY = event.getY();
    
    
                    break;
    
                case MotionEvent.ACTION_CANCEL:
                    Log.d(TAG, "Action was CANCEL");
                    break;
    
    
    
                }
    
    
    
    
    
            return false;
        }
    

    【讨论】:

    • 通过使用初始 x 和最终 x 值我们可以找到向上或向下滚动
    【解决方案2】:
    1. 在 Fragment xml 中使用 NestedScrollView 而不是 ScrollView
    2. 设置属性 app:layout_behavior="@string/appbar_scrolling_view_behavior"NestedScrollView 而不是将其添加到片段容器中。

    一个完整的工作示例:

    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">
    
      <fragment
          android:id="@+id/fragment"
          android:name="io.victoralbertos.stackoverflow.ChildFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:layout="@layout/child_fragment"/>
    
      <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="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"/>
    
      </android.support.design.widget.AppBarLayout>
    
    
    </android.support.design.widget.CoordinatorLayout>
    

    child_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.NestedScrollView
        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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:background="@color/colorAccent"
            android:textSize="150sp"
            android:text="Scroll me as much as you need"
            android:layout_height="match_parent"/>
      </LinearLayout>
    
    </android.support.v4.widget.NestedScrollView>
    

    【讨论】:

    • 这是一个很好的技巧,但是我在Activity的片段之间切换,我不需要在activity.xml中包含静态片段
    • 我认为这不会改变什么。您是否尝试过将此解决方案应用于您的具体情况?
    • 我的英语很差,所以我无法详细解释我需要什么。所以我附上一个img
    • 您仍然可以从答案中受益。您只需将活动 xml 中的所有组件移动到片段之一,并对其进行调整;)
    • 我可以像使用 FrameLayout 一样在 Activity 中以编程方式将片段实例传递给 吗? FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
    猜你喜欢
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2018-08-21
    • 2018-09-24
    • 2016-03-12
    相关资源
    最近更新 更多