Can a GridView have a footer and header just like ListView?

前端 未结 11 2055
北海茫月
北海茫月 2020-12-14 16:29

A quick question:

In ListView I use this code:

list.addHeaderView(headerView);

How to deal with it when working on

11条回答
  •  一生所求
    2020-12-14 16:54

    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:

        ....
        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.

    
    
        
    
        
        
    
    

提交回复
热议问题