Install RecyclerItemDecoration
masterAdd the following dependency to your build.gradle file to include the library in your Android project.
dependencies {
compile 'com.dinuscxj:recycleritemdecoration:1.0.0'
}repository·master·Indexed 19 days ago
https://github.com/dinuscxj/recycleritemdecorationAn Android library providing specialized ItemDecorations for RecyclerView. It includes PinnedHeaderDecoration for sticky headers, ShaderItemDecoration for edge gradients, LinearDividerItemDecoration and GridDividerItemDecoration for dividers, and LinearOffsetsItemDecoration and GridOffsetsItemDecoration for item spacing.
Add the following dependency to your build.gradle file to include the library in your Android project.
dependencies {
compile 'com.dinuscxj:recycleritemdecoration:1.0.0'
}ShaderItemDecoration allows you to add custom top, bottom, left, or right shaders (gradients) to the edges of your RecyclerView.
Simple usage: Pass bitwise flags to the constructor to specify which edges should have shaders and use setShaderTopDistance or setShaderBottomDistance to control the gradient spread.
Complex usage: Use registerBottomShaderCreator (or similar) to provide a custom ShaderCreator that returns a Shader object.
//simple usage
ShaderItemDecoration shaderItemDecoration = new ShaderItemDecoration(getActivity(),
ShaderItemDecoration.SHADER_BOTTOM
| ShaderItemDecoration.SHADER_TOP
| ShaderItemDecoration.SHADER_RIGHT
| ShaderItemDecoration.SHADER_LEFT);
shaderItemDecoration.setShaderTopDistance(shaderTopDistance);
shaderItemDecoration.setShaderBottomDistance(shaderBottomDistance);
//complex usage
ShaderItemDecoration shaderItemDecoration = new ShaderItemDecoration(getActivity(),
ShaderItemDecoration.SHADER_BOTTOM | ShaderItemDecoration.SHADER_TOP);
shaderItemDecoration.registerBottomShaderCreator(new ShaderItemDecoration.ShaderCreator() {
@Override
public Shader createShader(RecyclerView parent) {
return null;
}
});GridOffsetsItemDecoration applies spacing to items in a grid layout.
Simple usage: Set global vertical and horizontal offsets using setVerticalItemOffsets(offsets) and setHorizontalItemOffsets(offsets).
Complex usage: Use registerTypeDrawable (note: the API name in the README is registerTypeDrawable but it takes an OffsetsCreator) to provide custom vertical and horizontal integer offsets for specific ViewTypes. Use setOffsetEdge(true) and setOffsetLast(true) to manage boundary offsets.
//simple usage
GridOffsetsItemDecoration offsetsItemDecoration = new GridOffsetsItemDecoration(
GridOffsetsItemDecoration.GRID_OFFSETS_HORIZONTAL);
offsetsItemDecoration.setVerticalItemOffsets(verticalItemOffsets);
offsetsItemDecoration.setHorizontalItemOffsets(horizontalItemOffsets);
//complex usage
GridOffsetsItemDecoration offsetsItemDecoration = new GridOffsetsItemDecoration(
GridOffsetsItemDecoration.GRID_OFFSETS_HORIZONTAL);
offsetsItemDecoration.registerTypeDrawable(getItemViewType(ItemAnimal.class),
new GridOffsetsItemDecoration.OffsetsCreator() {
@Override
public int createVertical(RecyclerView parent, int adapterPosition) {
return 30;
}
@Override
public int createHorizontal(RecyclerView parent, int adapterPosition) {
return 30;
}
}
);
offsetsItemDecoration.setOffsetEdge(true);
offsetsItemDecoration.setOffsetLast(true);LinearOffsetsItemDecoration applies offsets (spacing) to items in a linear list.
Simple usage: Set a global ItemOffset using setItemOffsets(itemOffsets).
Complex usage: Use registerTypeOffset to provide custom integer offsets for specific ViewTypes via an OffsetsCreator. You can also use setOffsetEdge(true) and setOffsetLast(true) to control behavior at the boundaries of the list.
//simple usage
LinearOffsetsItemDecoration offsetsItemDecoration = new LinearOffsetItemDecoration(
LinearOffsetItemDecoration.LINEAR_OFFSETS_HORIZONTAL);
offsetsItemDecoration.setItemOffsets(itemOffsets);
//complex usage
LinearOffsetsItemDecoration offsetsItemDecoration = new LinearOffsetItemDecoration(
LinearOffsetItemDecoration.LINEAR_OFFSETS_HORIZONTAL);
offsetsItemDecoration.registerTypeOffset(getItemViewType(ItemAnimal.class),
new LinearOffsetItemDecoration.OffsetsCreator() {
@Override
public int create(RecyclerView parent, int adapterPosition) {
return 30;
}
}
);
offsetsItemDecoration.setOffsetEdge(true);
offsetsItemDecoration.setOffsetLast(true);Use PinnedHeaderDecoration to make specific ViewType items stick to the top of the RecyclerView as a header. You must register the view type using registerTypePinnedHeader and provide a PinnedHeaderCreator to determine if a specific position should act as a header.
PinnedHeaderDecoration pinnedHeaderDecoration = new PinnedHeaderDecoration();
pinnedHeaderDecoration.registerTypePinnedHeader(getItemViewType(ItemTitle.class),
new PinnedHeaderDecoration.PinnedHeaderCreator() {
@Override
public boolean create(RecyclerView parent, int adapterPosition) {
return true;
}
}
);GridDividerItemDecoration provides dividers for grid layouts.
Simple usage: Set vertical and horizontal dividers globally using setVerticalDivider(divider) and setHorizontalDivider(divider).
Complex usage: Use registerTypeDrawable to provide specific Drawables for vertical and horizontal dividers based on the item's ViewType using createVertical and createHorizontal.
//simple usage
GridDividerItemDecoration dividerItemDecoration = new GridDividerItemDecoration(getActivity(),
GridDividerItemDecoration.GRID_DIVIDER_VERTICAL);
dividerItemDecoration.setVerticalDivider(verticalDivider);
dividerItemDecoration.setHorizontalDivider(horizontalDivider);
//complex usage
GridDividerItemDecoration dividerItemDecoration = new GridDividerItemDecoration(getActivity(),
GridDividerItemDecoration.GRID_DIVIDER_VERTICAL);
dividerItemDecoration.registerTypeDrawable(getItemViewType(ItemAnimal.class),
new GridDividerItemDecoration.DrawableCreator() {
@Override
public Drawable createVertical(RecyclerView parent, int adapterPosition) {
return getResources().getDrawable(R.drawable.bg_animal_divider);
}
@Override
public Drawable createHorizontal(RecyclerView parent, int adapterPosition) {
return getResources().getDrawable(R.drawable.bg_animal_divider);
}
}
);LinearDividerItemDecoration adds dividers to a list. You can specify the orientation (e.g., LINEAR_DIVIDER_VERTICAL).
Simple usage: Set a single Drawable for all items using setDivider(divider).
Complex usage: Use registerTypeDrawable to provide different dividers for different ViewTypes by implementing DrawableCreator.
//simple usage
LinearDividerItemDecoration dividerItemDecoration = new LinearDividerItemDecoration(
getActivity(), LinearDividerItemDecoration.LINEAR_DIVIDER_VERTICAL);
dividerItemDecoration.setDivider(divider);
//complex usage
LinearDividerItemDecoration dividerItemDecoration = new LinearDividerItemDecoration(
getActivity(), LinearDividerItemDecoration.LINEAR_DIVIDER_VERTICAL);
dividerItemDecoration.registerTypeDrawable(getItemViewType(ItemAnimal.class),
new LinearDividerItemDecoration.DrawableCreator() {
@Override
public Drawable create(RecyclerView parent, int adapterPosition) {
return getResources().getDrawable(R.drawable.bg_animal_divider);
}
}
);