Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 由于工具栏的原因,RecycleView在底部留下空白_Android_Android Toolbar_Android Coordinatorlayout_Android Collapsingtoolbarlayout_Stickyrecycleview - Fatal编程技术网

Android 由于工具栏的原因,RecycleView在底部留下空白

Android 由于工具栏的原因,RecycleView在底部留下空白,android,android-toolbar,android-coordinatorlayout,android-collapsingtoolbarlayout,stickyrecycleview,Android,Android Toolbar,Android Coordinatorlayout,Android Collapsingtoolbarlayout,Stickyrecycleview,更新: 实际问题在于CoordinatorLayout而不是RecycleView 我没有使用RecycleView而是在ScrollView中尝试了TextView,这是同样的问题 如果将工具栏设置为操作栏,并将协调布局与另一个工具栏用作底部带有滚动元素的粘性标题,则会出现不对齐的情况 原件: 我正在开发一个视图,该视图需要使用底部的循环视图实现粘性头。我在这里使用了描述 工作原理: 滚动列表上的粘性视图。 工具栏使用layout\u collapseMode=pin折叠工具栏布局使用

更新:

  • 实际问题在于
    CoordinatorLayout
    而不是
    RecycleView
  • 我没有使用
    RecycleView
    而是在
    ScrollView
    中尝试了
    TextView
    ,这是同样的问题
  • 如果将
    工具栏
    设置为
    操作栏
    ,并将
    协调布局
    与另一个
    工具栏
    用作底部带有滚动元素的粘性标题,则会出现不对齐的情况
原件:

我正在开发一个视图,该视图需要使用底部的循环视图实现粘性头。我在这里使用了描述

工作原理:

  • 滚动列表上的粘性视图。
    • 工具栏使用
      layout\u collapseMode=pin
      折叠工具栏布局
      使用
      layout\u scrollFlags=scroll | exituntilclopsed | snap
      属性
    • 使用行为回收视图
      app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior
问题是:

  • 循环视图在底部留有边距,它的大小和我用来粘贴视图的
    工具栏的大小相同
  • 循环视图最后一项不显示,它需要额外的
    底部_边距
    作为粘性工具栏视图的大小
观察:

  • 如果我立即填充回收,它就会工作。但如果延迟通知,则会导致问题
  • 更新。在另一个试用运行**中,我在
    嵌套滚动视图
    (PFA)中放置了一个
    文本视图
    (此处未在布局中更新)
    • 在这里,我添加了来自xml的文本,经过2秒的延迟后,只需再添加一些文本,结果是一样的
    • 是版式再次占据了底边。因此,与回收视图无关,这似乎是
      协调布局的一些问题
我尝试了多种可用的解决方案,但都没有成功

PFA,电流输出

更新PFA,延迟文本视图

这是布局文件

<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>

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

您应该从
notifyDataSetChanged()
更改为
notifyItemInserted(index)

有关
notifyItemInserted(index)
的更多文档


希望有帮助。

当进入滚动时,
RecyclerView
高度计算错误。这两个不同的示例只会使问题更加混乱,没有提供最少的
Java
代码

只需从
回收视图中删除这一行即可:

android:layout_marginBottom="72dp"
此属性没有什么用处,因为它是默认值:

android:visibility="visible"
不要像这样设置
RecyclerView
高度:

android:layout_height="match_parent"
但要让它填满可用空间:

android:layout_height="0dp"
android:layout_weight="1.00"

因此,
RecyclerView
将始终适合,以防出现工具栏,无论是否-此工具栏不应设置为粘性,因为这需要半心半意地“修复”布局。一个工具栏最终可能会有用,这取决于该工具栏的用途。甚至还找到了脚本的源代码:,根据许可证的要求,该脚本应该具有属性。

嘿,您已经在那里设置了
marginBottom
属性。去掉那个。这就是错误所在。
RecyclerView
的代码应为:

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:listItem="@layout/item_dessert" />

我认为,
协调布局
(可能是
AppBarLayout
CollingToolBarLayout
-不确定是哪个组件)由于工具栏粘滞,正在记录
CollingToolBarLayout
的不正确高度。如果在初始布局之前或之后添加项目,则行为会有所不同

请尝试以下操作:

  • RecyclerView
    的XML中删除
    android:layout_marginBottom=“72dp”
  • android:minHeight=“72dp”
    添加到XML中,用于
    折叠工具栏布局
  • 由于粘性工具栏设置为
    72dp
    ,因此可以使用
    minHeight
    设置为
    72dp

    如果你对此有问题,请发回这里


    下面是使用
    NestedScrollView
    和上述更改快速演示布局

    更新:我已经使用显示相同问题的
    RecyclerView
    解决了这个问题。显示问题和修复的演示项目已启动

    代码如下:

    MainActivity.java

    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);  
            }  
        }  
    }
    
    公共类MainActivity扩展了AppCompatActivity{
    //设置为true可中断布局;设置为false可使布局正常工作。
    //此标志的设置应仅适用于
    //布局活动不工作。
    私有布尔mBreakIt=true;
    //private int mlayoutouse=R.layout.activity\u不工作;
    私有int MlayoutUse=R.layout.activity\u working;
    私人线路布局;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(mLayoutUse);
    mLayout=findViewById(R.id.linearLayout);
    如果(mBreakIt){
    mLayout.post(新的Runnable(){
    @凌驾
    public void run(){
    添加视图();
    }  
    });  
    }否则{
    添加视图();
    }  
    }  
    私有void addViews(){
    对于(inti=0;i<50;i++){
    TextView tv=新的TextView(MainActivity.this);
    
    <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>
    
    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>