2012-04-12 160 views
0

我有這個漂亮的基本觀點:Android的 - 不能得到滾動視圖滾動列表正確

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     >   

<TextView 
    android:id="@+id/question_label"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Your question: " 
    />  

<TextView 
    android:id="@+id/question"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="" 
    />   

     <EditText 
      android:id="@+id/question_text" 
      android:layout_height="wrap_content" 
      android:hint="@string/question_comment_hint" 
      android:inputType="textMultiLine" 
      android:lines="4" 
      android:layout_width="fill_parent"> 
     </EditText> 

<Button 
      android:id="@+id/submit" 
      android:layout_height="wrap_content" 
      android:text="@string/submit" 
      android:onClick="sendFeedback" 
      android:layout_width="fill_parent"> 
     </Button>  



<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" >  

<TextView 
    android:id="@+id/please_wait"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Please wait while the discussion loads..." 
    />  

    <ListView 
    android:id="@android:id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+id/label" 
     android:textSize="20px" >   
    </ListView>   

    </LinearLayout> 
</ScrollView> 

</LinearLayout> 

我希望用它做是爲了讓用戶通過整個滾動列表註釋。到目前爲止,當屏幕上出現更多評論時,屏幕不會向下滾動。

任何想法我應該改變,以啓用滾動的評論?

謝謝!

+2

不確定這是問題所在,但您絕對不應該在Android的scrollview中擁有listview。 – coder 2012-04-12 20:51:09

+0

@coder怎麼回事?你是對的。無論我嘗試什麼,這都會給我帶來麻煩。你將如何組織這種頁面? – GeekedOut 2012-04-12 20:54:12

+0

什麼是listview的?所有的評論? – coder 2012-04-12 20:56:19

回答

1

我會完全取消滾動視圖 - 基於我認爲你試圖完成的事情,因爲listview將自己處理滾動。喜歡這樣:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    >   

<TextView 
android:id="@+id/question_label"  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Your question: " 
/>  

<TextView 
android:id="@+id/question"  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="" 
/>   

    <EditText 
     android:id="@+id/question_text" 
     android:layout_height="wrap_content" 
     android:hint="@string/question_comment_hint" 
     android:inputType="textMultiLine" 
     android:lines="4" 
     android:layout_width="fill_parent"> 
    </EditText> 

<Button 
     android:id="@+id/submit" 
     android:layout_height="wrap_content" 
     android:text="@string/submit" 
     android:onClick="sendFeedback" 
     android:layout_width="fill_parent"> 
    </Button>  

<TextView 
android:id="@+id/please_wait"  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Please wait while the discussion loads..." 
/>  

<ListView 
android:id="@android:id/list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" 
    android:textSize="20px" >   
</ListView>   

現在的差別是,與ID TextView的 「PLEASE_WAIT」 將不會在列表中滾動。如果你需要它滾動列表,我會建議使用Java中的動態構建的TableLayout作爲列表,然後將它和你的textview添加到ScrollView中。 TableLayout不會自行滾動,這就是爲什麼它比在ScrollView中使用ListView更好的解決方案。如果你想這樣做,讓我知道,我會發布一些示例代碼。

+0

謝謝 - 讓我現在試試這個。我知道我陷入困境與嵌套scrollviews :) – GeekedOut 2012-04-12 21:08:45

+0

哦,是的,完全工作!謝謝。你絕對不辜負你的屏幕名稱,然後一些!喝彩。 – GeekedOut 2012-04-12 21:13:20

1

好像你想在ListView中的註釋上面添加一個標題。正如編碼員指出的那樣,您不應該在ScrollView中放置ListView。你應該看看直接添加一個標題視圖到你的ListView(見addHeaderView)。

1

基本上你可以這樣做: 擺脫scrollview和加載textview。 設置一個簡單的適配器將列表視圖這樣的:

fList = (ListView)findViewById(R.id.lstview); 
fList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"Please wait while the discussion loads..."})); 

比得到您的意見,並設置爲你的列表視圖。

list.setAdapter(adapter); 
1

你不應該在ScrollView中有一個ListView。 哄擡的LinearLayout(垂直),而不是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/question_label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Your question: " /> 

    <TextView 
     android:id="@+id/question" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="" /> 

    <EditText 
     android:id="@+id/question_text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/question_comment_hint" 
     android:inputType="textMultiLine" 
     android:lines="4" > 
    </EditText> 

    <Button 
     android:id="@+id/submit" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:onClick="sendFeedback" 
     android:text="@string/submit" > 
    </Button> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/please_wait" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Please wait while the discussion loads..." /> 

      <LinearLayout 
       android:id="@+id/comments_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:visibility="invisible" > 
      </LinearLayout> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

在您的代碼:

LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.comments_layout); 

要填充的LinearLayout:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    for(int i = 0; i < comments.size(); i++){ 

     View childView = inflater.inflate(R.layout.comment_list_item, commentsLayout, false); 

     TextView comment = (TextView) childView.findViewById(R.id.comment); 

     comment.setText("Something"); 

     categoriesLayout.addView(childView, i); 

    } 

類似的東西,希望這有助於。