【问题标题】:Gridview show according to actual height within scroll viewGridview 根据滚动视图中的实际高度显示
【发布时间】:2013-12-31 22:28:09
【问题描述】:
 <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/titleBarBG"
        android:layout_alignParentLeft="true" >

    <RelativeLayout
        android:id="@+id/scrollContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         >

    <GridView
        android:id="@+id/issueList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/archiveTitle"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/customshape"
        android:numColumns="3"
        android:overScrollMode="never"
        android:scrollbars="none" >
    </GridView>

</RelativeLayout>
 </ScrollView>

我想创建一个类似于表格的网格视图。例如,网格的大小将增加,这将使 gridview 更高。而不是隐藏额外的内容,我希望网格视图显示所有内容并在有额外内容时扩展高度

如何实现?谢谢

【问题讨论】:

  • 为什么不希望GridView提供滚动? ScrollView 为您提供了 GridView 没有的哪些滚动功能?
  • 谢谢,唯一的问题是gridview的高度应该随着项目数的增加而增加
  • 您可能正在寻找 [ExpandableHeightGridView][1] [1]:stackoverflow.com/questions/8481844/gridview-height-gets-cut/…
  • 动态设置网格视图的布局参数(高度)是否更容易?谢谢

标签: java android android-layout gridview android-gridview


【解决方案1】:

这是稍微清理过的版本:Grid of images inside ScrollView

WrappedGridView.java:

/**
 * Use this class when you want a gridview that doesn't scroll and automatically
 * wraps to the height of its contents
 */
public class WrappedGridView extends GridView {
    public WrappedGridView(Context context) {
        super(context);
    }

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

像 GridLayout 一样包含在 XML 布局中。使用适配器为其提供视图。

据我所知,这是目前最简单的解决方案。在处理包装的框架中没有其他可用的视图。如果有人提供一个优雅的、自动调整大小的表格视图,那就太好了。为此目的修改GridView.java 可能不是一个坏主意。

或者,您可能会发现“FlowLayout”项目之一是可以接受的。有android-flowlayoutFlowLayout。这些比简单的网格要灵活一些,而且我认为效率要低一些。您也不需要为他们提供适配器。

【讨论】:

    【解决方案2】:
    public class MyGridView extends GridView {
    
        public MyGridView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyGridView(Context context) {
            super(context);
        }
    
        public MyGridView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);
        }
    }
    

    【讨论】:

    • 你应该解释一下。
    • 干得好,伙计...感谢这段代码,我尝试了很多,但最后我没有找到任何东西,我再次找到了,谢谢
    • 谢谢老兄.. 节省时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多