协调布局中的可回收视图

5

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

    <?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);),但我只能看到最后一个视图的一部分(而不是完整大小)。如果RecycleView未嵌套在CoordinatorLayout中,则所有操作都按预期工作-我可以看到完整大小的最后一个元素视图。我该如何更改布局以使其全部正确工作?

你正在使用哪个版本的RecyclerView?如果不是v7:22.2.0,请尝试更新它。 - bkurzius
这里也是同样的事情 v 22.2.1.0。你解决了吗? - Mikhail
不,现在我还没有任何解决方案。已经为库创建了工单,但目前还未得到解决。 - dmtrlbdv
1
我看到了相同的问题。RecyclerView中的最后一个元素被剪切了 :-P - kenyee
1
https://code.google.com/p/android/issues/detail?id=179128 - dmtrlbdv
显示剩余4条评论
4个回答

1
问题似乎在于 smoothScrollToPosition() 会在不告知 CoordinatorLayout 的情况下静默滚动 RecyclerView。这是我想到的解决方案。好处是,如果适配器中有足够的项目,它应该只会滚动AppBarLayout
    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);

0

尝试这种方式:

<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:layout_marginBottom="16dp">

主要的重点是在你的活动中使用android:layout_marginBottom


0

由于RecyclerView没有完全显示在屏幕上,因此最后一个元素被裁剪了。它被扩展的AppBar向下推。请注意,手动滚动时,当您到达最后一个元素时,AppBar会缩小。

在我的情况下,最好的方法是在滚动之前简单地折叠AppBar:

AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appbar);
appBarLayout.setExpanded(false, false);
recyclerView.smoothScrollToPosition(position);

我想你可以通过仅在需要时折叠它来改进这个解决方案。

如果不希望折叠AppBar,则可以添加与展开的AppBar相同高度的底部填充。然而,还会有其他故障(例如,当滚动到已经在RecyclerView中但刚好在屏幕外的位置时)


0
在您的代码中,从中删除app:layout_scrollFlags =“scroll | enterAlways”,并将其添加到AppBarLayout中,使其应该像这样。
<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways">

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接