【问题标题】:Collapsible layout with custom view in androidandroid中带有自定义视图的可折叠布局
【发布时间】:2016-09-26 06:14:34
【问题描述】:

您好,我正在使用可折叠布局,我希望布局的行为根据我的滚动事件而改变,如图所示:

  1. 这是带有列表视图的默认图像,当用户滚动时,顶部布局应该会发生变化,看起来就像第二张图片中的那个。

我应该如何使用可折叠布局来实现这种行为。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • .support.design.widget.CollapsingToolbarLayout>

标签: android android-layout scrollview


【解决方案1】:

您只需在工具栏下添加 TabLayout,如下所示

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"/>
<android.support.design.widget.TabLayout
    android:id="@+id/tabLayoutactivity_mainId"
    android:layout_below="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

【讨论】:

  • 我不想要标签布局。选项卡布局将保持不变,但工具栏已被替换,请参阅第二张图片。
  • 我听不懂你在说什么..,我正在使用上述布局进行我的一个应用程序开发
【解决方案2】:

尝试根据您的需要覆盖此设置。您可以在 CollapsableToolbar 中设置使组件可见/隐藏的布局。

 appbar = (AppBarLayout) findViewById(R.id.appbar);
    header = (Toolbar) findViewById(R.id.toolbar);

    appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
                private State state;

                @Override
                public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                    if (verticalOffset == 0) {
                        if (state != State.EXPANDED) {
                            collapsingToolbar.setTitleEnabled(false);
                           // do something here
                        }
                        state = State.EXPANDED;
                    } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
                        if (state != State.COLLAPSED) {
                            collapsingToolbar.setTitle("All Jobs ");
                            collapsingToolbar.setTitleEnabled(true);

    //                  do something here      header.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.actionbar_gradient));
    //                        collapsingToolbar.setContentScrim(ContextCompat.getDrawable(this, R.drawable.actionbar_gradient));
                            collapsingToolbar.setContentScrim(ContextCompat.getDrawable(this, R.drawable.actionbar_bg));

    //                        collapsingToolbar.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.actionbar_bg));

                        }
                        state = State.COLLAPSED;
                    } else {
                        if (state == State.IDLE) {
                            collapsingToolbar.setTitleEnabled(false);
                            container.setVisibility(View.VISIBLE);
                        }
                        state = State.IDLE;
                    }
                }
            });

它对我有用,试试吧....

【讨论】:

    猜你喜欢
    • 2015-05-29
    • 2021-06-09
    • 2011-07-28
    • 1970-01-01
    • 2015-01-05
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多