可以轻松地为您RecyclerViews创建分隔符的Android库:Dividers
jopen
9年前
Dividers是一个Android库可以轻松地为您RecyclerViews创建分隔符。It supports a wide range of dividers from simple ones that apply to all your items equally to a system of selectors that apply different styles to each item.
Screenshots
Usage
The most easy way to start usingDividersis to create aDividerItemDecorationwith a layer and provide it to yourRecyclerViewas follows:
// Create a drawable for your divider Drawable exampleDrawable = getResources().getDrawable(R.drawable.example_drawable); // Create a DividerItemDecoration instance with a single layer and add it to your recycler view RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(new Layer(DividerBuilder.get().with(exampleDrawable).build())); recyclerView.addItemDecoration(itemDecoration);
If you want to use all the features ofDividersfollow this steps:
- Create a collection of instances ofLayerwith the help ofLayersBuilderto define the drawables you want to apply. EachLayeris composed of:
- An implementation of theSelectorinterface, defining which items are going to be affected by the layer. You can use one of the multiple implementations that come in the library or create your own.
public class HighRatingMovieSelector implements Selector { private final List<Movie> movies; private final int maxHighRating; public HighRatingMovieSelector(List<Movie> movies, int maxHighRating) { this.movies = movies; this.maxHighRating = maxHighRating; } @Override public boolean isPositionSelected(Position position) { return movies.get(position.getAbsoluteIndex()).getRating() >= maxHighRating; } @Override public EnumSet<Direction> getDirectionsByPosition(Position position) { return EnumSet.allOf(Direction.class); } }