【问题标题】:How to put button on top of a Recycler View?如何将按钮放在回收站视图的顶部?
【发布时间】:2021-07-22 13:51:21
【问题描述】:

我正在使用 Android Studio 开发一个 Android 应用,但在使用回收站视图顶部的浮动按钮时遇到问题。 我想要实现的是在我的回收站视图顶部有一个浮动按钮,当我滚动到回收站视图的底部时,该按钮不应覆盖我列表中的最后一项。

如果我在我的回收站视图中添加填充或边距,这就是我得到的:

结果是没有填充也没有边距:

我的目标是在滚动到最后一项时得到这个:

当我不在最后一项时:

这是我目前拥有的代码:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        tools:context=".frontend.MainActivity">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/activityList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animationCache="true"
            android:background="@color/white"
            android:layout_marginTop="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/addRoutine"
            style="@style/Widget.AppCompat.Button"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/round_button"
            app:iconPadding="0dp"
            android:layout_marginBottom="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

如果有人能说出这是如何实现的,我将不胜感激。

谢谢!

【问题讨论】:

    标签: android xml android-layout android-recyclerview


    【解决方案1】:

    只需将ItemDecoration 附加到回收站视图即可实现此目的。

    第一步:添加这个Item装饰类

    class BottomItemDecoration : RecyclerView.ItemDecoration() {
    
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        if(parent.getChildAdapterPosition(view) == state.itemCount - 1) // Check if it's the last item
            outRect.bottom = 80 // Space(in pixels) to give after the last item for that floating button
    }
    

    }

    第 2 步:将 BottomItemDecoration 附加到回收站视图

    recyclerView.addItemDecoration(BottomItemDecoration())
    

    布局 XML 文件无需更改。

    【讨论】:

      【解决方案2】:

      我认为您可以在 RecyclerView 的Adapter 中创建 2 个ViewType,其中一个是实际内容,另一个是空白。在Adapter 中,您覆盖了getItemViewType(int position) 方法(此方法用于决定使用哪个视图持有者)。 注意:在这个方法中,我们只在 Recycler 视图滚动到最后一项 (position == yourList.size() -1) 时返回第二个视图持有者(空白的)。举个例子:

          @Override
          public int getItemViewType(int position) {
              if (position == yourList.size() - 1)
                  return CONTENT_VIEW_TYPE; //the actual content View Holder
              else return BLANK_VIEW_TYPE; //the blank View Holder
          }
      

      【讨论】:

        【解决方案3】:

        我认为你可以使用这种方法, 可能重复:How to allow scrolling the last item of recyclerview above of the FAB?

         recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy){
                if (dy > 0) {
                    fab.hide();
                    return;
                }
                if (dy < 0) {
                    fab.show();
                }
            }
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-01
          • 2020-09-05
          • 2017-01-26
          • 2018-04-29
          • 2020-10-31
          • 2021-12-23
          相关资源
          最近更新 更多