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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3405211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:03:26  来源:igfitidea点击:

How to use multiple listviews in a single activity on android?

android

提问by Anand

I have 9 listviews which should come under single activity. All the lists may/maynot display at a time on the screen (all at a time is nice)

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

回答by Scott Biggs

Here is a decent solution to more than one ListView within the same Activity. First the all-important layout file (copied from ANDROID : split the screen in 2 equals parts with 2 listviews , good job!). Note that the ListViews are embedded in their own LinearLayouts, which are each equally weighted.

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

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

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

Sorry if there are any typos. I'm editing right here in the window, not in eclipse. Good luck!

如有错别字,请见谅。我正在窗口中进行编辑,而不是在 eclipse 中。祝你好运!

回答by Mighty

@Scott many thanks, I found this post helpful. In my case I wanted the two listview to be on the same horizontal layout (sideways).

@Scott 非常感谢,我发现这篇文章很有帮助。在我的情况下,我希望两个列表视图处于相同的水平布局(横向)。

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

回答by Osama.070032

You should use page viewer, take a look at this example, hope this will help you http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/

你应该使用页面查看器,看看这个例子,希望这对你有帮助 http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizo​​ntal-view-paging/