2013-12-11 59 views
0

我有一個活動,其中頁面的下半部分是可滾動結果視圖。在結果視圖的頂部,是包含一些文本和按鈕的相關佈局。此按鈕將使新項目出現在此相對佈局中以優化搜索。這部分是全部工作。但是,在此相對佈局下方,我需要添加搜索結果列表。我有這個工作與一個列表視圖,但因爲我需要頁面的部分(包括該標頭相對佈局)的整個底部可滾動,因爲你不能在滾動視圖中有一個列表視圖,這將無法正常工作。Android可滾動結果視圖和可滾動標題部分

所以,我希望我可以做一些事情,如製作另一個視圖,用每個結果項目的結果數據填充它,並以編程方式將它們添加到相對佈局下方。也許只是在該標題相對佈局下有一個線性佈局。

我在這個想法的正確軌道上?什麼是正確的方法來做到這一點?

如果有問題,我的應用程序有8分鐘的sdk版本。我正在使用支持庫。

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

<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文件),如果是我如何做到這一點。或者如果有其他設備我應該用來完成這個。我的目標是讓該滾動視圖中的所有內容都能滾動。我不知道在加載頁面之前會有多少結果項目。

+0

我在理解你想要完成的事情上有點麻煩。你能發佈你擁有什麼,你想要什麼,或者一些代碼的截圖嗎? –

+0

@MarioStoilov當然可以。用代碼和附加註釋編輯。 – PFranchise

+0

我發現了這個解決方案,我將嘗試:http://stackoverflow.com/a/2397869/361815 – PFranchise

回答

1

怎麼樣,我們實際上把ListViewScrollView

我知道人們說你不能,但我找到了一種方法。


裹包含您ListView,具有ScrollView佈局。

2.將此類添加到包含您的ListView的佈局中。確保在設置適配器後放置它。唯一需要更改的是dp到您的ListView行佈局的高度。

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; 

讓我知道如果您有任何問題!