Last active
November 18, 2022 03:35
-
-
Save chowaikong/20720a959d11358fc79f to your computer and use it in GitHub Desktop.
VerticalSpaceItemDecoration for RecyclerView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Rect; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration { | |
private final int mVerticalSpaceHeight; | |
/** | |
* @param mVerticalSpaceHeight height value in dp | |
*/ | |
public VerticalSpaceItemDecoration(int mVerticalSpaceHeight) { | |
this.mVerticalSpaceHeight = dp2px(mVerticalSpaceHeight); | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, | |
RecyclerView.State state) { | |
outRect.bottom = mVerticalSpaceHeight; | |
} | |
private int dp2px(float dp) { | |
DisplayMetrics displayMetrics = OAApplication.getInstance().getResources().getDisplayMetrics(); | |
return Math.round(dp * (displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment