一個自定義的RecyclerView layout manager

wjka6798 8年前發布 | 16K 次閱讀 Android開發 移動開發

一個自定義的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
    }
}


項目地址: https://github.com/500px/greedo-layout-for-android

 本文由用戶 wjka6798 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!