Android Gridview行高问题

时间:2014-09-09 17:43:14

标签: android android-layout android-gridview

我有一个Gridview,我不想要一个恒定的行高。我希望行高可以变化以适应每行中最高的内容。我目前正在使用https://stackoverflow.com/a/7568226/1149456(已修改,因此我可以使用多个适配器恢复)作为我的解决方案,但我遇到了问题。所有行都正确地重新调整大小,直到我打开第二个片段。

例如,我在片段片段1 中打开成就类别列表,当您选择一个类别时,会打开一个新片段,其中包含片段2中的子类别列表< / strong>即可。如果第二个项目较大,则第一个项目不会重新调整大小,例如此screenshot中的项目。

如果我首先打开片段2,它会正确地重新调整大小,或者如果我向上和向下滚动它会像screenshot一样正确调整大小。

AutoMeasureGridView.java

public class AutoMeasureGridView extends GridView {
private ListAdapter mAdapter;

public AutoMeasureGridView(Context context) {
    super(context);
}

public AutoMeasureGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public AutoMeasureGridView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public void setAdapter(ListAdapter adapter) {
    this.mAdapter = adapter;

    super.setAdapter(adapter);
    super.setAdapter(this.mAdapter);
}

@Override
public ListAdapter getAdapter() {
    return this.mAdapter;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if(changed) {
        //TutorialAdapter adapter = (TutorialAdapter)getAdapter();
        int numColumns = getNumColumns();

        if(getAdapter() != null) {
            GridViewItemLayout.initItemLayout(numColumns, getAdapter().getCount());
        }

        /*if(numColumns > 1) {
            int columnWidth = getMeasuredWidth() / numColumns;
            adapter.measureItems(columnWidth);
        }*/
    }
    super.onLayout(changed, l, t, r, b);
}

}

原始代码调用 adapter.measureItems(columnWidth); ,但我发现大部分代码都没有它,我无法通过调用自定义函数重用多个适配器的代码

GridViewItemLayout.java     公共类GridViewItemLayout扩展了LinearLayout {

// Array of max cell heights for each row
private static int[] mMaxRowHeight;

// The number of columns in the grid view
private static int mNumColumns;

// The position of the view cell
private int mPosition;

// Public constructor
public GridViewItemLayout(Context context) {
    super(context);
}

// Public constructor
public GridViewItemLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

/**
 * Set the position of the view cell
 * @param position
 */
public void setPosition(int position) {
    mPosition = position;
}

/**
 * Set the number of columns and item count in order to accurately store the
 * max height for each row. This must be called whenever there is a change to the layout
 * or content data.
 * 
 * @param numColumns
 * @param itemCount
 */
public static void initItemLayout(int numColumns, int itemCount) {
    mNumColumns = numColumns;
    mMaxRowHeight = new int[itemCount];
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    // Do not calculate max height if column count is only one
    if(mNumColumns <= 1 || mMaxRowHeight == null) {
        return;
    }

    // Get the current view cell index for the grid row
    int rowIndex = mPosition / mNumColumns;
    // Get the measured height for this layout
    int measuredHeight = getMeasuredHeight();
    // If the current height is larger than previous measurements, update the array
    if(measuredHeight > mMaxRowHeight[rowIndex]) {
        mMaxRowHeight[rowIndex] = measuredHeight;
    }
    // Update the dimensions of the layout to reflect the max height
    setMeasuredDimension(getMeasuredWidth(), mMaxRowHeight[rowIndex]);
}

}

我尝试过两个achievementAdapter.notifyDataSetChanged();和achievementListView.invalidateViews();但都没有强迫它调整大小。

AchievementAdapter.java         public class AchievementAdapter扩展了ArrayAdapter {

    private final Context context;
    private final ArrayList<AchievementsItem> swtorAchievements;
    int AdvancedPos1;
    int AdvancedPos2;
    String type;

    public AchievementAdapter(Context context, ArrayList<AchievementsItem> swtorAchievements, String type) {
        super(context, R.layout.achievement_row, swtorAchievements);

        this.context = context;
        this.swtorAchievements = swtorAchievements;
        this.type = type;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = null;
        final AchievementsItem item = swtorAchievements.get(position);

        if(item.isGroupHeader()){
            rowView = inflater.inflate(R.layout.advanced_class_tab3_header, parent, false);
            TextView txtHeader = (TextView) rowView.findViewById(R.id.txtAbilityTitle);
            txtHeader.setText(item.getTitle());

        } else {
            rowView = inflater.inflate(R.layout.achievement_row, parent, false);

            TextView txtViewCategory1 = (TextView) rowView.findViewById(R.id.txtCategory1);
            TextProgressBar txtViewProgress = (TextProgressBar) rowView.findViewById(R.id.progressBarWithText);

            if (item != null) {

                if (type.equals("")) {
                    txtViewCategory1.setText(item.getCategory1());
                } else if (type.equals("category1")) {
                    txtViewCategory1.setText(item.getCategory1());
                } else if (type.equals("category2")) {
                    txtViewCategory1.setText(item.getCategory2());
                } else if (type.equals("category3")) {
                    txtViewCategory1.setText(item.getCategory3());

                }


                txtViewProgress.setText("0 / " + item.getCount());
            }

        }
        return rowView;
    }

}

1 个答案:

答案 0 :(得分:0)

我能想到的一种愚蠢的方法是在设置时手动设置gridview的布局高度。像

这样的东西
if(newHeight>oldHeight)  {
     gridview.setlayoutparams();       // here you will set the layout height to the new height
}

fragment.addFragment();                // replace the old gridview element with the new element