簡體   English   中英

具有可滾動標題部分的Android可滾動結果視圖

[英]Android scrollable results view with scrollable header section

我有一個活動,頁面的下半部分是可滾動的結果視圖。 在結果視圖的頂部,是一個帶有一些文本和一個按鈕的相對布局。 此按鈕將使新項目出現在此相對布局中,以優化搜索。 這部分工作正常。 但是,在此相對布局下,我需要添加搜索結果列表。 我曾使用listview進行此操作,但是由於我需要頁面的整個部分的底部(包括該頁眉的相對布局)可滾動,並且由於您無法在滾動視圖中使用listview,因此無法使用。

因此,我希望可以做一些其他的事情,用每個結果項的結果數據填充它,並以編程方式將它們添加到相對布局下。 也許只是在標題相對布局下面有一個線性布局。

我的想法是否正確? 什么是正確的方法?

如果有關系,我的應用程序的最低SDK版本為8。我正在使用支持庫。

編輯:這是我當前的代碼:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DealerFragment" 
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:background="@drawable/background">

    <ImageView
        android:id="@+id/topBar" 
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        />

    <ImageView
        android:id="@+id/logoImageView"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/CD_logo"
        android:src="@drawable/logo" />

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        class="com.google.android.gms.maps.SupportMapFragment" />

    <ScrollView
        android:id="@+id/scrollBox"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:background="#00000000" >

        <RelativeLayout
            android:id="@+id/scrollViewRelativeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00000000">

            <RelativeLayout 
                android:id="@+id/searchHeaderBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:background="#c0000000">

                <TextView
                    android:id="@+id/near"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="@string/near"
                    android:textColor="#ffffff" />

                <TextView
                    android:id="@+id/nearZip"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/near"
                    android:layout_marginLeft="4dp"
                    android:layout_centerVertical="true"
                    android:text="78749"
                    android:textColor="#ffffff" />

                <ImageView
                    android:id="@+id/narrowSearchImage"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/filter"
                    android:contentDescription="@string/CD_Narrow_Results"/>

                <TextView 
                    android:id="@+id/narrowSearchText"
                    android:layout_toLeftOf="@+id/narrowSearchImage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="@string/narrow_results"
                    android:textColor="#ffffff"/>
            </RelativeLayout>
            <LinearLayout 
                android:id="@+id/resultsLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/searchHeaderBox"
                android:layout_marginTop="1dp"
                android:orientation="vertical"
                android:background="#00000000">
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

基本上,我想知道是否應該嘗試添加結果項(當前作為單獨的.XML文件添加),以及如何添加。 或者,如果有其他設備我應該使用它來完成。 我的目標是要使該滾動視圖中的所有內容都能滾動。 在頁面加載之前,我不知道會有多少結果項。

怎么樣,我們實際上將 ListView 放在了 ScrollView

我知道有人說你做不到,但我找到了辦法。


1.ScrollView包裝包含ListView的布局。

2.將其添加到包含ListView的布局的類中。 設置適配器后,請確保將其放置。 只需更改dpListView行布局的高度即可。

int listViewAdapterSize = yourListView.getAdapter().getCount();
LayoutParams listViewParams = yourListView.getLayoutParams();

final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) ((listViewAdapterSize * dp) * scale + 0.5f);
params.height = pixels;

如果您有任何問題,請告訴我!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM