简体   繁体   English

向 GridView 添加页眉/页脚

[英]Adding header/footer to GridView

I'm having a hard time to design a GridView layout with header and footer like this:我很难设计一个带有页眉和页脚的GridView布局,如下所示:

布局

What I did is, create a ListView with just one item which is the GridView and add the header/footer using addHeaderView() and addFooterView() .我所做的是,创建一个ListView其中只有一个项目是GridView并使用addHeaderView()addFooterView()添加页眉/页脚。 The problem is that the GridView doesn't show the whole items.问题是GridView没有显示整个项目。 I've disabled the GridView scrolling using:我使用以下方法禁用了GridView滚动:

  gridview.setOnTouchListener(new View.OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_MOVE){
                    return true;
                }
                return false;
            }

        });

Is there a way to show the make the GridView show all items?有没有办法显示使GridView显示所有项目? I've tried setting the height of the GridView to wrap_content but it doesn't work.我试过将GridView的高度设置为wrap_content但它不起作用。 Here is the layout for the GridView :这是GridView的布局:

<GridView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="2"
    android:background="@color/main_gray"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center">

</GridView>

I know it is not right to put gridview inside a listview but the client requested for this kind of layout..我知道将 gridview 放在列表视图中是不对的,但是客户端要求这种布局..

Here is a good starting point :这是一个很好的起点:

https://android.googlesource.com/platform/packages/apps/Gallery2/+/idea133/src/com/android/photos/views/HeaderGridView.java https://android.googlesource.com/platform/packages/apps/Gallery2/+/idea133/src/com/android/photos/views/HeaderGridView.java

This class handles headers very well, exaclty the same way as ListView does.此类处理标题非常好,与ListView处理方式完全相同。

You should manage to add footer handling as well, but it'a a bit more complicated, as you have to put placeholders depending on the number of elements you have in your Gridview .您也应该设法添加页脚处理,但它有点复杂,因为您必须根据Gridview的元素数量放置占位符。 And as the height of a Gridview row is based on the height of its last element, you will have to get the height of the last item of your GridView由于Gridview行的高度基于其最后一个元素的高度,因此您必须获得GridView最后一项的高度

convertView.setVisibility(View.INVISIBLE);
convertView.setMinimumHeight(lastItemHeight);

I use this to calculate last item height.我用它来计算最后一个项目的高度。 It's probably not optimal, but it works for my use cases :它可能不是最佳的,但它适用于我的用例:

View v = mAdapter.getView(adjPosition, convertView, parent);

//measure last item height for placeholders before footer views
v.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                lastItemHeight = v.getMeasuredHeight();
lastItemHeight = v.getMeasuredHeight();

Check This Out Hope it Resolve Your Problem看看这个希望它能解决你的问题

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:background="#AAFFDD" >
    </LinearLayout>

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.97"
        android:background="@color/main_gray"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </GridView>

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:background="#FADFAE" >
    </LinearLayout>

</LinearLayout>

gridview 类不提供 addheaderview() 和 addfooterview() 方法,您可以采用垂直方向的线性布局,并且可以通过膨胀布局添加自定义标题,然后在该页脚视图之后膨胀 gridview 。

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

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