更新:
CoordinatorLayout
不是RecycleView
。RecycleView
而是在TextView
内尝试了ScrollView
,这是同样的问题。Toolbar
作为ActionBar
并使用CoordinatorLayout
和另一个ToolBar
作为Sticky Header,底部有滚动元素,那么就没有对齐原版的:
我正在开发一个视图,需要Sticky标头实现,底部有回收视图。我在这里描述了Coordinator layout support。
工作原理:
layout_collapseMode = pin
和CollapsingToolbarLayout
使用layout_scrollFlags = scroll|exitUntilCollapsed|snap
属性。
使用行为app:layout_behavior="@string/appbar_scrolling_view_behavior
回收视图“问题是什么:
Toolbar
具有相同的大小我正在使用粘性视图。观察:
bottom_margin
放在TextView
。(PFA)(这里没有更新布局)
这里我添加了xml中的文本,延迟2秒后只需添加一些文本,结果相同。
它的布局再次降低利润率。所以没有什么特别的相关的回收视图,它似乎与NestedScrollView
的一些问题。我试过多种解决方案可用CoordinatorLayout
,here,但没有工作。
PFA,电流输出。
更新PFA,尝试延迟文本视图。
这是布局文件。
你能试试下面的布局文件吗?我测试了它,它工作正常。您也可以从recyclerview中删除marginButtom。
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/summaryAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="@drawable/fable_1"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.3" />
<!-- This is sticky header-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/summaryToolBar"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="center"
android:background="@android:color/white"
android:padding="@dimen/common_layout_margin"
android:visibility="visible"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="24sp"
android:text="Offer"/>
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- Bottom margin if I do't use then it does not display last child item. Wired but true in this case-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:visibility="visible"
android:layout_marginBottom="72dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listItem="@layout/item_dessert" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我相信xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/summaryAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="@drawable/fable_1"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.3" />
<!-- This is sticky header-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/summaryToolBar"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="center"
android:background="@android:color/white"
android:padding="@dimen/common_layout_margin"
android:visibility="visible"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="24sp"
android:text="Offer"/>
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:visibility="visible"
android:layout_marginBottom="72dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listItem="@layout/item_dessert" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>
(可能是CoordinatorLayout
或AppBarLayout
- 不确定哪个组件)由于粘滞的工具栏而记录了CollapsingToolbarLayout
的错误高度。如果在初始布局之前或之后添加项,则行为会有所不同。
请尝试以下方法:
CollapsingToolbarLayout
的XML中删除android:layout_marginBottom="72dp"
。RecyclerView
添加到android:minHeight="72dp"
的XML中由于您的粘性工具栏设置为CollapsingToolbarLayout
,因此可以将72dp
设置为minHeight
。
如果您遇到此问题,请回复此处。
以下是使用72dp
和上述更改的布局的快速演示。
更新:我使用NestedScrollView
解决了这个问题。演示项目显示问题和修复是在RecyclerView
。
这是代码:
main activity.Java
activity_working.xml
public class MainActivity extends AppCompatActivity {
// Set to true to break the layout; false for it to work.
// The setting of this flag should only matter for the
// layout activity_not_working.
private boolean mBreakIt = true;
// private int mLayoutToUse = R.layout.activity_not_working;
private int mLayoutToUse = R.layout.activity_working;
private LinearLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(mLayoutToUse);
mLayout = findViewById(R.id.linearLayout);
if (mBreakIt) {
mLayout.post(new Runnable() {
@Override
public void run() {
addViews();
}
});
} else {
addViews();
}
}
private void addViews() {
for (int i = 0; i < 50; i++) {
TextView tv = new TextView(MainActivity.this);
tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tv.setText("TextView #" + (i + 1));
mLayout.addView(tv);
}
}
}
activity_not_working.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/summaryAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="72dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="@drawable/beach"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.3" />
<!-- This is sticky header-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/summaryToolBar"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="center"
android:background="@android:color/white"
android:padding="@dimen/common_layout_margin"
android:visibility="visible"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Offer"
android:textSize="24sp" />
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- Bottom margin if I do't use then it does not display last child item. Wired but true in this case-->
<!-- Removed following: -->
<!--android:layout_marginBottom="72dp"-->
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:visibility="visible"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_insetEdge="bottom"
tools:listItem="@layout/item_dessert">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
进入滚动时,<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/summaryAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="@drawable/beach"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.3" />
<!-- This is sticky header-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/summaryToolBar"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="center"
android:background="@android:color/white"
android:padding="@dimen/common_layout_margin"
android:visibility="visible"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Offer"
android:textSize="24sp" />
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- Bottom margin if I do't use then it does not display last child item. Wired but true in this case-->
<!-- Removed following: -->
<!--android:layout_marginBottom="72dp"-->
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="72dp"
android:background="@android:color/holo_blue_light"
android:visibility="visible"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_insetEdge="bottom"
tools:listItem="@layout/item_dessert">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A TextView" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
高度被错误计算。这两个不同的例子只是增加了问题的混淆,没有提供最少的RecyclerView
代码。
只需从Java
中删除这一行:
RecyclerView
此属性几乎没用,因为它是默认值:
android:layout_marginBottom="72dp"
不要像这样设置android:visibility="visible"
高度:
RecyclerView
但要填补可用空间:
android:layout_height="match_parent"
所以android:layout_height="0dp"
android:layout_weight="1.00"
总是适合,万一有工具栏存在,是否 - 这个工具栏不应该设置粘性,因为这是要求半心半意地“修复”布局。一个RecyclerView
可能最终在那里有用,取决于那个工具栏可能有用。甚至找到了脚本的来源:BottomNavigationView,应该被归因于许可证要求。
我假设在添加新数据和通知适配器时可能会出现问题。
你应该从CoordinatorBehaviorExample改为notifyDataSetChanged()
。
有关notifyItemInserted(index)
notifyItemInserted(index)
的更多文档。
希望能帮助到你。
嘿,你在那里设置了here属性。删除它。这就是错误所在。 marginBottom
的代码应该是:
RecyclerView