Android how to display 2 listviews in one activity

2019-01-03 05:52发布

I have used this code to display 2 list view one on top of the other.

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

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#f00" >
</ListView>

<ListView
    android:id="@+id/listView2"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#0f0" >
</ListView>

The problem is that, this causes the 2 listviews to each occupy half of the screen. I am adding a header to both lists like this.

LevelAdapter adapter = new LevelAdapter(getActivity(),
            R.layout.list_item, weather_data);

    View header = inflater.inflate(R.layout.header2, null);
    View header2 = inflater.inflate(R.layout.header, null);
   lv1.addHeaderView(header);
   lv2.addHeaderView(header2);
    lv1.setAdapter(adapter);
    lv2.setAdapter(adapter);

I would like the header of the second list to appear after the first list is over. How do i do this?How do i make the listviews appear such that the second one starts when the first one is over ? Thanks

9条回答
姐就是有狂的资本
2楼-- · 2019-01-03 06:23

when weights are provided height should be provided as 0dp or wrap_content. but you are not doing so. change your xml file as below. I hope it works for you.

as per your comment i am editing my post according to requirement Solution

1- : Create a list view with only 2 row

1.1-: Add a list view as a row child in first listview #1

1.2-: Add another list view as second row child in first listview #1

i hope by this way you can achieve success.

查看更多
三岁会撩人
3楼-- · 2019-01-03 06:27

I'm new to Android as well and I got something similar (not exactly but kind of workaround) but much EASIER, by making a layout file and putting the required number of list views in it and another XML layout and wrap the previously made layout in ScrollView.

Suppose you have layout named two_listview_layout.xml defined as

<RelativeLayout
..>
    <ListView
        android:id="@+id/listViewOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <ListView
        android:id="@+id/listViewTwo"
        android:layout_width="match_parent"
        android:layout_width="wrap_content"/>
</RelativeLayout>

RelativeLayout is not mandatory here but you can play around with your requirements and the layouts.

Now make another layout in which this layout is wrapped in ScrollView. As ScrollView can have only 1 direct child, you'll have this complete layout as its child.

Name this (below) layout as final_layout.xml

<FrameLayout
...>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include layout="@layout/two_listview_layout" />
    </ScrollView>
</FrameLayout>

Now inflate this final_layout.xml in your fragment/activity and use the Id's that were named in the two_listview_layout.xml to access any of the listview.

Output is something like this: (apologies for the bad quality of screen recorder :p ) Trailers and Reviews - both are listviews here.

enter image description here

查看更多
放荡不羁爱自由
4楼-- · 2019-01-03 06:30

Try with ScrollView and LinearLayout:

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
   <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/action_bar1"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip">

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="200dip"
            android:layout_margin="10dip"
            android:background="#B29090">
        </ListView>



        <ListView
            android:id="@+id/listview2"
            android:layout_width="match_parent"
            android:layout_height="200dip"
            android:layout_margin="10dip"
            android:background="#4A9C67">
        </ListView>
    </LinearLayout>
</ScrollView>
</RelativeLayout>

MainActivity.java

 private ListView list1, list2;
private String [] data1 ={"H", "P", "D", "N", "P", "P"};
private String [] data2 ={"K", "M", "B", "K", "A", "K"};
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sounds);
    list1 = (ListView) findViewById(R.id.listView1);
    list2 = (ListView) findViewById(R.id.listview2);

    list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,data1));
    list2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,data2));



}
查看更多
登录 后发表回答