防止 GridView 缩放
Prevent GridView from scaling
如何通过自动调整高度来防止 gridView 滚动?我希望所有项目,无论我添加到 gridView 的项目有多少都保留在屏幕上而无需滚动。这可能吗?
这是我目前的UI。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tv_header"
android:fadingEdge="none"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:listSelector="#00000000"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</LinearLayout>
我确实尝试过向根添加一个 weightSum 并向 gridView 添加权重,但它仍然需要滚动。
更新:我也试过使用自定义网格视图。这没有用,但无论如何这是我的尝试。
public class CustomGridView extends GridView {
public CustomGridView(Context context) {
super(context);
}
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST));
getLayoutParams().height = getMeasuredHeight();
}
}
提前致谢!
我已经找到了答案。您可以使用
设置适配器中每个项目的高度
view.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, resizeValue));
resizeValue 是您要将行调整到的大小。要获取 resizeValue,您可以根据相对于设备屏幕尺寸的计算将 mResizeValue 传递给适配器。像
resizevalue = this.getResources().getDisplayMetrics().heightPixels / (NUM_COLS);
我想出了一些其他方法来根据屏幕尺寸计算每行的高度,然后做类似的事情,但是,这需要您在设置适配器后进行这些计算,然后将更改更新到适配器.它似乎效率较低,但我也会分享该方法。
private void resizeGridView(GridView gridView, int items, int columns) {
ViewGroup.LayoutParams params = gridView.getLayoutParams();
int oneRowHeight = gridView.getHeight();
int rows = (int) (items / columns);
params.height = oneRowHeight * rows;
gridView.setLayoutParams(params);
}
然后在设置适配器后使用
gridView.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (!gridViewResized) {
gridViewResized = true;
resizeGridView(gridView, numItems, numColumns);
}
}
});
如何通过自动调整高度来防止 gridView 滚动?我希望所有项目,无论我添加到 gridView 的项目有多少都保留在屏幕上而无需滚动。这可能吗?
这是我目前的UI。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tv_header"
android:fadingEdge="none"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:listSelector="#00000000"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</LinearLayout>
我确实尝试过向根添加一个 weightSum 并向 gridView 添加权重,但它仍然需要滚动。
更新:我也试过使用自定义网格视图。这没有用,但无论如何这是我的尝试。
public class CustomGridView extends GridView {
public CustomGridView(Context context) {
super(context);
}
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST));
getLayoutParams().height = getMeasuredHeight();
}
}
提前致谢!
我已经找到了答案。您可以使用
设置适配器中每个项目的高度view.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, resizeValue));
resizeValue 是您要将行调整到的大小。要获取 resizeValue,您可以根据相对于设备屏幕尺寸的计算将 mResizeValue 传递给适配器。像
resizevalue = this.getResources().getDisplayMetrics().heightPixels / (NUM_COLS);
我想出了一些其他方法来根据屏幕尺寸计算每行的高度,然后做类似的事情,但是,这需要您在设置适配器后进行这些计算,然后将更改更新到适配器.它似乎效率较低,但我也会分享该方法。
private void resizeGridView(GridView gridView, int items, int columns) {
ViewGroup.LayoutParams params = gridView.getLayoutParams();
int oneRowHeight = gridView.getHeight();
int rows = (int) (items / columns);
params.height = oneRowHeight * rows;
gridView.setLayoutParams(params);
}
然后在设置适配器后使用
gridView.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (!gridViewResized) {
gridViewResized = true;
resizeGridView(gridView, numItems, numColumns);
}
}
});