Skip to content

Instantly share code, notes, and snippets.

@StKotok
Created January 15, 2018 17:00
Show Gist options
  • Save StKotok/9da3354793e9797d777ef5d9c9783d93 to your computer and use it in GitHub Desktop.
Save StKotok/9da3354793e9797d777ef5d9c9783d93 to your computer and use it in GitHub Desktop.
my
/*
Copyright 2018 *********.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package co.neatapps.std;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class STDItemDecoration extends RecyclerView.ItemDecoration {
private final int backgroundColor;
private Drawable background;
public STDItemDecoration(int backgroundColor) {
this.backgroundColor = backgroundColor;
}
@Override
public void onDraw(Canvas canvas, RecyclerView recyclerView, RecyclerView.State rvState) {
if (recyclerView.getItemAnimator().isRunning()) {
if (background == null) background = new ColorDrawable(backgroundColor);
// find nearby moving views
View topShiftingView = null;
View bottomShiftingView = null;
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
for (int i = 0; i < layoutManager.getChildCount(); i++) {
View child = layoutManager.getChildAt(i);
if (child.getTranslationY() < 0) { // view is shifting down
topShiftingView = child;
} else if (child.getTranslationY() > 0 && bottomShiftingView == null) {
bottomShiftingView = child;
}
}
Rect rect = new Rect(0, 0, recyclerView.getWidth(), 0);
setTopAndBottomBetweenMovingViews(rect, topShiftingView, bottomShiftingView);
background.setBounds(rect);
background.draw(canvas);
}
super.onDraw(canvas, recyclerView, rvState);
}
private void setTopAndBottomBetweenMovingViews(Rect rect, View topShiftingView, View bottomShiftingView) {
if (topShiftingView != null && bottomShiftingView != null) {
// views are shifting down AND shifting up
rect.top = topShiftingView.getBottom() + (int) topShiftingView.getTranslationY();
rect.bottom = bottomShiftingView.getTop() + (int) bottomShiftingView.getTranslationY();
} else if (topShiftingView != null) {
// views are shifting down
rect.top = topShiftingView.getBottom() + (int) topShiftingView.getTranslationY();
rect.bottom = topShiftingView.getBottom();
} else if (bottomShiftingView != null) {
// views are shifting up
rect.top = bottomShiftingView.getTop();
rect.bottom = bottomShiftingView.getTop() + (int) bottomShiftingView.getTranslationY();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment