2013-08-20 72 views
4

我想在ScrollView以下安裝一個ListView。我試圖把他們一個LineaLayout內像這樣ListView下面的滾動視圖在Android中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/frameLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <ScrollView 
     android:id="@+id/marketDetailScrollView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    ... 
    </ScrollView> 
    <ListView android:id="@android:id/list" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:background="#FFF"/> 
</LinearLayout> 

ListView不會得到顯示。我也試過把它放在RelaviteLayout裏面,但什麼也沒有。我能以某種方式在ScrollView下有ListView嗎?

只是爲了添加一些東西。我不想分割我的屏幕,因此我有一半用ScrollView,另一半用ListView。我希望用戶向下滾動ScrollView這顯然是比屏幕尺寸再大一點的ListView應該開始

+0

修復滾動視圖的高度..就像'android:layout_height =「100dip」' –

+0

你可以讓listview在scrollview中將視圖作爲頁眉或頁腳添加到listview(不帶scrollview)或指定滾動視圖的固定高度並在其下面添加listview – Raghunandan

+1

在你的滾動視圖中,'android:layout_height =「fill_parent」'意味着你的滾動視圖將把所有空間都放在最下面,所以你的ListView沒有更多的空間。 –

回答

10

我會建議你把在ListView了滾動內容HeaderView或者你明確地希望有兩個獨立的屏幕上滾動的領域?

實施例用於使滾動視圖的內容列表視圖作爲標題(一個單一滾動區):

public void onCreate(Bundle s){ 
    setContentView(R.id.yourLayout); 

    ListView listView = (ListView) findViewById(R.id.listView); 

    // Set the adapter before the header, because older 
    // Android version may have problems if not 

    listView.setAdapter(new YourAdapter()); 

    // Add the header 
    View header = inflater.inflate(
      R.layout.you_layout_that_was_in_scrollview_before, null, false); 

    listView.addHeaderView(header); 

} 

活性的佈局將看起來像:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/frameLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ListView android:id="@+id/listView" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:background="#FFF"/> 
</LinearLayout> 

如果你想要兩個可滾動區域,你應該使用版圖權重:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/frameLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<ScrollView 
    android:id="@+id/marketDetailScrollView" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" > 
    <!-- ScrollViewContent --> 
</ScrollView> 
<ListView android:id="@android:id/list" 
android:layout_height="0dp" 
android:layout_width="match_parent" 
android:background="#FFF" 
android:layout_weight="1"/> 
</LinearLayout> 
+0

不,我不知道。所以通過放置是因爲headerview只會創建一個可滾動的視圖? – alecnash

+0

是的...那會創建一個單一的可滾動區域...我會更新我的答案並添加一個示例 – sockeqwe

+0

我仍然有兩個可滾動區域 – alecnash

0

​​你不會工作, 改變它來包裝內容,並給予layout_weight 1也滾動視圖,然後它可能適用於你

0

通常情況下,在滾動視圖中放置listview是不好的。但如果需要的話,你需要設置listview的高度。按照下面的鏈接來計算在scrollview中擬合的listview的高度。

http://www.jiramot.info/android-listview-in-scrollview-problem

使用這種方法,你可以實現你想要

+0

它不在滾動查看其滾動視圖 – alecnash

+0

哦好吧。在這種情況下,只需使用weight屬性併爲您的線性佈局的scrollview和listview設置權重。它應該工作 – Sushil

0

你必須設置滾動視圖的內容和它的元素「包裝內容」的高度。 XML將是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/frameLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<ScrollView 
    android:id="@+id/marketDetailScrollView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 
...(height will be wrap_content for inner element as well) 
</ScrollView> 
<ListView android:id="@android:id/list" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:background="#FFF" 
android:layout_weight="1"/> 
</LinearLayout> 

這將可以幫助你..

+0

不這樣工作。 ListView仍然無處可見 – alecnash

+0

這可能是因爲你內在的ScrollView內容。你能告訴我什麼是你的scrollView的內容? – Avijit

+0

與此有關的內容是什麼?滾動視圖大於屏幕 – alecnash

0

我認爲這是接近你在找什麼。我給你這個代碼的代碼,但我不得不說,把ListView放在ScrollView是一個壞習慣,你不應該使用這個解決方案。您應該在更正確的事情上工作,並且在滾動視圖中沒有滾動視圖。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:android1="http://schemas.android.com/apk/res/android" 
    android:id="@+id/containerScrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:id="@+id/scrollViewContent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <ScrollView 
      android:id="@+id/marketDetailScrollView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <LinearLayout 
       android:id="@+id/scrollViewContainer" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" > 

       <!-- the views contained in your ScrollView goes here --> 
      </LinearLayout> 
     </ScrollView> 

     <ListView 
      android:id="@+id/list" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#FFF" /> 
    </LinearLayout> 

</ScrollView> 
+0

不會像這樣工作 – alecnash

+0

@alecnash我編輯我的答案並測試它,現在可以工作^^ –

+0

請重新閱讀答案 – alecnash