gpt4 book ai didi

android - coordinatorlayout 中的 Recyclerview

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:13:35 25 4
gpt4 key购买 nike

我正在尝试创建底部具有 CoordinatorLayout 和 LinearLayout 的 RelativeLayout,但发现了一些我无法解决的奇怪行为。这是我的布局

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.CoordinatorLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/sender"
android:background="@android:color/white"
android:fitsSystemWindows="true">

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

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="?colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />

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

<android.support.v7.widget.RecyclerView
android:id="@+id/messages_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

<LinearLayout
android:id="@+id/sender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_vertical"
android:background="@android:color/white">

<EditText
android:id="@+id/inputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text" />

<Button
android:fontFamily="sans-serif"
style="?android:attr/borderlessButtonStyle"
android:textAppearance="?android:textAppearanceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"
android:textColor="?colorPrimary"
android:id="@+id/send"/>

</LinearLayout>

</RelativeLayout>

更改适配器中的数据后,我试图滚动到最后一个元素(例如通过 recyclerView.smoothScrollToPosition(size); ),我看到的只是最后一个 View 的一部分(不是全尺寸)。如果 recycleview 没有嵌套到 CoordinatorLayout - 一切都按预期工作 - 我看到全尺寸的最后一个元素 View 。如何更改布局以使其正常工作?

最佳答案

问题似乎是 smoothScrollToPosition() 将静默滚动 RecyclerView 而不会让 CoordinatorLayout 知道正在发生的滚动。这就是我想出的。它的好处是它应该只滚动 AppBarLayout 如果您在 Adapter 中有足够的项目。

    final AppBarLayout layout = (AppBarLayout) findViewById(R.id.appbar);
layout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
lastVerticalOffset = verticalOffset;
}
});
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (tryCollapseAppbarOnNextScroll && lastVerticalOffset != -layout.getTotalScrollRange()) {
layout.setExpanded(false);
tryCollapseAppbarOnNextScroll = false;
}
}
});

现在,每当您发送消息时,请执行以下操作:

tryCollapseAppbarOnNextScroll = true;
recyclerView.smoothScrollToPosition(adapter.getItemCount()-1);

关于android - coordinatorlayout 中的 Recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31230650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com