一个自定义的RecyclerView layout manager
wjka6798
9年前
一个自定义的RecyclerView layout manager ,可以将图片按照网格布局,并保持图片的长宽比。
使用说明:
通过Gradle下载最新的版本:
repositories { maven { url 'https://github.com/500px/greedo-layout-for-android/raw/master/releases/' } } dependencies { compile 'com.fivehundredpx:greedo-layout:1.0.0' }
要了解GreedoLayout完整的用法参见sample 项目。下面是几个特殊的步骤:
// Create an instance of the GreedoLayoutManager and pass it to the RecyclerView MyRecyclerAdapter recyclerAdapter = new MyRecyclerAdapter(this); GreedoLayoutManager layoutManager = new GreedoLayoutManager(recyclerAdapter); RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(recyclerAdapter); // Set the max row height in pixels layoutManager.setMaxRowHeight(300); // If you would like to add spacing between items (Note, MeasUtils is in the sample project) int spacing = MeasUtils.dpToPx(4, this); recyclerView.addItemDecoration(new GreedoSpacingItemDecoration(spacing));
然后在你的RecyclerView adapter或者你选择的其它类中实现SizeCalculatorDelegate。这个实现将被传递到上面的 layout manager 。
public class MyRecyclerAdapter extends RecyclerView.Adapter<ViewHolder> implements SizeCalculatorDelegate { @Override public double aspectRatioForIndex(int index) { // Return the aspect ratio of your image at the given index } }