RecyclerView装饰器在刷新时添加了额外的填充 [英] RecyclerView decorator adding extra padding on refresh

查看:63
本文介绍了RecyclerView装饰器在刷新时添加了额外的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我的项目中实施RecyclerView时遇到一个奇怪的问题.我有一个自定义装饰器,以实现一致的顶部和底部填充以及元素之间的填充值完全不同.每当我点击刷新时,即从后端获取数据并再次填充RecyclerView时,填充都会增加,并且每次刷新时都会不断增加.

So, I'm facing a weird problem while implementing RecyclerView in my project. I have a custom decorator to implement a consistent top and bottom padding and a rather different value of padding in between elements. Whenever I tap on refresh, i.e. fetch data from back-end and populate RecyclerView again, the padding increases and it keeps on increasing on each refresh.

当我单击刷新"(使AsyncTask再次执行)时,项目之间的间隔增加了.并且每次刷新时都会不断增加.

When I hit refresh (causing AsyncTask to execute again), the space between items increase. And it keeps on increase with each refresh.

我有一个像这样的典型RecyclerView

I have a typical RecyclerView like this

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="false"
    android:focusableInTouchMode="false">
</android.support.v7.widget.RecyclerView>

使用以下布局将CardView填充到RecyclerView中:

Populating content inside RecyclerView is CardView with the following layout:

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
>

    <RelativeLayout
        android:id="@+id/itemLinearTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="17dp"
        android:paddingRight="10dp"
        android:paddingLeft="10dp">

        <TextView
            android:id="@+id/tv1"
            android:textColor="@color/logo_black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium" />


        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/tv1"
            android:textColor="@color/logo_black"
            android:layout_alignParentRight="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <!-- More elements -->
    </RelativeLayout>
</android.support.v7.widget.CardView>

我正在像这样初始化和填充RecyclerView:

I am initializing and populating RecyclerView like this:

// In PostExecute of AsyncTask
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.recyclerview);
RecycleViewAdapter adapter = new FuelPricesRecycleViewAdapter(data);
rv.setAdapter(adapter);
rv.addItemDecoration(new RecyclerViewItemDecoration(30, item_count - 1));

这是我的RecyclerViewItemDecoration类的样子:

And this is what my RecyclerViewItemDecoration class looks like:

public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration {
    private int space = 0;
    private int item_count = 0;
    private int ADDITIONAL_PADDING = 20;

    public RecyclerViewItemDecoration(int space, int item_count) {
        this.space = space;
        this.item_count = item_count;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if(parent.getChildPosition(view) != item_count)
             outRect.bottom = space;

         // Add top margin only for the first item to avoid double space between items
         if(parent.getChildPosition(view) == 0)
             outRect.top = space + ADDITIONAL_PADDING;
         if(parent.getChildPosition(view) == item_count)
             outRect.bottom = space + ADDITIONAL_PADDING;
     }
 }

谢谢您的帮助!

推荐答案

重置新数据之前,您必须先删除商品装饰.

You have to remove the item decoration before reseting new data.

rv.removeItemDecoration(yourDecorator)

让我知道它是否有效!

这篇关于RecyclerView装饰器在刷新时添加了额外的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆