Android:折叠线性布局而不是折叠工具栏

标签 android android-collapsingtoolbarlayout

我正在尝试在单个 fragment 中创建主/明细事务。我想使用 LinearLayout 作为我的标题的编辑文本的容器。然后是 RecyclerView 以获取详细信息。

如何实现类似于 CollapsingToolbar 效果的 LinearLayout 的折叠/展开?

这是我正在尝试做的事情的屏幕截图。

enter image description here

到目前为止我的 xml 代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/colorAccent"
        android:padding="@dimen/activity_horizontal_margin">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/ic_date_range_black_24dp"
                android:tint="@android:color/darker_gray" />

            <android.support.v4.widget.Space
                android:layout_width="@dimen/activity_horizontal_margin"
                android:layout_height="wrap_content" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/date_til"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Date">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/date"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:cursorVisible="false"
                    android:focusable="false"
                    android:longClickable="false" />

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

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/ic_store_black_24dp"
                android:tint="@android:color/darker_gray" />

            <android.support.v4.widget.Space
                android:layout_width="@dimen/activity_horizontal_margin"
                android:layout_height="wrap_content" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/store_til"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Store">

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

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

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/ic_place_black_24dp"
                android:tint="@android:color/darker_gray" />

            <android.support.v4.widget.Space
                android:layout_width="@dimen/activity_horizontal_margin"
                android:layout_height="wrap_content" />

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

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/location"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Location" />

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

        </LinearLayout>

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layoutManager="LinearLayoutManager"
        tools:listitem="@layout/list_car" />

</LinearLayout>

另外,不确定这是否可以通过 CollapsingToolbar 完成,因为我大多只看到 ImageView 在工具栏中折叠。

感谢任何帮助。

更新:基本上我想在这里做的是在向上滚动时能够折叠标题 View 。然后向下滚动时展开。

最佳答案

您可以使用计时器并逐渐缩小顶部栏的 LinearLayout 的高度/边距(编辑:Anton Maiorov 的 答案中的缺陷已在此处修复)

enter image description here

见下面的代码 fragment (在设备上测试)

方法 I:Anton Maiorov 的 答案,修复了缺陷,这比下面的第二个实现简单得多

public class MainActivity extends AppCompatActivity {

    LinearLayout toolbar;
    int mOriginalHeight;
    boolean initialSizeObtained = false;    
    boolean isShrink = false;

    Animation _hideAnimation = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) toolbar.getLayoutParams();
            params.topMargin = -(int) (mOriginalHeight * interpolatedTime);
            toolbar.setLayoutParams(params);
        }
    };

    Animation _showAnimation = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) toolbar.getLayoutParams();
            params.topMargin = (int) (mOriginalHeight * (interpolatedTime - 1));
            toolbar.setLayoutParams(params);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (LinearLayout) findViewById(R.id.toolbar);
        //Get the original height, which is measured according to WRAP_CONTENT
        toolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (initialSizeObtained)
                    return;
                initialSizeObtained = true;
                mOriginalHeight = toolbar.getMeasuredHeight();
            }
        });
        _hideAnimation.setDuration(2000);
        _showAnimation.setDuration(2000);
    }

    //Click on the Olimpic image --> Toggles the top toolbar
    public void ToggleTopBar(View view) {
        isShrink = !isShrink;

        toolbar.clearAnimation();  //Important            
        toolbar.startAnimation(isShrink? _hideAnimation : _showAnimation);
    }
}

方法二:我原来的答案是改变工具栏的高度,还手动使用计时器,这涉及更多:

public class MainActivity extends AppCompatActivity {

    LinearLayout toolbar;
    int mOriginalHeight;
    boolean initialSizeObtained = false;
    int currentHeight;
    boolean isShrink = false;
    Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (LinearLayout) findViewById(R.id.toolbar);
        toolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (initialSizeObtained)
                    return;
                initialSizeObtained = true;
                mOriginalHeight = toolbar.getMeasuredHeight();
                currentHeight = mOriginalHeight;
                Log.d("Demo", "Original height is " + mOriginalHeight);
            }
        });
    }

    //Click on the Olimpic image --> Toggles the top toolbar
    public void ToggleTopBar(View view) {
        isShrink = !isShrink;
        Resize(isShrink, toolbar, 250);
    }


    void Resize(final boolean isShrink, final LinearLayout layout, final int minHeight) {
        final int H0 = mOriginalHeight;
        timer = runTimerAction(10, new Runnable() {
                    public void run() {
                        Log.d("demo", "Current Height= " + currentHeight);
                        if (isShrink && currentHeight > minHeight) {
                            currentHeight -= 10;
                            layout.getLayoutParams().height = currentHeight;
                            refreshToolbar();
                        } else if (!isShrink && currentHeight < H0) {
                            currentHeight += 10;
                            layout.getLayoutParams().height = currentHeight;
                            refreshToolbar();
                        } else {
                            layout.getLayoutParams().height = isShrink ? minHeight : H0;
                            refreshToolbar();
                            if (timer != null)
                                timer.cancel();
                        }
                    }
                }
        );
    }

    public void refreshToolbar() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                toolbar.requestLayout();
            }
        });
    }

关于Android:折叠线性布局而不是折叠工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39137188/

相关文章:

Android ndk r15b - Posix_memalign () 未声明的标识符

java - 模运算符空指针异常

android - 无法解析方法setExpandedTitleGravity(int)

android - 如何实现自定义 TextView 固定在顶部的折叠工具栏布局?

android - 当gridview不在顶部时如何展开appbarlayout

android - 当没有更多内容可显示时,如何停止折叠的工具栏?

android - 如何从 onResume() 中的包中提取字符串?

java - 下载 PDF 不起作用

java - 终止应用程序后继续运行计时器

android - 如何模仿谷歌地图的底页 3 阶段行为?