【问题标题】:How to recreate Android quick settings sliding panel?如何重新创建 Android 快速设置滑动面板?
【发布时间】:2016-03-09 19:21:50
【问题描述】:

在我的应用程序中,我想重新创建与大家都知道的 Lollipop+ 快速设置面板非常相似的东西。

即:通过点击或拖动表头,希望面板从表头下方向下滑动,将现有内容下推。

现在应用于我的应用程序,标题是工具栏,主要内容是显示博客文章列表的 RecyclerView。通过单击或拖动工具栏,我希望出现一个面板来显示有关博客的一些统计信息。像这样:

我一直在玩弄很棒(但很复杂)的 Android 设计支持库。它具有很好的滚动和设计应用栏和主要内容之间交互的功能。但是效果很难达到。

我研究了CollapsingToolbarLayout,但无法以将内容扩展低于主要Toolbar 的方式使用它。我还研究了SlidingUpPanel 库,但无法将内容向下推,只需悬停即可。总的来说,我有点不知道CoordinatorLayout、CollapsingToolbarLayout 和滚动Behaviors 应该如何交互......

有谁知道重新创建这种“快速设置”效果?或者,也许有人知道我应该在哪里找到 AOSP 中快速设置的代码?

非常感谢!

【问题讨论】:

    标签: android android-toolbar android-design-library android-collapsingtoolbarlayout android-appbarlayout


    【解决方案1】:

    最近我创建了一个名为 Toolbar Panel 的库,它的工作原理类似于快速设置抽屉。您可以自己自定义面板。如果您有任何疑问或问题,可以在 github 中创建问题或在此答案中发表评论。

    这是演示视频:

    【讨论】:

    • 感谢 Niko,这个库很有趣。但是,它会拖动整个工具栏,因此工具栏本身会移动,并且面板会显示在 上方。我正在尝试使面板显示在工具栏的下方。所以即使实现的效果本身是一致的,它并没有真正回答我的问题:s
    • @Dadou 嗯,这也是一个有趣的补充。我会在业余时间尝试实现它!
    • 我开始认为我要自己使用自定义视图来开发效果。我会确保与您分享任何有趣的结果,并尽可能提交 PR!
    • @大豆 谢谢!将等待它:)
    • 最后我没有直接修改你的代码,因为我选择了另一种使用 CoordinatorLayout 的方法。但感谢您的灵感;)
    【解决方案2】:

    您可以通过将折叠代码放入您的主页活动中并根据工具栏点击事件使其可见和隐藏来做到这一点。

    用于动画 slid_down.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <translate
        android:duration="1000"
        android:fromYDelta="0"
        android:toYDelta="100%" />
    </set>
    

    slid_up.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <translate
        android:duration="1000"
        android:fromYDelta="100%"
        android:toYDelta="0" />
    </set>
    

    你的布局

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/lay"
                android:visibility="gone">
                <TextView android:text="Collapse Ne" android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/content_layout">
            <TextView android:text="Hello World!" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>
    

    在你的活动中

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);
                final LinearLayout lay = (LinearLayout) findViewById(R.id.lay);
                final LinearLayout content_layout = (LinearLayout) findViewById(R.id.content_layout);
    
                toolbar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(i%2==0){
                            lay.setVisibility(View.VISIBLE);
           content_layout.startAnimation(AnimationUtils.loadAnimation(context,
                R.anim.slid_down));
    }
                        else{
                            lay.setVisibility(View.GONE);
    content_layout.startAnimation(AnimationUtils.loadAnimation(context,
                R.anim.slid_up));
    }
                        i++;
                    }
                });
    

    我刚刚准备了一个演示...您可以根据您的要求进行更改。

    【讨论】:

    • 抱歉,我认为这在我的信息中会很明显,但确实我没有明确说明:我也想要动画和拖动,而不仅仅是出现/消失的布局。
    • 您可以在拖动时使用简单的 android 动画对其进行动画处理....查看我编辑的答案
    • @大豆在你刚才提到的点击工具栏的问题中,拖动什么时候起作用?
    • 是的,实际上,如果在赏金结束之前没有人有更好的解决方案,我会接受这个答案(感谢@HetalUpadhyay)。但理想情况下,我想实现完整的“快速设置效果”,包括点击和拖动。
    • 我自己的答案更完整且“可重复使用”,但是我奖励了你,因为你用动画让我走上了正确的轨道;)
    【解决方案3】:

    在得到Niko Yuwono 和Hetal Upadhyay 的深刻见解之后,我终于抽出时间解决了我自己的问题。

    关键是要依靠CoordinatorLayout来描述两个自定义Behaviors:一个用于滑动面板,另一个用于主要内容。我实际上为此创建了一个库,因为这可能会在将来帮助其他人:SubAppBarPanel。 See it in action.

    示例代码:

    <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:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:clickable="true"
                android:foreground="?selectableItemBackground" />
    
        </android.support.design.widget.AppBarLayout>
    
        <com.davidferrand.subappbarpanel.SubAppBarPanel
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:panel_expanded="false"
            app:panel_offset="10dp"
            app:panel_slidingQuantity="85%">
    
            <!-- Content of the sliding panel -->
    
        </com.davidferrand.subappbarpanel.SubAppBarPanel>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="com.davidferrand.subappbarpanel.SubAppBarPanel$ScrollingViewBehavior">
    
            <!-- Main content -->
    
        </FrameLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    注意:拖动行为仍然是 TODO。

    【讨论】:

      猜你喜欢
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多