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

当我点击刷新(导致AsyncTask再次执行)时,项目之间的空间增加.并且每次刷新都会不断增加.

我有一个像这样的典型RecyclerView
<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>
Run Code Online (Sandbox Code Playgroud)
在RecyclerView中填充内容是CardView,具有以下布局:
<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>
Run Code Online (Sandbox Code Playgroud)
我正在初始化和填充RecyclerView,如下所示:
// 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));
Run Code Online (Sandbox Code Playgroud)
这就是我的RecyclerViewItemDecoration类的样子:
public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration {
private int …Run Code Online (Sandbox Code Playgroud) android ×1