簡體   English   中英

向 GridView 添加頁眉/頁腳

[英]Adding header/footer to GridView

我很難設計一個帶有頁眉和頁腳的GridView布局,如下所示:

布局

我所做的是,創建一個ListView其中只有一個項目是GridView並使用addHeaderView()addFooterView()添加頁眉/頁腳。 問題是GridView沒有顯示整個項目。 我使用以下方法禁用了GridView滾動:

  gridview.setOnTouchListener(new View.OnTouchListener(){

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

        });

有沒有辦法顯示使GridView顯示所有項目? 我試過將GridView的高度設置為wrap_content但它不起作用。 這是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>

我知道將 gridview 放在列表視圖中是不對的,但是客戶端要求這種布局..

這是一個很好的起點:

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

此類處理標題非常好,與ListView處理方式完全相同。

您也應該設法添加頁腳處理,但它有點復雜,因為您必須根據Gridview的元素數量放置占位符。 由於Gridview行的高度基於其最后一個元素的高度,因此您必須獲得GridView最后一項的高度

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

我用它來計算最后一個項目的高度。 它可能不是最佳的,但它適用於我的用例:

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();

看看這個希望它能解決你的問題

    <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