2012-03-17 23 views
1

我正在通過教程http://www.vogella.de/articles/AndroidListView/article.html 瞭解如何使用ListActivity。我試圖讓列表滾動,但意識到你不能在佈局文件中使用scrollview來實現這一點。谷歌搜索後,我意識到Android不允許這樣做,但我一直無法找到一個似乎解釋如何使滾動發生的答案。Android ListActivity Scrollview如何獲取滾動列表

的代碼如下:

public class MyListActivityActivity extends ListActivity { 
/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
      "Linux", "OS/2", "iPhone", "WindowsMobile", 
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
      "Linux", "OS/2" , "iPhone", "WindowsMobile", 
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
      "Linux", "OS/2" }; 
    /*LayoutInflater factory = getLayoutInflater(); 
    LinearLayout footer = 
      (LinearLayout) factory.inflate(R.layout.main, null); 
    getListView().addFooterView(footer); */ 


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      R.layout.rowlayout, R.id.label, values); 
    setListAdapter(adapter); 
} 

    protected void onListItemClick(ListView l, View v, int position, long id) { 
    String item = (String) getListAdapter().getItem(position); 
    Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); 
} 
} 

//的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="22px" 
    android:layout_height="22px" 
    android:layout_marginLeft="4px" 
    android:layout_marginRight="10px" 
    android:layout_marginTop="4px" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

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

</LinearLayout> 

目前,更多的價值,你把數組,較小的列表成爲以確保它適合在屏幕上。我希望文本保持相同的大小,並讓它成爲一個可以滾動的長列表。

我讀過關於addHeader和addFooter的地方,它可以使滾動發生。但任何人都可以向我解釋或引導我有一個關於如何實現這一點的更多信息的來源?

我會很感激任何輸入。謝謝

+0

你使用的是什麼版本的android?在4.0.3上它會自動爲我滾動。 – mpeerman 2012-03-17 11:09:56

+0

你的問題中的XML沒有包含「ListView」 - 你的意思是向我們展示不同的佈局? – dldnh 2012-03-17 11:16:57

+0

謝謝大家。我想我什麼也沒有嚇到。 – railslearner 2012-03-18 02:50:03

回答

3

ListView本身就是一個滾動窗口小部件,因此它不能在ScrollView

1

你不需要額外的ScrollView。如果有更多的項目要繪製,則可以使用空間,ListActivity或者精確地說是ListView是默認可滾動的。

順便說一句,應該避免ScrollViews和ListViews的組合有幾個問題需要考慮,你也不需要它的解決方案。

-1

列表視圖它的自我滾動的內部使用。如果你想設置滾動佈局,你可以使用這個代碼 這是你的工作。

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/lwidget36" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_above="@+id/lwidget35" 
android:background="#000321" 
android:overScrollMode="never" > 

<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<ImageView 
android:id="@+id/icon" 
android:layout_width="22px" 
android:layout_height="22px" 
android:layout_marginLeft="4px" 
android:layout_marginRight="10px" 
android:layout_marginTop="4px" 
android:src="@drawable/ic_launcher" > 
</ImageView> 

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

</LinearLayout> 
</ScrollView>