如果不需要,防止CollapsingToolbarLayout崩溃

时间:2015-08-29 16:22:47

标签: android android-support-library android-toolbar android-collapsingtoolbarlayout

使用:

compile 'com.android.support:design:23.0.0'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'com.android.support:recyclerview-v7:23.0.0'

更新了项目Cheesesquare

在奶酪的细节中,我删除2张牌(只有一张)。有没有办法防止显示空格的工具栏折叠?

enter image description here

6 个答案:

答案 0 :(得分:28)

要在Cheesesquare示例中实现此类行为,只需将NestedScrollView的nb_values = 300 value_range = c(1:nb_values) nb_rows = 1000 d = data.table( c1 = rep(c(10,30,60,80), (nb_rows/4)) , c2 = rep(2,nb_rows) ) res1 = data.table( rep(0,nb_values) ) res2 = data.table( rep(0,nb_values) ) # METHOD 1 system.time( for(i in (1:10)) { for(i in value_range) res1[i] <- d[, sum( (i>c1) & (i>c2) ),] } ) # user system elapsed # 6.973 0.000 6.982 # METHOD 2 f <- function(i) { d[, sum( (i>c1) & (i>c2) ),] } system.time( for(i in (1:10)) { res2 <- apply(cbind(value_range), 1, f) } ) # user system elapsed # 3.152 0.000 3.161 identical(as.integer(res1$V1), res2) # [1] TRUE param修改为android:layout_height即可。 如果内容足够小以适应屏幕,它将阻止按内容滚动。

要防止按wrap_content滚动,您应该以编程方式将CollapsingToolbarLayout参数设置为layout_scrollFlags值。

Here描述了如何做到这一点。

答案 1 :(得分:1)

在xml中我使用了属性

app:layout_scrollFlags="snap"

中的

<android.support.design.widget.CollapsingToolbarLayout

并在活动中跟随

 toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 toolbar.setTitle(null);
 toolbar.setCollapsible(false);

现在正在运作。

答案 2 :(得分:1)

您可以为此使用以下代码:

   public static void stopScroll() {
    AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) collapsing_toolbar.getLayoutParams();
    toolbarLayoutParams.setScrollFlags(0);
    collapsing_toolbar.setLayoutParams(toolbarLayoutParams);

    CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
    appBarLayoutParams.setBehavior(null);
    appbar.setLayoutParams(appBarLayoutParams);
}

public static void startScroll() {
    AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) collapsing_toolbar.getLayoutParams();
    toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
    collapsing_toolbar.setLayoutParams(toolbarLayoutParams);

    CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
    appBarLayoutParams.setBehavior(new AppBarLayout.Behavior());
    appbar.setLayoutParams(appBarLayoutParams);
}

答案 3 :(得分:0)

这是我的工作代码,最初要拼贴栏:

_appbar.setExpanded(假);

   AppBarLayout _appbar = (AppBarLayout) findViewById(R.id.appbar);
    _appbar.setExpanded(false);

这是布局xml

 <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">



        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

            app:layout_collapseMode="pin" />





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

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

参考文献是:https://spark.apache.org/docs/latest/api/python/pyspark.sql.html

答案 4 :(得分:0)

一种受@Vishal回答启发的数据绑定解决方案

<com.google.android.material.appbar.AppBarLayout
    <com.google.android.material.appbar.CollapsingToolbarLayout
        app:enableCollapsingScroll="@{listItems.size > 0}"

@BindingAdapter("app:enableCollapsingScroll")
fun setCollapsingToolbarLayoutScrollEnabled(collapsingToolbarLayout: CollapsingToolbarLayout, enabled: Boolean?) {
    val lp = collapsingToolbarLayout.layoutParams as AppBarLayout.LayoutParams
    if (enabled.orFalse()) {
        lp.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL or AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED
    } else {
        lp.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP
    }
    collapsingToolbarLayout.layoutParams = lp
}

答案 5 :(得分:0)

  AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) activityUserGroupProfleBinding.collapsingToolbarLayout.getLayoutParams();
        if (logic) {
            params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
        } else {
            params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
        }
        activityUserGroupProfleBinding.collapsingToolbarLayout.setLayoutParams(params);