简体   繁体   English

Android Gridview行高问题

[英]Android Gridview Row Height Issue

I have a Gridview that I don't want a constant row height. 我有一个Gridview,我不需要恒定的行高。 I want the row height to vary to accommodate the tallest content in each row. 我希望行高变化以适应每一行中最高的内容。 I am currently using https://stackoverflow.com/a/7568226/1149456 (modified so i can resume with multiple adapters) as my solution but I have ran into a problem. 我目前正在使用https://stackoverflow.com/a/7568226/1149456 (已修改,因此我可以使用多个适配器进行恢复)作为我的解决方案,但我遇到了问题。 The rows are all re-sized correctly until I open in a second fragment. 在我打开第二个片段之前,所有行的大小都会正确调整。

For example, I open a list of achievement categories in a fragment fragment 1 and when you select a category a new fragment is opened with a list of sub-categories in fragment 2 . 例如,我在片段片段1中打开一个成就类别列表,当您选择一个类别时,将打开一个新片段,其中包含片段2中的子类别列表。 The first item is not re-sized if the second is larger such as here in this screenshot . 如果第二项较大(例如此屏幕截图中的此处),则不会调整第一项的大小。

If i open fragment 2 first it re-sizes correctly, or if i scroll up and down it resizes correctly like this screenshot . 如果我先打开片段2,则它会正确调整大小,或者如果我上下滚动,它会正确调整大小,如此屏幕截图所示

AutoMeasureGridView.java 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);
}

} }

The original code calls adapter.measureItems(columnWidth); 原始代码调用adapter.measureItems(columnWidth); but i found the code for the most part worked without it and I would be unable to reuse the code with multiple adapters by calling the custom function. 但是我发现没有它,大部分代码都可以工作,而且我无法通过调用自定义函数来在多个适配器中重用代码。

GridViewItemLayout.java public class GridViewItemLayout extends LinearLayout { 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]);
}

} }

I have tried both achievementAdapter.notifyDataSetChanged(); 我都尝试了AchievementAdapter.notifyDataSetChanged(); and achievementListView.invalidateViews(); 和AchievementListView.invalidateViews(); but neither forced it to resize. 但都没有强迫它调整大小。

AchievementAdapter.java public class AchievementAdapter extends ArrayAdapter { AchievementAdapter.java公共类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;
    }

} }

A stupid way that I can think of is to set the layout height of the gridview manually when it is setting up. 我可以想到的一种愚蠢的方法是在设置gridview时手动设置它的布局高度。 Something like 就像是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM