Android ListActivity Scrollview How to get a list

2019-07-27 05:43发布

I was going through a tutorial on http://www.vogella.de/articles/AndroidListView/article.html learning how to do use ListActivity. I am trying to get the list to scroll, but came to realize that you can't use scrollview in the layout file to make this happen. After googling around, I realized that android doesn't allow this, but I haven't been able to find an answer that seems to explain how to make the scrolling happen.

The code is below:

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();
}
}

//The xml file:

<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>

Currently, the more values you put into the array, the smaller the list becomes to make sure that it fits on the screen. I want the text to stay the same size and let it be a long list that I could scroll through.

I've read somewhere about addHeader and addFooter that'd enable the scrolling to happen. But can anyone explain to me or lead me to a source that has more information about how to implement this?

I'd appreciate any input. Thanks

3条回答
一夜七次
2楼-- · 2019-07-27 05:43

You don't need an additional ScrollView. The ListActivity or to be precise the ListView is scrollable by default if there are more items to draw then space is available.

By the way, one should avoid the combination of ScrollViews and ListViews there are couple of problems to consider and you don't need it for your solution, either.

查看更多
乱世女痞
3楼-- · 2019-07-27 05:56

the ListView is itself a scrolling widget, so it can't be used inside of a ScrollView

查看更多
beautiful°
4楼-- · 2019-07-27 06:06

List view it's self scrolling. if you want to set scrolling layout you can use this code This is work for you.

<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>
查看更多
登录 后发表回答