如何修复滚动时折叠工具栏中的视图?

2023-11-23

我想用两个实现折叠工具栏EditText在其中,用于用户输入的目的。我正在跟进this回答。答案给出了添加两个的完美解决方案EditText进入折叠工具栏。但行为并不如预期。

我所取得的成就:

enter image description here

预期行为:

enter image description here

我的 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="wrap_content"
android:fitsSystemWindows="true">

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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_tool_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        app:expandedTitleTextAppearance="@style/Widget.AppCompat.ActionBar.TabText"
        app:layout_scrollFlags="scroll|enterAlways"
        app:statusBarScrim="?attr/colorAccent">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_1"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/primary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_collapseMode="none"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
        </android.support.v7.widget.Toolbar>

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_collapseMode="none"
        app:elevation="0dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="32dp"
            android:paddingTop="16dp"
            android:paddingBottom="56dp"
            android:paddingRight="16dp">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/lNameLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/fNameLayout"
                android:layout_marginTop="10dp">

                <EditText
                    android:id="@+id/ltitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="Title"/>
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/lNameLayout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/fNameLayout"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp">

                <EditText
                    android:id="@+id/ldesc"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="Description"/>
            </android.support.design.widget.TextInputLayout>


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

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


<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="10dp">

        <!--my widgets here-->

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

我知道我可以使用这种类型的样式scrollFlag。我读了这篇关于滚动标志的文章。但无法准确找到如何将其用于此目的。
我还想改变字体大小EditText如上图所示GIF.

但第一个问题是如何修复工具栏中的一个视图并在用户向上滚动时隐藏另一个视图。如果有人用合适的例子来解释那就太好了。


我确信Todoist正在以另一种方式做这件事,但仍然......

这里是XML布局。主要思想是,应该固定的视图应该位于内部Toolbar,当您想要隐藏的另一个视图应该在里面时CollapsingToolbarLayout具有适当的上边距,以防止重叠:

<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/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_tool_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:statusBarScrim="?attr/colorAccent">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_collapseMode="parallax"
                android:layout_marginLeft="72dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="32dp"
                android:layout_marginTop="136dp">

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

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/ldesc"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ems="10"
                        android:hint="Description"/>
                </android.support.design.widget.TextInputLayout>

            </FrameLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/primary"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/lNameLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="16dp"
                    android:layout_marginBottom="16dp"
                    android:layout_marginTop="48dp">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Title"
                        android:textSize="30sp"
                        android:textColor="@android:color/white"
                        android:ems="10"/>
                </android.support.design.widget.TextInputLayout>

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

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


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

        <!-- your content here -->

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

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

然后,要实现相同的字体大小和上边距动画,我们可以实现AppBarLayout.OnOffsetChangedListener并根据滚动偏移变化对我们的属性进行动画处理。这里是活动课:

public class MainActivity extends AppCompatActivity
    implements AppBarLayout.OnOffsetChangedListener {

    private static final float COLLAPSED_TEXT_SIZE_SP = 18f;

    private static final float COLLAPSED_TOP_MARGIN_DP = 24f;

    private static final float MARGIN_SCROLLER_MULTIPLIER = 4f;

    private float expandedTextSize;
    private float collapsedTextSize;

    private int expandedTopMargin;
    private int collapsedTopMargin;

    private AppBarLayout mAppBarLayout;
    private Toolbar mToolbar;
    private TextInputEditText editText;
    private TextInputLayout textInputLayout;

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

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mAppBarLayout = (AppBarLayout) findViewById(R.id.appbar);
        editText = (TextInputEditText) findViewById(R.id.title);
        textInputLayout = (TextInputLayout) findViewById(R.id.lNameLayout);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mAppBarLayout.addOnOffsetChangedListener(this);

        expandedTextSize = editText.getTextSize();
        collapsedTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, COLLAPSED_TEXT_SIZE_SP, getResources().getDisplayMetrics());

        expandedTopMargin = ((ViewGroup.MarginLayoutParams) textInputLayout.getLayoutParams()).topMargin;
        collapsedTopMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, COLLAPSED_TOP_MARGIN_DP, getResources().getDisplayMetrics());
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
        int maxScroll = appBarLayout.getTotalScrollRange();
        float percentage = (float) Math.abs(offset) / maxScroll;
        float textSizeDiff = Math.abs(expandedTextSize - collapsedTextSize);
        int marginDiff = Math.abs(expandedTopMargin - collapsedTopMargin);
        //change text size along with scrolling
        editText.setTextSize(TypedValue.COMPLEX_UNIT_PX, expandedTextSize - textSizeDiff * percentage);
        //change top view margin along with scrolling
        ((ViewGroup.MarginLayoutParams) textInputLayout.getLayoutParams()).topMargin = (int) (expandedTopMargin - marginDiff * Math.min(1, percentage * MARGIN_SCROLLER_MULTIPLIER));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 

因此,对于扩展状态,我使用了我们在 xml 中应用的默认值。对于折叠状态,我定义了简单的常量。请随意满足您的需求。

Note,当您添加后退按钮或菜单项时Toolbar,你应该玩弄left and right第二个视图(您要隐藏的视图)的边距,因为第一个EditText是一部分Toolbar,因此它的边距将根据您添加的内容而改变。

Result:

快乐编码!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何修复滚动时折叠工具栏中的视图? 的相关文章

随机推荐

  • -D MACRO 和 #define MACRO 的优先级

    如果我有一个C文件 foo c 虽然我已经给出了 DMACRO 1作为编译的命令行选项 但是 如果在头文件中我也有 define MACRO 2 其中哪一个会优先 我正在假设您正在做什么 但是如果您想从命令行为该宏提供非默认值 请尝试对宏定
  • 多线程访问文件

    我们有一个多线程java程序 多个线程将写入一个文件 一个线程将从该文件中读取 我正在寻找一些设计想法 是否需要同步 文件通道理论上是线程安全的 来自javadoc 文件通道可供多个并发线程安全使用 这 close 方法可以随时调用 具体由
  • 读取套接字时Java中断线程[重复]

    这个问题在这里已经有答案了 可能的重复 如何立即终止阻塞在套接字IO操作上的线程 我有一个客户端在线程中运行 想要从 Java 中的套接字读取数据 但在阅读时 也许我想杀死线程 所以我interrupt它 但是套接字的读取方法会抛出异常吗I
  • 如何在 jQuery 中循环遍历数组?

    我正在尝试循环遍历一个数组 我有以下代码 var currnt image list 21 32 234 223 var substr currnt image list split array here 我正在尝试从数组中获取所有数据 有
  • C# - 捕获鼠标光标图像

    背景 我正在编写一个屏幕捕获应用程序 我的代码基于此项目 http www codeproject com KB cs DesktopCaptureWithMouse aspx display Print 请注意 代码还捕获了鼠标光标 这对
  • PHP iOS AES 加密

    我在尝试使用 AES 加密在 PHP 和我的 iOS 应用程序之间进行通信时遇到了问题 到目前为止 我已经考虑了两种实现方法 第一个是使用 OpenSSL 在 iOS 方面 我以模仿此处显示的代码的方式实现 http saju net in
  • 如果向下滚动经过它,则有一个 div 紧贴在屏幕顶部[重复]

    这个问题在这里已经有答案了 我有一个 div 当我的页面首次加载时 它距离顶部大约 100px 它包含页面的一些按钮等 当用户滚动经过它时 我希望 div 跟随 用户 因为它附加到屏幕顶部 当用户返回到页面顶部时 我希望它回到原来的位置 V
  • 如何使用 CSS 删除没有 html 标签的字符串

    我需要删除没有的字符串html tag 例如 div class A a href class link keep this a and i want to remove this div 我可以只使用 css 来做到这一点吗 也许你可以使
  • Eclipse + Maven + JavaServer Faces -> ClassNotFoundException:StartupServletContextListener

    Summary 当我尝试从 Eclipse 中 在 Tomcat 7 0 上 运行 JSF 2 0 应用程序时 出现以下异常 Problem SEVERE Error configuring application listener of
  • 消除解析器精神规则中的左递归 x3

    我目前陷入了一条规则 我试图使用 boostspirit x3 来解析 这是我要解析的 EBNF 使用 Spirit 中的 运算符作为列表 type class type lambda type lambda type more arg l
  • “NoMethodError:[:not(.block-layered-nav)]:Array的未定义方法‘特异性’”

    这是对这篇文章的补充 无法在 Magento CE 1 9 中编译 rwd 皮肤 SCSS Windows 8 1 PowerShell Compass 1 0 1 Polaris Sass 3 4 6 Selective Steve ru
  • Pandas 按列将 CSV 拆分为多个 CSV(或 DataFrame)

    我对一个问题感到非常困惑 如果有一些帮助或提示 我将不胜感激 问题 我有一个 csv 文件 其中有一列可能有多个值 例如 Fruit Color The evil column Apple Red something1 Apple Gree
  • 使用 ant 编辑/追加数据到文本文件

    我正在尝试在 ant 构建过程中向 jad 文件添加属性 ant 中有任务可以做到这一点吗 我需要做的就是在文本文件的末尾添加一行文本 但我找不到执行此操作的任务 我相信这会起作用 现在测试一下
  • 检测 jQuery.ajaxComplete() 中是否存在 HTTP 方法(POST、GET)

    In a jQuery ajaxComplete 如何检测 HTTP 方法 特别是 GET 或 POST 我尝试阅读 jQuery 文档并四处搜索 但似乎找不到传递给内部函数处理程序的 3 个对象的太多文档 jQuery element a
  • 如何诊断或检测 Java 静态初始化器中的死锁

    在 Java 中使用静态初始化器是否是一个好主意超出了这个问题的范围 我在 Scala 应用程序中遇到了死锁 我认为这是由编译类中的互锁静态初始化器引起的 我的问题是如何检测和诊断这些死锁 我发现当涉及静态初始化程序块时 用于死锁的普通 J
  • 如何将 jQuery 自动完成组合框与 AJAX JSON 数据一起使用?

    我需要使用组合框执行以下操作 Select box有一个用户可以搜索的默认城市列表 如果用户在input框 我需要进行 ajax 调用来获取数据并向用户显示选项 如果根据用户的请求获取数据 则应将这些城市附加到以下选项中Select box
  • 如何关闭生产中的 Node.js Express(ejs 模板引擎)错误?

    当我在开发服务器上出现错误时 Express 会发送回溯作为响应 然而 这对于生产来说并不好 我不想让任何人看到我的引用 我怎样才能关闭它 注意 我使用 EJS 作为模板引擎 这可能是原因 而不是表达 例如 当我在 ejs 模板中有一个未定
  • 使用 Zip() 合并不同长度的数组而不丢失值

    在下面的代码中 我合并两个类型的数组int and string 第一个的长度大于第二个 因此最后一个索引 即5 不会被合并 int numbers new 1 2 3 4 5 string words new string one two
  • 媒体查询可以根据 div 元素而不是屏幕调整大小吗?

    我想使用媒体查询根据 a 的大小调整元素的大小div它们所在的元素 我不能使用屏幕尺寸作为div只是像网页中的小部件一样使用 其大小可能会有所不同 是的 CSS 容器查询正是您所寻找的 这CSS 遏制模块是详细说明此功能的规范 您可以检查浏
  • 如何修复滚动时折叠工具栏中的视图?

    我想用两个实现折叠工具栏EditText在其中 用于用户输入的目的 我正在跟进this回答 答案给出了添加两个的完美解决方案EditText进入折叠工具栏 但行为并不如预期 我所取得的成就 预期行为 我的 XML 代码