Multi divider in RecyclerView on Android.
Vertical | Horizontal |
---|---|
Inset | Dash |
---|---|
Grid | FullBleed |
---|---|
Position | Inverted |
---|---|
- Requirement
- SDK Version 16+
- (Use from Java)Kotlin Version 1.0.0+
Add dependencies
compile 'com.github.sakebook:MultiLineDivider:0.2.0@aar'
// Use from Java
compile "org.jetbrains.kotlin:kotlin-stdlib:kotlin_version"
Add to RecyclerView like in addItemDecoration
- Java
MultiLineDivider multiLineDivider = new MultiLineDivider(this, LinearLayout.VERTICAL);
recyclerView.addItemDecoration(multiLineDivider);
- Kotlin
val multiLineDivider = MultiLineDivider(this)
recyclerView.addItemDecoration(multiLineDivider)
And Implement VerticalDivider
in ViewHolder
- Java
public class YourViewHolder extends RecyclerView.ViewHolder implements VerticalDivider {
public YourViewHolder(View itemView) {
super(itemView);
}
@Override
public int getHeight() {
return this.itemView.getContext().getResources()
.getDimensionPixelSize(R.dimen.small_margin);
}
@Override
public int getDrawableRes() {
return R.drawable.custom_divider;
}
@Nullable
@Override
// android.support.v4.util.Pair
public Pair<Integer, Integer> getVerticalInset() {
return null;
}
}
- Kotlin
class YourViewHolder(view: View): RecyclerView.ViewHolder(view), VerticalDivider {
override val height = view.context.resources.getDimensionPixelSize(R.dimen.small_margin)
override val drawableRes = R.drawable.custom_divider
override val verticalInset: Pair<Int, Int>? = null
}
MultiLineDivider multiLineDivider = new MultiLineDivider(this, LinearLayout.HORIZONTAL);
and implements HorizontalDivider
Implements NoDivider
@Nullable
@Override
// android.support.v4.util.Pair
public Pair<Integer, Integer> getVerticalInset() {
int insetLeft = (int) resources.getDimension(R.dimen.list_padding);
int insetRight = 0;
return Pair.create(insetLeft, insetRight);
}
- Java
public class YourViewHolder extends RecyclerView.ViewHolder implements GridDivider {
public YourViewHolder(View itemView) {
super(itemView);
}
@Override
public int getPadding() {
return itemView.getContext().getResources().getDimensionPixelSize(R.dimen.tiny_margin);
}
@Override
public boolean getFullBleed() {
return false;
}
}
- Kotlin
class YourViewHolder(view: View): RecyclerView.ViewHolder(view), GridDivider {
override val padding = view.context.resources.getDimensionPixelSize(R.dimen.grid_padding)
override val fullBleed = true
}
- Java
public class YourViewHolder extends RecyclerView.ViewHolder implements PositionDivider {
public YourViewHolder(View itemView) {
super(itemView);
}
@NotNull
@Override
public List<Integer> getPositions() {
return Arrays.asList(2, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 42); // include "2"
}
@Override
public boolean getInverted() {
return false;
}
}
- Kotlin
class YourViewHolder(view: View): RecyclerView.ViewHolder(view), PositionDivider {
override val positions = listOf(2, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 42)
override val inverted = false
}
Implements PositionDivider
with inverted
true
Implements PositionDivider
and VerticalDivider
or HorizontalDivider
If you are using ProGuard you might need to add the following option:
-keep class com.sakebook.android.library.multilinedevider.*{ *; }
Sample in project sample/
Copyright (C) 2017 Shinya Sakemoto
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.