由于工具栏,RecycleView在底部留下空白区域

问题描述 投票:10回答:5

更新:

  • 实际问题是CoordinatorLayout不是RecycleView
  • 我没有使用RecycleView而是在TextView内尝试了ScrollView,这是同样的问题。
  • 如果你有Toolbar作为ActionBar并使用CoordinatorLayout和另一个ToolBar作为Sticky Header,底部有滚动元素,那么就没有对齐

原版的:

我正在开发一个视图,需要Sticky标头实现,底部有回收视图。我在这里描述了Coordinator layout support

工作原理:

  • 滚动列表上的粘滞视图。 工具栏使用layout_collapseMode = pinCollapsingToolbarLayout使用layout_scrollFlags = scroll|exitUntilCollapsed|snap属性。 使用行为app:layout_behavior="@string/appbar_scrolling_view_behavior回收视图“

问题是什么:

  • 回收视图在底部留下边距,它与Toolbar具有相同的大小我正在使用粘性视图。
  • Recyclerview最后一项不显示,它需要额外的qazxsw poi作为粘性工具栏视图的大小。

观察:

  • 如果我填写回收即时,那么它的工作。但如果通知它有一些延迟,那么它会导致问题。
  • 更新。在另一个试验和运行**,而不是使用回收我把bottom_margin放在TextView。(PFA)(这里没有更新布局) 这里我添加了xml中的文本,延迟2秒后只需添加一些文本,结果相同。 它的布局再次降低利润率。所以没有什么特别的相关的回收视图,它似乎与NestedScrollView的一些问题。

我试过多种解决方案可用CoordinatorLayouthere,但没有工作。

PFA,电流输出。

here

更新PFA,尝试延迟文本视图。

enter image description here

这是布局文件。

enter image description here
android android-toolbar android-coordinatorlayout android-collapsingtoolbarlayout stickyrecycleview
5个回答
3
投票

你能试试下面的布局文件吗?我测试了它,它工作正常。您也可以从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>

5
投票

我相信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> (可能是CoordinatorLayoutAppBarLayout - 不确定哪个组件)由于粘滞的工具栏而记录了CollapsingToolbarLayout的错误高度。如果在初始布局之前或之后添加项,则行为会有所不同。

请尝试以下方法:

  1. CollapsingToolbarLayout的XML中删除android:layout_marginBottom="72dp"
  2. RecyclerView添加到android:minHeight="72dp"的XML中

由于您的粘性工具栏设置为CollapsingToolbarLayout,因此可以将72dp设置为minHeight

如果您遇到此问题,请回复此处。


以下是使用72dp和上述更改的布局的快速演示。

更新:我使用NestedScrollView解决了这个问题。演示项目显示问题和修复是在RecyclerView

GitHub

这是代码:

main activity.Java

enter image description here

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>

4
投票

进入滚动时,<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,应该被归因于许可证要求。


3
投票

我假设在添加新数据和通知适配器时可能会出现问题。

你应该从CoordinatorBehaviorExample改为notifyDataSetChanged()

有关notifyItemInserted(index) notifyItemInserted(index)的更多文档。

希望能帮助到你。


2
投票

嘿,你在那里设置了here属性。删除它。这就是错误所在。 marginBottom的代码应该是:

RecyclerView
© www.soinside.com 2019 - 2024. All rights reserved.