開源項目:https://github.com/etsy/AndroidStaggeredGrid
分享一下我用過之後,覺得最關鍵的地方。
在給出的demo中有一個集合,記錄每個位置的HeightRatio。設置Dyn/amicHeightTextView和DynamicHeightImageView的HeightRatio來控制顯示的高度。
比如在DynamicHeightImageView中有一段代碼來設置p_w_picpathview的顯示高度:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mHeightRatio > 0.0) {
// set the p_w_picpath views size
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) (width * mHeightRatio);
setMeasuredDimension(width, height);
}
else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
瞭解這幾點之後,就能夠顯示出高低不一樣的圖片效果