简体   繁体   English

GridView可以像ListView一样有页脚和页眉吗?

[英]Can a GridView have a footer and header just like ListView?

A quick question: 一个简单的问题:

In ListView I use this code: ListView我使用此代码:

list.addHeaderView(headerView);

How to deal with it when working on gridview ? gridview工作时如何处理?

Thanks. 谢谢。

抱歉, GridView不支持页眉或页脚视图。

There is a quite good implementation of GridView with header support in Google Photos application, as it's OSS code you can use it as is or take it as a reference for your own implementation: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.3_r2.1/com/android/photos/views/HeaderGridView.java 在Google照片应用程序中有一个很好的GridView实现,并且支持标题,因为它可以按原样使用它的OSS代码,或者将其作为您自己实现的参考: http//grepcode.com/file/repository.grepcode .COM / JAVA /转/ com.google.android / Android的应用程序/ 4.3_r2.1 / COM /安卓/照片/视图/ HeaderGridView.java

Basic idea is quite simple - WrapperAdapter creates an fake row by increasing number of items by number of columns and then return a header view for the item. 基本思路非常简单 - WrapperAdapter通过按列数增加项目数来创建假行,然后返回项目的标题视图。

You can use this. 你可以用它。 The footer appears/hides at the bottom of the grid when you reach/leave the last number of items. 当您到达/离开最后一个项目数时,页脚会显示/隐藏在网格的底部。 It does not actually scroll, but I hardly notice the difference. 它实际上并没有滚动,但我几乎没有注意到它的区别。

In your activity/fragment's onCreate/onCreateView you add an OnScrollListener to the GridView: 在您的activity / fragment的onCreate / onCreateView中,您将OnScrollListener添加到GridView:

    ....
    GridView gridview = (YMAnimatedGridview) v.findViewById(R.id.my_gridview);
    gridview.setAdapter(adapter);
    final View footerView = mainView
            .findViewById(R.id.my_grid_footer_view);
    gridview.setOnScrollListener(new GridView.OnScrollListener() {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

                if (firstVisibleItem + visibleItemCount == totalItemCount) {

                    // last item in grid is on the screen, show footer:
                    footerView.setVisibility(View.VISIBLE);

                } else if (footerView.getVisibility() != View.GONE) {

                    // last item in grid not on the screen, hide footer:
                    footerView.setVisibility(View.GONE);
                }
            }

        @Override
        public void onScrollStateChanged(AbsListView view,
                int scrollState) {
        }
    });

Your layout should look something like the below. 您的布局应如下所示。 Notice the layout_weight (and layout_height) parameter in the gridview, it is needed to make the correct space for the footer when it becomes visible. 请注意gridview中的layout_weight(和layout_height)参数,当页脚变为可见时,需要为页脚创建正确的空间。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/my_gridview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:columnWidth="160dp"
        android:gravity="center_horizontal"
        android:horizontalSpacing="12dp"
        android:numColumns="auto_fit"
        android:layout_weight="1"
        android:stretchMode="columnWidth"
        android:verticalSpacing="6dp" />

    <TextView
        android:id="@+id/my_grid_footer_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal"
        android:visibility="gone"
        android:text="footer text here" >
    </TextView>
</LinearLayout>

在此输入图像描述

Sample code: 示例代码:

GridViewWithHeaderAndFooter gridView = (GridViewWithHeaderAndFooter) v.findViewById(R.id.ly_image_list_grid);

LayoutInflater layoutInflater = LayoutInflater.from(this);
View headerView = layoutInflater.inflate(R.layout.test_header_view, null);
View footerView = layoutInflater.inflate(R.layout.test_footer_view, null);
gridView.addHeaderView(headerView);
gridView.addFooterView(footerView);

Gradle build: . Gradle构建:。

compile 'in.srain.cube:grid-view-with-header-footer:1.0.12'

You'd have to use a ListView, then make each row of the list look like it's actually a row of a grid. 您必须使用ListView,然后使列表中的每一行看起来像它实际上是一行网格。 So if you have a 3 column grid, you'd make a layout for the ListView that looks like 3 columns. 因此,如果您有一个3列网格,那么您将为ListView创建一个看起来像3列的布局。 You'd then have to modify certain aspects of the Adapter to make it work so that each ListView row actually represents 3 lines of data -- so you know, getCount()/3 type stuff. 然后,您必须修改适配器的某些方面以使其工作,以便每个ListView行实际上代表3行数据 - 所以你知道,getCount()/ 3类型的东西。

How about checking for the "0" index element in your adapter? 如何检查适配器中的“0”索引元素? You can inflate the custom view for the first one. 您可以为第一个自定义视图充气。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
            if(view==null){
                 if(position==0){
                      //  ...inflate header view
                 }else{
                      //  ...do as usual

Haven't tested it, but should work. 没有测试过,但应该工作。

您可以使用AsymmetricGridView并使用更大的rowSpan指定页眉/页脚,以便它们占据整行。

You could use this library, http://tonicartos.github.io/StickyGridHeaders/ 您可以使用此库, http://tonicartos.github.io/StickyGridHeaders/

which allows you to create headers that are sticky (for grouping the list and keeping the header visible for the current group). 它允许您创建粘滞的标题(用于对列表进行分组并保持标题对当前组可见)。 You can turn off the sticky feature as well. 您也可以关闭粘性功能。

There is a way to accomplish the desired functionality WITHOUT using a library or anything. 有一种方法可以在不使用库或任何东西的情况下实现所需的功能。

EDIT: Just borrow the HeaderGridView Implementation by google, see Here 编辑:只需借用谷歌的HeaderGridView实现,请参阅此处
You could also customize it for footer. 您也可以为页脚自定义它。 The below suggestion is just too complicated and required more tweaking. 以下建议太复杂,需要更多调整。 Without going into specific details, all you need to do is this. 没有详细说明,您需要做的就是这个。

1) Subclass GridView 1)子类GridView
2) override onScrollChanged 2)覆盖onScrollChanged
3) Calculate the offset everytime it scrolls 3)每次滚动时计算偏移量
4) Set the parentView(view that contains the headerView and gridview) translation y to -Offset.(view.setTranslationY(-offset). Also have an if statement to that once it reaches a certain offset it would stop scrolling. 4)将parentView(包含headerView和gridview的视图)转换为-Offset。(view.setTranslationY(-offset)。一旦到达某个偏移量,它还会有一个if语句,它将停止滚动。
5) obviously you want to structure this well so your gridview can have a method like attachToGridview(View view). 5)显然你想要很好地构建这个,所以你的gridview可以有一个像attachToGridview(View view)这样的方法。 I have a complete implementation of this which works. 我有一个完整的实现,这是有效的。
See Scroll offset of GridView for help on getting offset since GridView has a bug were the views get recycled. 有关获取偏移量的帮助,请参阅GridView的Scroll偏移量,因为GridView有一个错误,即视图被回收。

Why don't you change the appearance of the cells for the first rows? 为什么不更改第一行的单元格外观? if you know how many columns you have, you know how many items will appear in the header = number of columns.It works for me 如果你知道你有多少列,你知道在header =列数中会出现多少项。它对我有用

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
              android:layout_height="fill_parent" android:orientation="vertical">

    <com.test.Breadcrumbs android:layout_width="fill_parent" android:layout_height="100dp" />

    <GridView
            android:id="@+id/grid"
            android:numColumns="auto_fit"
            android:gravity="center"
            android:stretchMode="columnWidth"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:verticalSpacing="10dp"
            android:horizontalSpacing="10dp">
    </GridView>

</LinearLayout>

and Breadcrumbs: 和面包屑:

public class Breadcrumbs extends LinearLayout {

    public Breadcrumbs(final Context context, final AttributeSet attrs) {
        super(context, attrs);

        final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutView = inflater.inflate(R.layout.breadcrumbs, this, true);

works fine, scroll for grid works as well. 工作正常,滚动网格工作也是如此。

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

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