1

我正在努力让 ListView 能够滚动。据我所知,在 LinearLayout 中有一个 ListView 应该没问题,那为什么它不滚动呢?

这是布局

<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/tab2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

        </ListView>

    </LinearLayout>

    <include layout="@layout/tab3" />

    <include layout="@layout/tab1" />                 


</FrameLayout>

这是我用东西填满它的地方..

File folder = new File(Environment.getExternalStorageDirectory().getPath()+"/Download/");
File[] listOfFiles = folder.listFiles();
ArrayAdapter<String> arrayadp = new ArrayAdapter<String>(this, R.layout.list_files);

for (File file : listOfFiles) {
    if (file.isFile()) {
        String extension = "";
        String filename = file.getName();
        int i = filename.lastIndexOf('.');
        if (i > 0) {
            extension = filename.substring(i+1);
        }               
        if(extension.equalsIgnoreCase("wav")){
            arrayadp.add(filename);
        }
    }
}
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(arrayadp);
4

3 回答 3

0

it is ok to put a listview in a linearLayout.

it is not ok to set the height of the listView to be wrap_content, as listView contains a lot of items, all have dynamic size and number.

google has talked about this on this lecture . please watch it all. it might help you with other things too.

于 2013-06-20T19:45:06.637 回答
0

orientation属性对于 a 是必需的LinearLayout。即使只有一个孩子,您也需要将其设置为horizontal或。我认为这可能是您的问题的原因;我已经忘记了很多次方向,它可能会导致视图的显示和行为方式出现许多奇怪的问题。您可能还想考虑不在这里使用 a 作为您的根视图,因为具有多个子级的 a 会使它们很难正确布局。如果您希望您的三个孩子在水平或垂直行中,请考虑 a 。verticalLinearLayoutFrameLayoutFrameLayoutLinearLayout

于 2013-06-20T17:23:42.060 回答
0

This problem because your root layout is a FrameLayout.

Insert linearlayout how root

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/tab2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

        </ListView>

    </LinearLayout>

    <include layout="@layout/tab3" />

    <include layout="@layout/tab1" />                 


</FrameLayout>
</LinearLayout>
于 2015-09-20T05:07:35.953 回答