My question
How can I get my ItemDecoration
to "update" the item offsets for other views when I remove a given view from my adapter and then call notifyItemRemoved()
?
In my particular situation, I have an ItemDecoration
that provides a small amount of top offset to every item, and a large amount of bottom offset to the last item only. When I remove the last item, the view that becomes the new last item does not have any bottom offset.
Before and after:
If I then scroll the list around, when I return to the bottom of the list, my "new" last item has the correct offsets. It's only an issue for as long as the "new" last item remains visible on the screen.
What I've tried
If I change my notifyItemRemoved()
call to notifyDataSetChanged()
, the new last item will have the correct item offsets applied, but I lose the built-in animation I get from notifyItemRemoved()
.
If I keep using notifyItemRemoved()
and additionally call notifyItemChanged()
on the previous item, then I will get the correct item offsets but the animation is somewhat janky; the second-to-last item (before the removal) seems to fade out and then in again (in this screenshot, the card saying "item 19" was just removed):
I know that I can create a large space at the end of my list by applying bottom padding to my <RecyclerView>
tag and specifying android:clipToPadding="false"
, but this solution is not acceptable for various reasons.
To reproduce
I originally encountered this issue in a much larger app I'm working on, but here's a tiny app that demonstrates the issue.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MyAdapter adapter = new MyAdapter(20);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.removeItemAt(adapter.getItemCount() - 1);
}
});
RecyclerView recycler = findViewById(R.id.recycler);
recycler.setLayoutManager(new LinearLayoutManager(this));
recycler.addItemDecoration(new MyItemDecoration());
recycler.setAdapter(adapter);
}
private static class MyItemDecoration extends RecyclerView.ItemDecoration {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int viewPosition = parent.getChildViewHolder(view).getLayoutPosition();
int lastPosition = state.getItemCount() - 1;
outRect.top = 20;
outRect.bottom = (viewPosition == lastPosition) ? 200 : 0;
}
}
private static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private final List<String> list;
private MyAdapter(int count) {
this.list = new ArrayList<>();
for (int i = 0; i < count; ++i) {
list.add("" + i);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.text.setText("item: " + list.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
private void removeItemAt(int position) {
if (list.size() > 0) {
list.remove(position);
notifyItemRemoved(position);
}
}
}
private static class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView text;
public MyViewHolder(View itemView) {
super(itemView);
this.text = itemView.findViewById(R.id.text);
}
}
}
activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#eee">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="REMOVE LAST ITEM"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_margin="4dp">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_margin="8dp"
android:gravity="center"
android:textColor="#000"
android:textSize="16sp"/>
</android.support.v7.widget.CardView>