如何并行添加两个ListView?

时间:2013-07-12 05:47:27

标签: android android-layout android-listview android-adapter android-adapterview

嗨,我是Android的新手。是否可以在同一屏幕上添加两个ListView并行?我已经使用一个ListView并使用ListAdapter显示项目,但实际上我需要它:当它显示项目并滚动水平时,我希望第一个TextView冻结,以及何时我滚动垂直,它会滚动。

xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="horizontal">
<!-- Main ListView 
     Always give id value as list(@android:id/list)
-->


<HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        android:id="@+id/horizontalScrollView">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="#ffffff"
                  android:orientation="vertical">


        <TableRow
                android:id="@+id/tableRow1"
                android:scrollbarAlwaysDrawHorizontalTrack="true"
                android:layout_height="wrap_content"
                android:layout_width="match_parent">

            <TextView
                    android:id="@+id/TextView2"
                    android:text="GRPName"

                    android:background="#A513FF"

                    android:textColor="#000000"
                    android:layout_width="150dp"
                    android:padding="3dip"
                    />
            <TextView
                    android:id="@+id/TextView3" android:text="QTY"
                    android:background="#A513FF"
                    android:textColor="#000000"
                    android:layout_width="60dp"
                    android:gravity="right"

                    android:padding="3dip" />
            <TextView
                    android:id="@+id/TextView4" android:text="BUD"
                    android:background="#A513FF"
                    android:textColor="#000000"
                    android:layout_width="60dp"
                    android:gravity="right"
                    android:padding="3dip" />
            <TextView
                    android:id="@+id/TextView5" android:text="STK"
                    android:background="#A513FF"
                    android:textColor="#000000"
                    android:layout_width="60dp"
                    android:gravity="right"
                    android:padding="3dip" />
            <TextView
                    android:id="@+id/TextView2" android:text="DIF"
                    android:background="#A513FF"
                    android:gravity="right"

                    android:textColor="#000000"
                    android:layout_width="60dp"
                    android:padding="3dip"
                    />
            <TextView
                    android:id="@+id/TextView3" android:text="DIF%"
                    android:background="#A513FF"
                    android:textColor="#000000"
                    android:layout_width="60dp"
                    android:gravity="right"

                    android:padding="3dip" />


        </TableRow>
        <ListView
                android:id="@android:id/list"
                android:smoothScrollbar="true"
                android:layout_width="fill_parent"
                android:layout_weight="1"

                android:layout_height="fill_parent"/>

    </LinearLayout>


</HorizontalScrollView>

ListAdapter:

ListAdapter adapter = new SimpleAdapter(this, contactList,
            R.layout.list_item,
            new String[] {  TAG_GRPNAME, TAG_QNT, TAG_BUDGET, TAG_STOCK, TAG_DIFF, TAG_DIFF_P, },
            new int[] {
                     R.id.l2, R.id.l3, R.id.l4, R.id.l5, R.id.l6, R.id.l7});
                      setListAdapter(adapter);
                      ListView lv = getListView();
                      }

我想冻结TAG_GRPNAMER.id.l2

2 个答案:

答案 0 :(得分:2)

尝试这样的事情:
main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

                <ListView   android:id="@+id/listview1" 
                            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/listview2" 
                        android:layout_height="fill_parent" 
                        android:layout_width="fill_parent">

            </ListView>
</LinearLayout>

</LinearLayout>

其中2 LinearLayout将具有重量 1.
并且来自代码

ListView ls1,ls2;
String colors[] = {"Red","Blue","White","Yellow","Black", "Green","Purple","Orange","Grey"};
private String phones[] = {"Android", "Blackberry", "iPhone","Windows Phone"};

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ls1 = (ListView) findViewById (R.id.listview1);
    ls2 = (ListView) findViewById (R.id.listview2);

    ls1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, colors));
    ls2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, phones));
 }

答案 1 :(得分:0)

@ ridoy的代码稍有变化就在这里,可能你的工作完成了......!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/list1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight=".50" >
    </ListView>

    <ListView
        android:id="@+id/list2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight=".50" >
    </ListView>

</LinearLayout>

还有任何疑问,请问。