問題描述:
有時我們使用GridView會面對
類似這種情況。
這是是不是一臉愣逼,我們理想情況是把他變成這樣

保證同一行的item都是一樣高這樣就美觀許多了
注意:上面的兩張圖片是盜圖,用來作為效果觀看的,自己在手機上截圖比較麻煩(其實就是懶 哈哈~)
解決方案:
第一種方式 (不推薦): 不怎么靈活,比較麻煩 ,自己改寫adapter中的代碼 參考這個:https://blog.csdn.net/zhi890701/article/details/50510822
第二種方式(推薦):因為博主用的是通用的adapter自己改起來比較麻煩還要測試,所以就看看有沒有重寫GridView的,正好被博主找到了,很方便
https://github.com/JJdeGroot/AutoGridView
使用起來也方便里面就寫了一個AutoGridView.java直接拷貝到項目中就可以和普通GridView一樣使用了懶的話就直接拷貝我這代碼就行了 代碼:
import android.content.Context; import android.content.res.Configuration; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.GridView; import android.widget.ListAdapter; /** * Automatically calculates the ideal for each row * @author JJ * */ public class AutoGridView extends GridView { private static final String TAG = "AutoGridView"; private int numColumnsID; private int previousFirstVisible; private int numColumns = 1; public AutoGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } public AutoGridView(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public AutoGridView(Context context) { super(context); } /** * Sets the numColumns based on the attributeset */ private void init(AttributeSet attrs) { // Read numColumns out of the AttributeSet int count = attrs.getAttributeCount(); if(count > 0) { for(int i = 0; i < count; i++) { String name = attrs.getAttributeName(i); if(name != null && name.equals("numColumns")) { // Update columns this.numColumnsID = attrs.getAttributeResourceValue(i, 1); updateColumns(); break; } } } Log.d(TAG, "numColumns set to: " + numColumns); } /** * Reads the amount of columns from the resource file and * updates the "numColumns" variable */ private void updateColumns() { this.numColumns = getContext().getResources().getInteger(numColumnsID); } @Override public void setNumColumns(int numColumns) { this.numColumns = numColumns; super.setNumColumns(numColumns); Log.d(TAG, "setSelection --> " + previousFirstVisible); setSelection(previousFirstVisible); } @Override protected void onLayout(boolean changed, int leftPos, int topPos, int rightPos, int bottomPos) { super.onLayout(changed, leftPos, topPos, rightPos, bottomPos); setHeights(); } @Override protected void onConfigurationChanged(Configuration newConfig) { updateColumns(); setNumColumns(this.numColumns); } @Override protected void onScrollChanged(int newHorizontal, int newVertical, int oldHorizontal, int oldVertical) { // Check if the first visible position has changed due to this scroll int firstVisible = getFirstVisiblePosition(); if(previousFirstVisible != firstVisible) { // Update position, and update heights previousFirstVisible = firstVisible; setHeights(); } super.onScrollChanged(newHorizontal, newVertical, oldHorizontal, oldVertical); } /** * Sets the height of each view in a row equal to the height of the tallest view in this row. * @param firstVisible The first visible position (adapter order) */ private void setHeights() { ListAdapter adapter = getAdapter(); if(adapter != null) { for(int i = 0; i < getChildCount(); i+=numColumns) { // Determine the maximum height for this row int maxHeight = 0; for(int j = i; j < i+numColumns; j++) { View view = getChildAt(j); if(view != null && view.getHeight() > maxHeight) { maxHeight = view.getHeight(); } } //Log.d(TAG, "Max height for row #" + i/numColumns + ": " + maxHeight); // Set max height for each element in this row if(maxHeight > 0) { for(int j = i; j < i+numColumns; j++) { View view = getChildAt(j); if(view != null && view.getHeight() != maxHeight) { view.setMinimumHeight(maxHeight); } } } } } } }
還可以參考這個項目內容 https://github.com/etsy/AndroidStaggeredGrid 看介紹可以實現瀑布流哦 具體自行了解下
