简体   繁体   English

如何将按钮放在回收站视图的顶部?

[英]How to put button on top of a Recycler View?

I'm developing an android app with Android Studio and I'm having problems using a floating button on top of a recycler view.我正在使用 Android Studio 开发 android 应用程序,但在使用回收站视图顶部的浮动按钮时遇到问题。 What I want to achive is to have a floating button on top of my recycler view and when I scroll to the bottom of the recycler view the button should not cover the last item on my list.我想要实现的是在我的回收站视图顶部有一个浮动按钮,当我滚动到回收站视图的底部时,该按钮不应覆盖我列表中的最后一项。

If I add padding or margin to my recycler view this is what i get:如果我在我的回收站视图中添加填充或边距,这就是我得到的:

带内边距或边距

And without padding nor margin this is the result:没有填充也没有边距,结果如下:

没有填充也没有边距

My goal is to get this when scrolling to the last item:我的目标是在滚动到最后一项时得到这个:

目标看到最后一项

And this when I am not in the last item:当我不在最后一项时:

目标没有看到最后一项

This is the code I currently have:这是我目前拥有的代码:

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

I would really appreciate if someone could tell how this is achived.如果有人能说出这是如何实现的,我将不胜感激。

Thank you!谢谢!

Simply attach ItemDecoration with the recycler view to achieve this.只需将ItemDecoration附加到回收站视图即可实现此目的。

Step 1 : Add this Item Decoration class第 1 步:添加此项目装饰 class

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
}

} }

Step 2 : Attach BottomItemDecoration with the recycler view第 2 步:将BottomItemDecoration附加到回收站视图

recyclerView.addItemDecoration(BottomItemDecoration())

No change is required in the layout XML file.无需更改布局 XML 文件。

I think you can create 2 ViewType in your RecyclerView's Adapter with one is the actual content, the another is a blank one.我认为您可以在 RecyclerView 的Adapter中创建 2 个ViewType ,其中一个是实际内容,另一个是空白的。 In the Adapter , you override the method getItemViewType(int position) (This method is used to decide which view holder to use).Adapter中,您覆盖方法getItemViewType(int position) (此方法用于决定使用哪个视图持有者)。 Note that : in this method, we only return the second view holder (the blank one) when the Recycler view scrolls to the last item ( position == yourList.size() -1 ).请注意:在此方法中,我们仅在 Recycler 视图滚动到最后一项时返回第二个视图持有者(空白的)( position == yourList.size() -1 )。 Here an example:这里有一个例子:

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

I think you can use this approach, possibly duplicate: How to allow scrolling the last item of recyclerview above of the FAB?我认为您可以使用这种方法,可能重复: 如何允许滚动FAB上方的recyclerview的最后一项?

 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();
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM