android内容页折叠,android – 展开CollapsingToolbarLayout时折叠内容而不是滚动

这里真正的问题是当app栏改变高度时如何正确地调整RelativeLayout的大小.在查看如何执行此操作之前,我们需要更改XML布局.

AppBarLayout also requires a separate scrolling sibling in order to know when to scroll.

由于RelativeLayout`不符合“滚动兄弟”的要求,我们需要将其包含在诸如“NestedScrollView”之类的内容中.这是更新的XML文件:

activity_main.xml中

android:id="@+id/coordinatorLayoutProfile"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

tools:context=".MainActivity">

android:id="@+id/appBar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:fitsSystemWindows="true"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

android:layout_width="match_parent"

android:layout_height="220dp"

android:background="@color/colorPrimary"

app:layout_scrollFlags="scroll|exitUntilCollapsed">

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?android:attr/actionBarSize"

app:layout_collapseMode="pin"

app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

android:layout_width="match_parent"

android:layout_height="match_parent"

app:layout_behavior="@string/appbar_scrolling_view_behavior">

android:id="@+id/relativeLayout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/holo_blue_light">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:text="The text on the top"

android:textSize="32sp" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:text="The text on the bottom"

android:textSize="32sp" />

我可能对XML进行了一些其他更改,以使我的环境中的工作正常,但除了插入NestedScrollView之外,不应该有任何实质性的更改.

对RelativeLayout大小的更改将在代码中设置,并将由AppBarLayout.OnOffsetChangedListener触发.我们将根据应用栏的垂直偏移计算并更改RelativeLayout的大小.

MainActivity.java

public class MainActivity extends AppCompatActivity {

private RelativeLayout mRelativeLayout;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

mRelativeLayout = findViewById(R.id.relativeLayout);

final AppBarLayout appBarLayout = findViewById(R.id.appBar);

final int screenHeight = getResources().getDisplayMetrics().heightPixels;

appBarLayout.post(new Runnable() {

@Override

public void run() {

adjustRelLayoutHeight(mRelativeLayout, screenHeight - appBarLayout.getBottom());

}

});

appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

@Override

public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

adjustRelLayoutHeight(mRelativeLayout, screenHeight - appBarLayout.getBottom());

}

});

}

private void adjustRelLayoutHeight(RelativeLayout layout, int newHeight) {

FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) layout.getLayoutParams();

lp.height = newHeight;

layout.setLayoutParams(lp);

}

private static final String TAG = "MainActivity";

}

结果如下:

T6bQt.gif

更新:以下是使用ViewTreeObserver.OnPreDrawListener()避免额外绘制的替代方法.然而效果是一样的:

MainActivity.java(替代方法)

public class MainActivity extends AppCompatActivity {

private RelativeLayout mRelativeLayout;

private int mCoordinatorHeight;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

mRelativeLayout = findViewById(R.id.relativeLayout);

final AppBarLayout appBarLayout = findViewById(R.id.appBar);

final CoordinatorLayout coordinatorLayout =

(CoordinatorLayout) findViewById(R.id.coordinatorLayoutProfile);

// Wait for just before drawing the view to get its height.

coordinatorLayout.getViewTreeObserver()

.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

@Override

public boolean onPreDraw() {

// Remove the listener so we don't go into an infinite loop.

coordinatorLayout.getViewTreeObserver().removeOnPreDrawListener(this);

mCoordinatorHeight = coordinatorLayout.getHeight();

adjustRelLayoutHeight(mRelativeLayout, mCoordinatorHeight - appBarLayout.getBottom());

return false; // abort drawing

}

});

appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

@Override

public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

adjustRelLayoutHeight(mRelativeLayout, mCoordinatorHeight - appBarLayout.getBottom());

}

});

}

private void adjustRelLayoutHeight(RelativeLayout layout, int newHeight) {

if (newHeight <= 0) {

// no-op if height is invalid

return;

}

FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) layout.getLayoutParams();

if (lp.height != newHeight) {

lp.height = newHeight;

layout.setLayoutParams(lp);

}

}

private static final String TAG = "MainActivity";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值