如何在Android上的单个活动中使用多个列表视图?

Ana*_*and 16 android

我有9个列表视图应该属于单个活动.所有列表可能/可能不会一次显示在屏幕上(一次一个都很好)

Sco*_*ggs 24

对于同一个Activity中的多个ListView,这是一个不错的解决方案.首先是最重要的布局文件(从ANDROID复制:将屏幕拆分为2等于具有2个列表视图的部分,干得好!).请注意,ListViews嵌入在它们自己的LinearLayouts中,每个LinearLayout都是相同的权重.

<LinearLayout android:layout_weight="1" 
                    android:layout_height="fill_parent" 
                    android:layout_width="fill_parent">

                <ListView   android:id="@+id/list1" 
                            android:layout_height="fill_parent" 
                            android:layout_width="fill_parent">

                </ListView>
    </LinearLayout>

<LinearLayout android:layout_weight="1" 
                android:layout_height="fill_parent" 
                android:layout_width="fill_parent">

            <ListView   android:id="@+id/list2" 
                        android:layout_height="fill_parent" 
                        android:layout_width="fill_parent">

            </ListView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

// Start java Code (you can figure out the imports)
public class AnActivity extends Activity {
private ListView lv1 = null;
private ListView lv2 = null;
private String s1[] = {"a", "b", "c", "d", "e", "f"};
private String s2[] = {"r", "s", "t", "u", "v", "w", "x"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

        // Get UI references.
        //
    lv1 = (ListView) findViewById (R.id.list1);
    lv2 = (ListView) findViewById (R.id.list2);

    lv1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s1));
    lv2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s2));

} // onCreate()

} // class
Run Code Online (Sandbox Code Playgroud)

对不起,如果有任何错别字.我正在窗口中编辑,而不是在日食中.祝好运!


小智 5

@Scott非常感谢,我觉得这篇文章很有帮助.在我的情况下,我希望两个listview在相同的水平布局(侧面).

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ListView
                android:id="@+id/listView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:visibility="visible">
            </ListView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <ListView
                android:id="@+id/listView2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:visibility="visible">
            </ListView>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)