协调器布局中的回收视图
Recycleview in coordinatorlayout
我正在尝试创建底部具有 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); ),我看到的只是最后一个视图的一部分(不是全尺寸)。如果 recycleview 没有嵌套到 CoordinatorLayout - 一切都按预期工作 - 我看到全尺寸的最后一个元素视图。我怎样才能更改布局以使其正常工作?
试试这个方法:
<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加到你的activity
最后一个元素被剪掉了,因为 RecyclerView 在屏幕上并不完全可见。它被扩展的 AppBar 向下推。请注意,手动滚动时,AppBar 会在您到达最后一个元素时缩小。
在我的案例中,最有效的方法是在滚动之前简单地折叠 AppBar:
AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appbar);
appBarLayout.setExpanded(false, false);
recyclerView.smoothScrollToPosition(position);
我想您可以通过仅在需要时折叠它来改进此解决方案。
如果不需要折叠 AppBar,那么您可以添加与展开的 AppBar 相同高度的底部填充。但是,还会有其他故障(例如,当滚动到已经在 RecyclerView 中但刚好在屏幕之外的位置时)
问题似乎是 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);
在您的代码中从 Toolbar
中删除 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>
我正在尝试创建底部具有 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); ),我看到的只是最后一个视图的一部分(不是全尺寸)。如果 recycleview 没有嵌套到 CoordinatorLayout - 一切都按预期工作 - 我看到全尺寸的最后一个元素视图。我怎样才能更改布局以使其正常工作?
试试这个方法:
<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加到你的activity
最后一个元素被剪掉了,因为 RecyclerView 在屏幕上并不完全可见。它被扩展的 AppBar 向下推。请注意,手动滚动时,AppBar 会在您到达最后一个元素时缩小。
在我的案例中,最有效的方法是在滚动之前简单地折叠 AppBar:
AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appbar);
appBarLayout.setExpanded(false, false);
recyclerView.smoothScrollToPosition(position);
我想您可以通过仅在需要时折叠它来改进此解决方案。
如果不需要折叠 AppBar,那么您可以添加与展开的 AppBar 相同高度的底部填充。但是,还会有其他故障(例如,当滚动到已经在 RecyclerView 中但刚好在屏幕之外的位置时)
问题似乎是 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);
在您的代码中从 Toolbar
中删除 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>