【问题标题】:Android Gridview Row Height IssueAndroid Gridview 行高问题
【发布时间】:2014-09-10 03:44:34
【问题描述】:

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

例如,我在片段 fragment 1 中打开了一个成就类别列表,当您选择一个类别时,会打开一个新片段,其中包含 fragment 2 中的子类别列表。如果第二个较大,例如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]);
}

}

我已经尝试了两种成就Adapter.notifyDataSetChanged();和成就列表视图.invalidateViews();但都没有强迫它调整大小。

AchievementAdapter.java 公共类成就适配器扩展 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;
    }

}

【问题讨论】:

    标签: android android-layout android-gridview


    【解决方案1】:

    我能想到的一个愚蠢的方法是在设置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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多